SQL Joins


Tips

ON vs WHERE

The following queries are not interchangeable: ON clauses are evaluated at match time, while WHERE clauses are evaluated after matching.

SELECT *
FROM transactions
LEFT JOIN people ON people.ID=transactions.ID
WHERE transactions.ID = 1

This will return only the rows in people where ID = 1.

SELECT *
FROM transactions
LEFT JOIN people ON people.ID=transactions.ID
    AND transactions.ID = 12345

This will return all rows in people, but joined columns will only be non-NULL for the rows where ID = 1.


CategoryRicottone