Thaise
Thaise

Reputation: 1103

Does the join order matter in the 'ON clause' in a LEFT JOIN in SQL?

I would like to know if these two SQL queries give us the same result since the 'ON clause' is different.

-- A    
SELECT * FROM a LEFT JOIN b ON a.id=b.id

-- B
SELECT * FROM a LEFT JOIN b ON b.id=a.id

Thank you

Upvotes: 1

Views: 1066

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269973

No, the JOIN order does not matter. SQL is a descriptive language, where a SQL query describes the result set, not how it is processed. The compiler and optimizer figure out the execution plan.

The two conditions express the same condition. However, the order of the tables in the FROM clause matters a great deal for an outer join.

I should note that SQL optimizers do not catch everything, so sometimes how something is expressed can affect the execution plan. This wold not be such a case.

Upvotes: 3

Related Questions