A shop wants to send a discount coupon to everyone who signed up but never bought anything. The names are in one table and the orders are in another. Neither table alone can answer the question.

The answer comes from reading the two tables together, row against row, matched by a number they share.

See it#

Here are the shop's two tables. Every order carries the id of the customer who placed it.

customers

idname
1Asha
2Ben
3Cleo
4Dev
5Eve

orders

idcustomer_iditem
12Books
21Shoes
34Games

Tap Play to watch the same two tables combined two ways. Each customer lights up in turn, and a line runs from their id to every order that carries it.

Both results start the same: Asha with Shoes, Ben with Books, Dev with Games. The difference is Cleo and Eve, who never ordered. The first result drops them. The second keeps them, with NULL where the order would be, and those NULL rows are exactly the people who get the coupon.

Name it#

Matching rows across tables like that has a name. A join combines rows from two tables into one result by matching a column they share, usually a foreign key against the primary key it points to.

Here the shared column is the customer's id: orders.customer_id against customers.id. (Foreign keys are in the Keys and constraints lesson.) The two results you watched were an inner join and a left join.

Written in SQL, the first one is:

sql

ON says which rows pair up: a customer and an order, whenever the customer's id equals the order's customer_id. customers.id means the id column of the customers table. You need the table's name because both tables have a column called id. For a left join, write LEFT JOIN instead of INNER JOIN; nothing else changes.

There are a few more, and each answers a different question.

How it works#

For the rest of the lesson, the shop has two more orders: Asha bought a Bag too, and a guest with customer id 7, who isn't in the customers table, bought Pens. (This shop's orders table has no foreign key on customer_id, so nothing stopped it. With the foreign key from Keys and constraints, the database would refuse an order for customer 7. Many shops store a guest order with customer_id NULL instead, which a foreign key allows; every join below gives the same rows either way.)

idcustomer_iditem
12Books
21Shoes
34Games
41Bag
57Pens

1. INNER JOIN keeps only the matches#

An inner join returns a row for every pair that matches, and nothing else. Cleo and Eve have no orders, so they're gone. The guest's Pens order has no customer, so it's gone too. Asha matches two orders, so she appears twice.

nameitem
AshaShoes
AshaBag
BenBooks
DevGames

That's 4 rows. To count an inner join's rows, count the matching pairs, not the customers.

2. LEFT JOIN keeps every row of the left table#

A left join keeps every row of the first table, matched or not. A customer with no order appears once, with NULL in the order's columns. A customer with two orders still appears twice.

nameitem
AshaShoes
AshaBag
BenBooks
CleoNULL
DevGames
EveNULL

That's 6 rows: the 4 matches, plus Cleo and Eve. The guest's Pens order isn't here, because the left table is customers and the guest isn't a customer. The coupon list is the left join, keeping only the rows where the order is missing: WHERE orders.id IS NULL gives Cleo and Eve. (IS NULL is the test from SQL basics.)

3. RIGHT JOIN keeps every row of the right table#

A right join is the mirror: every row of the second table stays. Here that's every order, so the guest's Pens appears with NULL for the customer's name, and Cleo and Eve are gone.

nameitem
AshaShoes
AshaBag
BenBooks
DevGames
NULLPens

That's 5 rows. Any right join can be written as a left join with the tables swapped, so many people only ever write left joins.

4. FULL OUTER JOIN keeps every row of both#

A full outer join keeps everything: the matches, the customers with no order, and the orders with no customer, with NULLs filling whichever side is missing.

nameitem
AshaShoes
AshaBag
BenBooks
CleoNULL
DevGames
EveNULL
NULLPens

That's 7 rows. Reach for it when you need "everyone in either table, matched up where they're in both". Two coaching centres merge, each with its own class roster keyed by student id: the merged list needs everyone from either branch, matched up where a student had joined both. A left join would lose the students who were only on the second roster.

5. CROSS JOIN pairs every row with every row#

A cross join has no matching rule at all. It pairs every row of the first table with every row of the second. A shop with 3 shirt colours (Blue, Pink, Grey) and 4 sizes (S, M, L, XL) gets 3 x 4 = 12 rows, one for every shirt it could sell. Multiply the row counts; don't add them.

6. A self join reads one table twice#

Sometimes the row you need to match is in the same table. Say the shop runs a "refer a friend" offer, and its customers table gets a referred_by column holding the id of the customer who referred them:

idnamereferred_by
1AshaNULL
2Ben1
3Cleo1
4Dev2
5EveNULL

To list each customer next to the friend who referred them, use the table twice under two names, c for the customer and r for the referrer, and join c.referred_by = r.id. That's a self join. In SQL that's FROM customers c JOIN customers r ON c.referred_by = r.id. Writing a short name after a table gives it an alias, and JOIN on its own means INNER JOIN. It gives Ben with Asha, Cleo with Asha, and Dev with Ben. Asha and Eve weren't referred by anyone, so an inner self join leaves them out, and a left one keeps them with NULL.

Why it matters#

Almost every screen in an app is a join. Your Swiggy order page joins orders to restaurants to show the restaurant's name. An Instagram profile joins users to posts to likes. A bank statement joins transactions to accounts. Tables are kept separate so that each fact is stored once, and joins put them back together when someone asks.

In interviews, joins come as "how many rows does this return?" on two small tables, and as "which join would you use?". Read them the way you did above:

Interviews askThe answer you saw
What happens to an unmatched left row in a LEFT JOIN?it appears once, with NULLs in the right table's columns
How many rows does an INNER JOIN return?one per matching pair: Asha with two orders counts twice
How many rows does a CROSS JOIN of 3 and 4 rows return?12: multiply
Everyone in either table, matched where possible?FULL OUTER JOIN
A row that points at another row of the same table?a self join, with two aliases
Customers who never ordered?LEFT JOIN, then WHERE the order IS NULL

Code#

The two tables, then one query per join. Without an ORDER BY, the rows may come back in a different order than the tables above; the rows themselves are the same.

sql
sql
sql
sql

What to remember#

  • A join matches rows from two tables on a shared column, usually a foreign key against a primary key.
  • INNER keeps only matches. LEFT keeps every left row, with NULLs where there's no match. RIGHT does the same for the right table. FULL OUTER keeps both sides.
  • Count rows by matching pairs: a row that matches two rows appears twice.
  • CROSS JOIN multiplies: 3 rows by 4 rows is 12. A self join reads one table twice, under two aliases.