4 SQL Joins
Learn how SQL joins combine related table rows, how join types handle unmatched rows, and how to write clear queries that avoid common pitfalls.
How joins connect tables
A combines rows from separate tables when they satisfy a specified condition. This is useful when related information is stored in different places, such as employee names in one table and department names in another.
Suppose employees.department_id refers to departments.department_id. The employee column is a , and the department identifier can serve as a . Matching these columns connects each employee to the relevant department.
The ON clause states how rows match. In SQL, by itself means . Table aliases can make a query more concise:
The key idea is to make the relationship between tables explicit in the condition.
Choose which rows to keep
The type determines what happens when a row has no match. Choose the type according to which rows the result must retain.
returns only row pairs that match. Employees without a matching department and departments without employees are left out.
keeps every row from the left table. When no right-table row matches, its columns appear as
NULL.A right does the reverse: it preserves every row from the right table and fills left-table columns with
NULLwhen unmatched. Often, reversing the table order and using a is clearer.keeps all rows from both tables. Matching rows are combined; unmatched rows have
NULLvalues for columns from the other table.produces every possible pair. If one table has rows and another has rows, the result has rows.
For example, a can reveal employees without a matching department by filtering for a missing department identifier:
A is appropriate when every combination is intended, such as pairing each product with each available size. Otherwise, the number of resulting rows can grow unexpectedly.
Takeaway: An keeps matches only; outer joins preserve unmatched rows from one or both sides; a produces all combinations.
Follow relationships across tables
Joins can be chained to follow relationships across several tables. For example, an order can refer to a customer, and each order line can refer to a product:
Each ON clause describes one relationship in the chain. Because an order can have multiple order lines, the order details appear on multiple result rows—one for each matching line. This is expected: a combines matching rows and does not necessarily produce one result row per original record.
Avoid common pitfalls
A clear starts with an accurate match condition and a deliberate choice of which unmatched rows to preserve.
Use explicit conditions. A missing or incorrect condition can create unintended combinations; use
only when all combinations are intended.Be careful when filtering after an outer . A
WHEREcondition on the right table can remove rows whose right-side columns areNULL, making a behave like an .To keep unmatched left-table rows while restricting which right-table rows may match, put the restriction in
ONrather thanWHERE.Expect repeated values in one-to-many relationships. For example, a department's details repeat for each matching employee.
Select named columns instead of using
SELECT *when practical. This makes results easier to interpret and avoids unnecessary or duplicate-named columns.
When both tables share the matching column name, USING (column_name) can replace an ON condition and returns one copy of that column. ON is often clearer when key names differ.
Takeaway: Check the condition, the unmatched-row behavior, and the effect of filters before interpreting the result.