Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between DELETE and DELETE FROM in SQL?

Tags:

Is there one? I am researching some stored procedures, and in one place I found the following line:

DELETE BI_Appointments WHERE VisitType != (     SELECT TOP 1 CheckupType     FROM BI_Settings     WHERE DoctorName = @DoctorName) 

Would that do the same thing as:

DELETE FROM BI_Appointments WHERE VisitType != (     SELECT TOP 1 CheckupType     FROM BI_Settings     WHERE DoctorName = @DoctorName) 

Or is it a syntax error, or something entirely different?

like image 270
Joe M Avatar asked Mar 11 '14 21:03

Joe M


People also ask

What is difference between delete and delete from in SQL?

Hi friends there is no difference between delete and delete from in oracle database it is optional, but this is standard to write code like this DELETE FROM table [ WHERE condition ] this is sql-92 standard. always develop your code in the standard way.

What is the difference between delete from and TRUNCATE?

The delete statement is used to remove single or multiple records from an existing table depending on the specified condition. The truncate command removes the complete data from an existing table but not the table itself. It preserves the table structure or schema.

What does delete from do SQL?

The Delete command in SQL is a part of the Data Manipulation Language, a sub-language of SQL that allows modification of data in databases. This command is used to delete existing records from a table. Using this, you can either delete specific records based on a condition or all the records from a table.

What are 2 differences between delete and TRUNCATE?

The DELETE statement removes rows one at a time and records an entry in the transaction log for each deleted row. TRUNCATE TABLE removes the data by deallocating the data pages used to store the table data and records only the page deallocations in the transaction log. DELETE command is slower than TRUNCATE command.

What is the difference between delete and drop in SQL?

Difference between DELETE and DROP SQL. DELETE is a Data Manipulation Language command, DML command and is used to remove tuples/records from a relation/table. Whereas DROP is a Data Definition Language, DDL command and is used to remove named elements of schema like relations/table, constraints or entire schema.

What is the use of delete in SQL?

1. DELETE : DELETE is a DML (Data Manipulation Language) command and is used when we specify the row (tuple) that we want to remove or delete from the table or relation. The DELETE command can contain a WHERE clause.

What is the difference between delete and truncate in SQL?

Difference between SQL (Structured Query Language) and T-SQL (Transact-SQL). Delete and truncate both commands can be used to delete data of the table. Delete is a DML command whereas truncate is DDL command. Truncate can be used to delete the entire data of the table without maintaining the integrity of the table.

What is the difference between delete and delete from in Oracle?

It's there for standard language syntax compliance. Hi friends there is no difference between delete and delete from in oracle database it is optional, but this is standard to write code like this DELETE FROM table [ WHERE condition ] this is sql-92 standard. always develop your code in the standard way.


1 Answers

Assuming this is T-SQL or MS SQL Server, there is no difference and the statements are identical. The first FROM keyword is syntactically optional in a DELETE statement.

http://technet.microsoft.com/en-us/library/ms189835.aspx

The keyword is optional for two reasons.

First, the standard requires the FROM keyword in the clause, so it would have to be there for standards compliance.

Second, although the keyword is redundant, that's probably not why it's optional. I believe that it's because SQL Server allows you to specify a JOIN in the DELETE statement, and making the first FROM mandatory makes it awkward.

For example, here's a normal delete:

DELETE FROM Employee WHERE ID = @value 

And that can be shortened to:

DELETE Employee WHERE ID = @value 

And SQL Server allows you to delete based on another table with a JOIN:

DELETE Employee FROM Employee     JOIN Site        ON Employee.SiteID = Site.ID WHERE Site.Status = 'Closed' 

If the first FROM keyword were not optional, the second query above would need to look like this:

DELETE FROM Employee FROM Employee     JOIN Site        ON Employee.SiteID = Site.ID WHERE Site.Status = 'Closed' 

This above query is perfectly valid and does execute, but it's a very awkward query to read. It's hard to tell that it's a single query. It looks like two got mashed together because of the "duplicate" FROM clauses.

Side note: Your example subqueries are potentially non-deterministic since there is no ORDER BY clause.

like image 156
Bacon Bits Avatar answered Oct 06 '22 01:10

Bacon Bits