JOINs
Combine rows from two tables based on a related column.
Questions this answers
- →How do I get data from two tables at once?
- →How do I match orders to their customers?
- →What's the difference between INNER and LEFT JOIN?
- →How do I find rows in one table with no match in another?
How it works
Most real-world data is spread across multiple tables. JOINs combine them by matching rows where a column value is equal — typically a foreign key to a primary key. INNER JOIN returns only matched rows. LEFT JOIN returns all rows from the left table, with NULLs for unmatched right-side columns.
INNER JOIN
Returns only rows where the join condition matches in both tables.
SELECT orders.id, customers.name, orders.total FROM orders INNER JOIN customers ON orders.customer_id = customers.id;
LEFT JOIN
Returns all rows from the left table. Unmatched right-side rows get NULLs.
SELECT customers.name, orders.id FROM customers LEFT JOIN orders ON orders.customer_id = customers.id;
💡 Customers with no orders still appear — with NULL for orders.id.
Find unmatched rows
-- Customers who have never placed an order SELECT customers.name FROM customers LEFT JOIN orders ON orders.customer_id = customers.id WHERE orders.id IS NULL;