INNER JOIN returns only rows that match in both tables. LEFT JOIN returns all rows from the left table and fills missing matches from the right side with NULLs.
SELECT u.id, p.id
FROM users u
LEFT JOIN posts p ON p.user_id = u.id;Joins combine rows from two tables based on a join condition.
Customers with orders (INNER):
SELECT c.id, o.id AS order_id
FROM customers c
JOIN orders o ON o.customer_id = c.id;Customers even without orders (LEFT):
SELECT c.id, o.id AS order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id;If you put a condition on the right table in `WHERE`, you can filter out the `NULL` rows and effectively lose the left-join behavior:
-- This removes customers with no orders
... LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.status = 'PAID';To keep left rows, move conditions into the `ON` clause:
... LEFT JOIN orders o
ON o.customer_id = c.id AND o.status = 'PAID';