# Topic 30: LEFT & RIGHT JOIN
Preserving outer table rows, NULL padding for unmatched records, anti-joins, and why RIGHT JOIN is rarely used in practice.
1. Definition
A LEFT JOIN returns all rows from the left table, along with matching rows from the right table; if no match exists, NULLs are returned for right-table columns.
2. Explain Like I'm 5
Think of attendance at a party: Every invited guest on your list is recorded (left table). If they brought a gift (right table), the gift is recorded; if they did not, the gift column says NULL.
3. Animation & Visual Simulation
Interactive Engine TracePhysical Join Engine (Build & Probe Phase)
SELECT u.name, o.total FROM users u JOIN orders o ON u.id = o.user_id;Active InputStep 1: Build Phase (Smaller Table)
Key: id=1 ➔ Alice
Key: id=2 ➔ Bob
The optimizer selects the smaller table (users) and hashes primary keys into memory buckets.
4. Real-World Example
Finding all customers and any orders they placed, including customers who have never ordered.
| Customer | Order ID | Total |
|---|---|---|
| Alice | 101 | $120.00 |
| Bob | 102 | $85.00 |
| Charlie (Preserved) | NULL | NULL |
5. SQL Syntax
ANSI SQL / PostgreSQL DialectSELECT c.id, c.name, o.id AS order_id, o.total_amount FROM customers c LEFT JOIN orders o ON c.id = o.customer_id;
SELECT c.id, c.name FROM customers c LEFT JOIN orders o ON c.id = o.customer_id WHERE o.id IS NULL;
6. Important Rules & Behaviors
| Rule | Behavioral Explanation |
|---|---|
| All Left Rows Kept | Every row from the left table is preserved in the output at least once. |
| NULL Padding | If no match is found on the right table, all right table columns are populated with NULL. |
| RIGHT JOIN Equivalence | A RIGHT JOIN B is logically identical to B LEFT JOIN A. Standard practice standardizes on LEFT JOIN. |
| Outer row preservation | LEFT JOIN preserves all rows from the left relation, padding unmatched right attributes with NULL. |
| Filter placement (ON vs WHERE) | Filters placed in ON preserve left rows with NULL right columns; filters in WHERE discard unmatched left rows. |
7. Deep Dive: The Accidental Demotion of LEFT JOIN to INNER JOIN
A notorious bug occurs when developers write a LEFT JOIN, and then put a filter on the right table in the WHERE clause: WHERE orders.status = "completed". Because orders.status IS NULL for unmatched customers, and NULL = "completed" evaluates to UNKNOWN, all non-ordering customers are discarded, converting the LEFT JOIN into an accidental INNER JOIN!
-- ACCIDENTAL INNER JOIN (filters out customers without orders!): SELECT c.name, o.total FROM customers c LEFT JOIN orders o ON c.id = o.customer_id WHERE o.status = 'completed'; -- FIXED (Filter placed inside the ON clause preserves customers): SELECT c.name, o.total FROM customers c LEFT JOIN orders o ON c.id = o.customer_id AND o.status = 'completed';
8. Advantages & Disadvantages
Advantages
- •Preserves master entity records even when child records do not exist.
- •Anti-join pattern (WHERE right.id IS NULL) efficiently identifies missing data.
Disadvantages & Trade-offs
- •Accidental WHERE filters demote outer joins to inner joins.
9. Common Interview Questions
Q1.What is the critical difference between putting a filter in the ON clause vs the WHERE clause of a LEFT JOIN?
In a LEFT JOIN: 1. Conditions in the ON clause determine which right-table rows match the left-table rows; if no right row matches, the left row is STILL emitted with NULLs for right columns. 2. Conditions in the WHERE clause evaluate AFTER the join; if you filter right_table.status = 'active' in WHERE, the NULLs created for unmatched left rows are rejected, converting your LEFT JOIN into an INNER JOIN!
Q2.What is an Anti-Join and how do you write it using LEFT JOIN vs NOT EXISTS?
An Anti-Join returns rows from table A that have NO matching rows in table B (e.g. users with zero orders). Using LEFT JOIN: SELECT u.* FROM users u LEFT JOIN orders o ON u.id = o.user_id WHERE o.id IS NULL; Using NOT EXISTS: SELECT u.* FROM users u WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.user_id = u.id); NOT EXISTS is strictly preferred by modern optimizers because it short-circuits on the first match.
Q3.Why is RIGHT JOIN almost never used in professional SQL codebases?
RIGHT JOIN is semantically identical to LEFT JOIN with the table order reversed (A RIGHT JOIN B is identical to B LEFT JOIN A). In professional codebases, engineers read queries from top-to-bottom and left-to-right. Using LEFT JOIN keeps the primary driving relation as the anchor at the top of the FROM clause, maximizing readability and mental clarity.
Q4.Can a LEFT JOIN produce duplicate rows of the left table?
Yes! If a single row in the left table matches multiple rows in the right table (a 1-to-many relationship, such as one user with five orders), the left row is duplicated five times in the output stream, once for each matching order.
Q5.How does the query optimizer execute a Hash Left Join?
The optimizer builds an in-memory hash table on the right table (including a bitmask tracking whether each entry has been matched). It then streams the left table, probing the hash table: if matches exist, it emits joined rows; if no match exists, it emits the left row with NULL values for right columns.
10. Interview-Ready Answer
“A LEFT JOIN preserves every row from the left table, populating right-table columns with NULL when no match exists. Placing filters on the right table inside the WHERE clause accidentally demotes it to an INNER JOIN; filters on outer tables must remain in the ON condition.”
11. Quick Test
How does placing "WHERE orders.status = 'shipped'" affect a query that uses "FROM customers LEFT JOIN orders ON customers.id = orders.customer_id"?