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_idcustomer_idorder_datestatusemployee_id
100112026-01-08delivered3
100242026-01-12delivered4
100332026-01-20delivered3
100472026-02-03delivered4
100522026-02-11cancelled8
100612026-02-25delivered3
1007102026-03-04delivered2
100852026-03-17delivered4
100982026-03-30delivered3
101062026-04-07delivered8
1011122026-04-22delivered4
101232026-05-06delivered3
101392026-05-19cancelled8
101472026-06-02shipped4
101512026-06-10shipped3
1016112026-06-12openNULL
101742026-06-13openNULL
1018102026-06-14openNULL
customers 12 rows, 7 columns
customer_idnamecitycountrycustomer_sincesegmentphone
1Harbor Fremont PartnersBostonUSA2021-03-15Business617-555-0142
2Emily CarterChicagoUSA2022-07-01ConsumerNULL
3Riverside County OfficeRiversideUSA2020-11-20Government951-555-0110
4Raj PatelLeedsUK2023-01-09Consumer0113 496 0123
5Lakeview Hotel LtdManchesterUK2022-04-18Business0161 496 0456
6Olivia BrooksAustinUSA2024-02-27ConsumerNULL
7Greene & Shaw LLPLondonUK2021-09-05Business020 7946 0321
8Maple Grove High SchoolAustinUSA2023-08-14School512-555-0187
9Chloe EvansBostonUSA2025-05-30Consumer617-555-0199
10Northline Printing CoTorontoCanada2020-06-12Business416-555-0133
11Daniel KimSeattleUSA2024-10-03ConsumerNULL
12Westbrook Dental ClinicChicagoUSA2022-12-01Business312-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_idorder_datenamecity
10142026-06-02Greene & Shaw LLPLondon
10152026-06-10Harbor Fremont PartnersBoston
10162026-06-12Daniel KimSeattle
10172026-06-13Raj PatelLeeds
10182026-06-14Northline Printing CoToronto

"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_idnamecitycountrycustomer_sincesegmentphone
1Harbor Fremont PartnersBostonUSA2021-03-15Business617-555-0142
2Emily CarterChicagoUSA2022-07-01ConsumerNULL
3Riverside County OfficeRiversideUSA2020-11-20Government951-555-0110
4Raj PatelLeedsUK2023-01-09Consumer0113 496 0123
5Lakeview Hotel LtdManchesterUK2022-04-18Business0161 496 0456
6Olivia BrooksAustinUSA2024-02-27ConsumerNULL
7Greene & Shaw LLPLondonUK2021-09-05Business020 7946 0321
8Maple Grove High SchoolAustinUSA2023-08-14School512-555-0187
9Chloe EvansBostonUSA2025-05-30Consumer617-555-0199
10Northline Printing CoTorontoCanada2020-06-12Business416-555-0133
11Daniel KimSeattleUSA2024-10-03ConsumerNULL
12Westbrook Dental ClinicChicagoUSA2022-12-01Business312-555-0164
orders 18 rows, 5 columns
order_idcustomer_idorder_datestatusemployee_id
100112026-01-08delivered3
100242026-01-12delivered4
100332026-01-20delivered3
100472026-02-03delivered4
100522026-02-11cancelled8
100612026-02-25delivered3
1007102026-03-04delivered2
100852026-03-17delivered4
100982026-03-30delivered3
101062026-04-07delivered8
1011122026-04-22delivered4
101232026-05-06delivered3
101392026-05-19cancelled8
101472026-06-02shipped4
101512026-06-10shipped3
1016112026-06-12openNULL
101742026-06-13openNULL
1018102026-06-14openNULL

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:

INNER JOIN: only the overlap. LEFT JOIN: the whole left table. RIGHT JOIN: the whole right table. FULL JOIN: both tables in full. INNER JOINLEFT JOINRIGHT JOINFULL JOIN

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 typeWhat you getRows
INNER JOIN (or just JOIN)only pairs that match15
LEFT JOINevery left row; NULLs on the right where there's no match21 (15 + 6 employees)
RIGHT JOINevery right row; NULLs on the left where there's no match18 (15 + 3 orders)
FULL JOINevery row from both sides24 (15 + 6 + 3)
CROSS JOINevery left row with every right row, no ON180 (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_idproduct_idquantityunit_price
10011404.79
10013252.79
10016424.9
1002223.49
10021035.49
10031604.69
100311684.9
10033502.59
100443179
100493269
100551449
10061304.79
10067106.99
100782219
100711489.9
10071204.99
100846175
1008253.49
1009101204.99
10092403.19
100992279
1010726.99
1010213.49
1011121129
10111104.99
10113122.79
10126823.9
10121404.69
101391279
101452439
101442179
10151504.79
101511289.9
10161045.49
1017233.49
1017726.99
10181804.79
101861024.9
products 12 rows, 5 columns
product_idnamecategorypricestock
1Copy paper, 500 sheetsPaper4.99420
2Blue ballpoint pens, 10-packWriting3.49310
3Lever arch file, grayFiling2.79185
4Comfort office chairFurniture18912
5Standing deskFurniture4495
6Black ink cartridgePrinter supplies24.964
7Yellow sticky notes, 12 padsWriting6.990
8Mono laser printer L30Electronics2198
927-inch monitorElectronics27915
10Notebook A5, ruledPaper5.49140
11Black toner XLPrinter supplies89.922
12Cross-cut shredderElectronics1290

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:

DatabaseRIGHT JOINFULL JOINUSING (column)
MySQLyes; the manual recommends LEFT JOIN for portable codenot in the syntaxyes
PostgreSQLyesyesyes
SQL Serveryesyesnot in the syntax, ON only
SQLite (these exercises)from version 3.39.0 (2022)from version 3.39.0yes

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.

Up next: LEFT JOIN

INNER JOIN drops everything without a partner. LEFT JOIN keeps every customer, including those with no orders, and lets you find exactly the ones who have never bought anything.

Practice LEFT JOIN

Write to us

Choose language

Select your preferred language