SQL JOIN: combining tables, explained and practiced
A database keeps customers, orders and products in separate tables. A join puts them back together for a query: the customer name next to each order, the product next to each order line. This is the point where SQL starts doing things a single spreadsheet can't.
In short: SELECT o.order_id, c.name FROM orders AS o INNER JOIN customers AS c ON o.customer_id = c.customer_id; pairs each order with the customer that has the same customer_id. The ON clause says which rows belong together. o and c are aliases, short names for the tables.
What you should know first: picking columns with SELECT and filtering rows with WHERE.
Why the data lives in several tables
The orders table stores only a customer_id, not the name. The name lives once, in customers, so a renamed business means updating one row instead of every order. For a readable list, a join reassembles the pieces through the shared column, customer_id.
Try it now
Both tables are shown above the editor. Join them on customer_id.
Task The orders table only stores a customer_id, but customer service wants to see names. For every order, show order_id and order_date from orders and the customer's name from customers.
orders 18 rows, 5 columns
| order_id | customer_id | order_date | status | employee_id |
|---|---|---|---|---|
| 1001 | 1 | 2026-01-08 | delivered | 3 |
| 1002 | 4 | 2026-01-12 | delivered | 4 |
| 1003 | 3 | 2026-01-20 | delivered | 3 |
| 1004 | 7 | 2026-02-03 | delivered | 4 |
| 1005 | 2 | 2026-02-11 | cancelled | 8 |
| 1006 | 1 | 2026-02-25 | delivered | 3 |
| 1007 | 10 | 2026-03-04 | delivered | 2 |
| 1008 | 5 | 2026-03-17 | delivered | 4 |
| 1009 | 8 | 2026-03-30 | delivered | 3 |
| 1010 | 6 | 2026-04-07 | delivered | 8 |
| 1011 | 12 | 2026-04-22 | delivered | 4 |
| 1012 | 3 | 2026-05-06 | delivered | 3 |
| 1013 | 9 | 2026-05-19 | cancelled | 8 |
| 1014 | 7 | 2026-06-02 | shipped | 4 |
| 1015 | 1 | 2026-06-10 | shipped | 3 |
| 1016 | 11 | 2026-06-12 | open | NULL |
| 1017 | 4 | 2026-06-13 | open | NULL |
| 1018 | 10 | 2026-06-14 | open | NULL |
customers 12 rows, 7 columns
| customer_id | name | city | country | customer_since | segment | phone |
|---|---|---|---|---|---|---|
| 1 | Harbor Fremont Partners | Boston | USA | 2021-03-15 | Business | 617-555-0142 |
| 2 | Emily Carter | Chicago | USA | 2022-07-01 | Consumer | NULL |
| 3 | Riverside County Office | Riverside | USA | 2020-11-20 | Government | 951-555-0110 |
| 4 | Raj Patel | Leeds | UK | 2023-01-09 | Consumer | 0113 496 0123 |
| 5 | Lakeview Hotel Ltd | Manchester | UK | 2022-04-18 | Business | 0161 496 0456 |
| 6 | Olivia Brooks | Austin | USA | 2024-02-27 | Consumer | NULL |
| 7 | Greene & Shaw LLP | London | UK | 2021-09-05 | Business | 020 7946 0321 |
| 8 | Maple Grove High School | Austin | USA | 2023-08-14 | School | 512-555-0187 |
| 9 | Chloe Evans | Boston | USA | 2025-05-30 | Consumer | 617-555-0199 |
| 10 | Northline Printing Co | Toronto | Canada | 2020-06-12 | Business | 416-555-0133 |
| 11 | Daniel Kim | Seattle | USA | 2024-10-03 | Consumer | NULL |
| 12 | Westbrook Dental Clinic | Chicago | USA | 2022-12-01 | Business | 312-555-0164 |
How INNER JOIN works
The database takes each row of orders and looks in customers for every row where the ON condition is true. Each matching pair becomes one row of the result, with columns from both tables. WHERE and ORDER BY then work on that combined row as usual:
SELECT o.order_id, o.order_date, c.name, c.city
FROM orders AS o
INNER JOIN customers AS c ON o.customer_id = c.customer_id
WHERE o.order_date >= '2026-06-01';
| order_id | order_date | name | city |
|---|---|---|---|
| 1014 | 2026-06-02 | Greene & Shaw LLP | London |
| 1015 | 2026-06-10 | Harbor Fremont Partners | Boston |
| 1016 | 2026-06-12 | Daniel Kim | Seattle |
| 1017 | 2026-06-13 | Raj Patel | Leeds |
| 1018 | 2026-06-14 | Northline Printing Co | Toronto |
"Inner" means only rows that found a partner make it into the result. An order without a matching customer, or a customer without orders, is left out. A customer with several orders shows up several times, once per order.
Aliases and table prefixes
orders AS o gives the table the short name o; the AS is optional. Writing o.order_date says which table a column comes from. You must do this whenever a column exists in both tables, like customer_id. Leave the prefix off and the exercise tells you: "The column "customer_id" exists in more than one table." SQLite's own wording is ambiguous column name. Prefixing every column in a join is a good habit anyway.
Practice: a join plus WHERE
This time you filter after joining. Watch out for the column that both tables share.
Task A sales rep is visiting Boston next week and needs every order from Boston customers: customer_id, name, order_id and order_date.
customers 12 rows, 7 columns
| customer_id | name | city | country | customer_since | segment | phone |
|---|---|---|---|---|---|---|
| 1 | Harbor Fremont Partners | Boston | USA | 2021-03-15 | Business | 617-555-0142 |
| 2 | Emily Carter | Chicago | USA | 2022-07-01 | Consumer | NULL |
| 3 | Riverside County Office | Riverside | USA | 2020-11-20 | Government | 951-555-0110 |
| 4 | Raj Patel | Leeds | UK | 2023-01-09 | Consumer | 0113 496 0123 |
| 5 | Lakeview Hotel Ltd | Manchester | UK | 2022-04-18 | Business | 0161 496 0456 |
| 6 | Olivia Brooks | Austin | USA | 2024-02-27 | Consumer | NULL |
| 7 | Greene & Shaw LLP | London | UK | 2021-09-05 | Business | 020 7946 0321 |
| 8 | Maple Grove High School | Austin | USA | 2023-08-14 | School | 512-555-0187 |
| 9 | Chloe Evans | Boston | USA | 2025-05-30 | Consumer | 617-555-0199 |
| 10 | Northline Printing Co | Toronto | Canada | 2020-06-12 | Business | 416-555-0133 |
| 11 | Daniel Kim | Seattle | USA | 2024-10-03 | Consumer | NULL |
| 12 | Westbrook Dental Clinic | Chicago | USA | 2022-12-01 | Business | 312-555-0164 |
orders 18 rows, 5 columns
| order_id | customer_id | order_date | status | employee_id |
|---|---|---|---|---|
| 1001 | 1 | 2026-01-08 | delivered | 3 |
| 1002 | 4 | 2026-01-12 | delivered | 4 |
| 1003 | 3 | 2026-01-20 | delivered | 3 |
| 1004 | 7 | 2026-02-03 | delivered | 4 |
| 1005 | 2 | 2026-02-11 | cancelled | 8 |
| 1006 | 1 | 2026-02-25 | delivered | 3 |
| 1007 | 10 | 2026-03-04 | delivered | 2 |
| 1008 | 5 | 2026-03-17 | delivered | 4 |
| 1009 | 8 | 2026-03-30 | delivered | 3 |
| 1010 | 6 | 2026-04-07 | delivered | 8 |
| 1011 | 12 | 2026-04-22 | delivered | 4 |
| 1012 | 3 | 2026-05-06 | delivered | 3 |
| 1013 | 9 | 2026-05-19 | cancelled | 8 |
| 1014 | 7 | 2026-06-02 | shipped | 4 |
| 1015 | 1 | 2026-06-10 | shipped | 3 |
| 1016 | 11 | 2026-06-12 | open | NULL |
| 1017 | 4 | 2026-06-13 | open | NULL |
| 1018 | 10 | 2026-06-14 | open | NULL |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
Types of joins at a glance
Besides INNER JOIN there are joins that also keep rows without a partner. The left table is the one after FROM, the right table the one after JOIN. The shaded area shows which rows end up in the result:
To see what that means in rows, join employees (left) and orders (right) with ON o.employee_id = e.employee_id. Six employees haven't handled any order, and three open orders have nobody assigned yet:
| Join type | What you get | Rows |
|---|---|---|
INNER JOIN (or just JOIN) | only pairs that match | 15 |
LEFT JOIN | every left row; NULLs on the right where there's no match | 21 (15 + 6 employees) |
RIGHT JOIN | every right row; NULLs on the left where there's no match | 18 (15 + 3 orders) |
FULL JOIN | every row from both sides | 24 (15 + 6 + 3) |
CROSS JOIN | every left row with every right row, no ON | 180 (10 times 18) |
Day to day, you'll mostly use INNER and LEFT JOIN. Any RIGHT JOIN can be written as a LEFT JOIN by swapping the tables. LEFT JOIN gets the next lesson, joins across more than two tables the one after. Joining a table to itself, say employees to their managers, is a self join.
One more: comparing columns from both tables
After a join, values from two tables sit in the same row, so you can compare them directly.
Task The sales manager wants to know where items were sold below list price. Show every order line whose unit_price is lower than the product's current price: order_id, the product name, price and unit_price.
order_items 38 rows, 4 columns
| order_id | product_id | quantity | unit_price |
|---|---|---|---|
| 1001 | 1 | 40 | 4.79 |
| 1001 | 3 | 25 | 2.79 |
| 1001 | 6 | 4 | 24.9 |
| 1002 | 2 | 2 | 3.49 |
| 1002 | 10 | 3 | 5.49 |
| 1003 | 1 | 60 | 4.69 |
| 1003 | 11 | 6 | 84.9 |
| 1003 | 3 | 50 | 2.59 |
| 1004 | 4 | 3 | 179 |
| 1004 | 9 | 3 | 269 |
| 1005 | 5 | 1 | 449 |
| 1006 | 1 | 30 | 4.79 |
| 1006 | 7 | 10 | 6.99 |
| 1007 | 8 | 2 | 219 |
| 1007 | 11 | 4 | 89.9 |
| 1007 | 1 | 20 | 4.99 |
| 1008 | 4 | 6 | 175 |
| 1008 | 2 | 5 | 3.49 |
| 1009 | 10 | 120 | 4.99 |
| 1009 | 2 | 40 | 3.19 |
| 1009 | 9 | 2 | 279 |
| 1010 | 7 | 2 | 6.99 |
| 1010 | 2 | 1 | 3.49 |
| 1011 | 12 | 1 | 129 |
| 1011 | 1 | 10 | 4.99 |
| 1011 | 3 | 12 | 2.79 |
| 1012 | 6 | 8 | 23.9 |
| 1012 | 1 | 40 | 4.69 |
| 1013 | 9 | 1 | 279 |
| 1014 | 5 | 2 | 439 |
| 1014 | 4 | 2 | 179 |
| 1015 | 1 | 50 | 4.79 |
| 1015 | 11 | 2 | 89.9 |
| 1016 | 10 | 4 | 5.49 |
| 1017 | 2 | 3 | 3.49 |
| 1017 | 7 | 2 | 6.99 |
| 1018 | 1 | 80 | 4.79 |
| 1018 | 6 | 10 | 24.9 |
products 12 rows, 5 columns
| product_id | name | category | price | stock |
|---|---|---|---|---|
| 1 | Copy paper, 500 sheets | Paper | 4.99 | 420 |
| 2 | Blue ballpoint pens, 10-pack | Writing | 3.49 | 310 |
| 3 | Lever arch file, gray | Filing | 2.79 | 185 |
| 4 | Comfort office chair | Furniture | 189 | 12 |
| 5 | Standing desk | Furniture | 449 | 5 |
| 6 | Black ink cartridge | Printer supplies | 24.9 | 64 |
| 7 | Yellow sticky notes, 12 pads | Writing | 6.99 | 0 |
| 8 | Mono laser printer L30 | Electronics | 219 | 8 |
| 9 | 27-inch monitor | Electronics | 279 | 15 |
| 10 | Notebook A5, ruled | Paper | 5.49 | 140 |
| 11 | Black toner XL | Printer supplies | 89.9 | 22 |
| 12 | Cross-cut shredder | Electronics | 129 | 0 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
JOIN vs. VLOOKUP
If you come from Excel, you already know the idea behind VLOOKUP and XLOOKUP: take an ID and fetch a value from another table. A join does the same job with three differences. VLOOKUP returns the first match only; a join returns every matching row. VLOOKUP brings back one column per formula; a join brings every column you list in SELECT. And where VLOOKUP shows #N/A, INNER JOIN drops the row, while LEFT JOIN keeps it with NULLs.
Common mistakes
Forgetting ON
In SQLite and MySQL, FROM orders JOIN customers without ON pairs every order with every customer. In the first exercise you'd see "Your query returns 216 rows. Wanted: 18 rows." A multiplied row count usually means a missing join condition.
Column without a table prefix
SELECT customer_id, name … fails when customer_id is in both tables. The exercise says "The column "customer_id" exists in more than one table." Write c.customer_id.
Table name after defining an alias
Once you've written orders AS o, only o works. orders.order_date gets you a message that the column exists, but not in the table before the dot.
Joining the wrong columns
ON o.order_id = c.customer_id compares order numbers from 1001 up with customer numbers up to 12. Nothing matches, and the exercise reports "Your query returns no rows." Always join the column that points to the other table with that table's key.
Comma joins
FROM orders, customers WHERE … is the older style and returns the same rows. The exercises in this lesson ask for JOIN, because it keeps the join condition in ON apart from the filter in WHERE.
JOIN in MySQL, PostgreSQL, SQL Server and SQLite
INNER JOIN with ON works the same everywhere, and a bare JOIN means INNER JOIN in all four. The differences are in the rarer types:
| Database | RIGHT JOIN | FULL JOIN | USING (column) |
|---|---|---|---|
| MySQL | yes; the manual recommends LEFT JOIN for portable code | not in the syntax | yes |
| PostgreSQL | yes | yes | yes |
| SQL Server | yes | yes | not in the syntax, ON only |
| SQLite (these exercises) | from version 3.39.0 (2022) | from version 3.39.0 | yes |
According to the MySQL manual, JOIN, INNER JOIN and CROSS JOIN are interchangeable there, so even CROSS JOIN with ON works. In PostgreSQL the word INNER is optional, and so is OUTER in LEFT, RIGHT and FULL joins.
FAQ
Is JOIN the same as INNER JOIN?
Yes. A join without a type is an inner join in all four databases. Writing INNER just makes the query more explicit.
Does the order of the tables matter?
Not for INNER JOIN: orders JOIN customers returns the same rows as customers JOIN orders. For LEFT and RIGHT JOIN it decides which table is kept in full.
What's the difference between ON and WHERE?
ON says which rows belong together, WHERE filters the result. With an inner join, moving a condition from one to the other gives the same rows. With a left join it doesn't, as the LEFT JOIN lesson shows.
Why do some rows appear twice?
Because they have more than one partner. A customer with three orders appears three times. That's correct as long as you want to list orders. How to avoid counting things twice when you summarize is covered in joining multiple tables.
How many tables can I join?
As many as you need: each extra JOIN with its own ON adds a table. That's the lesson after next.