SQL course menu
SQL JOINs Explained: Combine Data From Multiple Tables
An introduction to SQL JOINs for beginners: why data is split across tables, how ON matches rows, and the difference between INNER, LEFT, RIGHT, FULL OUTER, SELF and CROSS JOIN, with diagrams and examples.
On this page
Databases split information into separate tables: customers in one, orders in another. That keeps each fact in one place, but most questions need data from several tables at once. Which customer placed this order? Which products have never sold? A JOIN combines rows from two tables into one result, based on a matching column.
Why data lives in separate tables#
Imagine storing everything in one big table:
If Asha moves to Pune, every one of her orders must be updated, and missing one leaves the data contradicting itself. So instead, customer details are stored once in customers, and each order stores only the customer's id. JOINs reunite the pieces at query time. (See SQL Database Basics for primary and foreign keys.)
The example tables#
customers
orders
Two details make these tables a good test of every JOIN type: Meera and John have no orders, and order 104 belongs to customer 5, who does not exist.
How a JOIN works#
SELECT c.name, o.order_id, o.amount
FROM customers c
JOIN orders o
ON o.customer_id = c.customer_id;FROM customers c JOIN orders onames the two tables (with short aliases).ON o.customer_id = c.customer_idis the join condition: it says which rows belong together.
For each pair of rows where the condition is true, the database produces one combined row. The JOIN type decides what happens to rows that have no partner.
The JOIN types at a glance#
INNER JOIN: only matches#
name | order_id | amount
-----+----------+-------
Asha | 101 | 250
Asha | 102 | 120
Ravi | 103 | 560LEFT JOIN: all customers#
name | order_id | amount
------+----------+-------
Asha | 101 | 250
Asha | 102 | 120
Ravi | 103 | 560
Meera | NULL | NULL
John | NULL | NULLFULL OUTER JOIN: everything#
name | order_id | amount
------+----------+-------
Asha | 101 | 250
Asha | 102 | 120
Ravi | 103 | 560
Meera | NULL | NULL
John | NULL | NULL
NULL | 104 | 90NULL fills the columns of the side that had no match.
Choosing the right JOIN#
Ask one question: which rows must not disappear?
- Only complete pairs matter →
INNER JOIN. - Every row of the first table must appear →
LEFT JOIN. - Every row of both tables must appear →
FULL OUTER JOIN. - You need every combination (rare) →
CROSS JOIN.
In practice, INNER JOIN and LEFT JOIN cover the vast majority of real queries. RIGHT JOIN is almost always rewritten as a LEFT JOIN with the tables swapped.
JOIN syntax rules#
JOINon its own meansINNER JOIN;LEFT JOINmeansLEFT OUTER JOIN.- Prefix columns with their table alias (
c.name,o.amount), especially columns that exist in both tables. - The
ONcondition usually connects a foreign key to the primary key it refers to. - You can join more than two tables by adding more
JOIN ... ON ...clauses (see Multiple JOINs).
Common mistakes#
Practice#
Practice exercise
Without running anything, answer for the tables above:
- How many rows does
customers INNER JOIN ordersreturn? - How many rows does
customers LEFT JOIN ordersreturn? - Which join would you use to list every order, including order 104, with the customer's name where it exists?
Show hint
Count the matching pairs first, then add the unmatched rows that each join type keeps.
Show solution
- 3 rows: Asha's two orders and Ravi's one.
- 5 rows: the 3 matches plus Meera and John with
NULLorder columns. orders LEFT JOIN customers(or, equivalently,customers RIGHT JOIN orders). Order 104 appears with aNULLname.
Key takeaway#
Start with the most common type: INNER JOIN.
Finished this lesson?