Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a SQL query have two from clauses?

This just looks so odd to me:

delete from GearsDev.dbo.Products 
from GearsDev.dbo.Products as C
inner join #Common as M
    on M.item = C.ItemNumber

#Common is a temp table, but the rest of it makes no sense to me.

How can you have two from clauses?

like image 220
broke Avatar asked Nov 04 '11 14:11

broke


People also ask

Can you have two from clauses in SQL?

As can be seen from the documentation of DELETE, it can take two FROM clauses. The first FROM : FROM: Is an optional keyword that can be used between the DELETE keyword and the target table_or_view_name, or rowset_function_limited.

Can a SQL query have multiple WHERE?

The AND operator allows the existence of multiple conditions in an SQL statement's WHERE clause.

How do you do multiple conditions in SQL?

The SQL AND condition and OR condition can be combined to test for multiple conditions in a SELECT, INSERT, UPDATE, or DELETE statement. When combining these conditions, it is important to use parentheses so that the database knows what order to evaluate each condition.


1 Answers

As can be seen from the documentation of DELETE, it can take two FROM clauses.

The first FROM:

FROM: Is an optional keyword that can be used between the DELETE keyword and the target table_or_view_name, or rowset_function_limited.

The second FROM:

FROM <table_source>: Specifies an additional FROM clause. This Transact-SQL extension to DELETE allows specifying data from and deleting the corresponding rows from the table in the first FROM clause.

This extension, specifying a join, can be used instead of a subquery in the WHERE clause to identify rows to be removed.

So, the SQL will delete records from the Products table that have a matching item when it is joined with #common.

This is equivalent (in meaning) to the following query:

delete from [GearsDev].[dbo].[Products]
where ItemNumber in
(
  select item from #common
)
like image 73
Oded Avatar answered Oct 23 '22 15:10

Oded