What this page is for

Intent: how two tables are joined, why ON and WHERE differ, and how to find rows with no match.
You’ll learn: inner vs left join · ON vs WHERE · the NULL trap · NOT EXISTS · duplicates.
Time: about 8 minutes. The detailed Joins page repeats each section with every step table.

Words you need first

  • Table: rows and columns, like a spreadsheet.
  • Primary key: the column that names each row once, such as customers.id. It is never NULL.
  • Foreign key: a column that holds another table’s primary key. orders.customer_id points at customers.id.
  • Join: combines rows of two tables where a condition matches.
  • NULL: “no value”. Not zero, not an empty text. Test it with IS NULL, because = NULL never matches.
  • Inner join: keeps only the pairs that match on both sides.
  • Left join: keeps every row of the left table (written first). Missing right-side values are NULL.
  • Anti-join: rows of one table with no match in another, such as “never ordered”.
  • RIGHT JOIN keeps every row of the right table. FULL OUTER JOIN keeps every row of both. Both are rare. A right join is a left join with the tables swapped.

Why it exists

A join puts split tables back together for one question.

The picture

Customers matched to orders: Arjun has no line, so an inner join drops him and a left join keeps him with NULL

How it works, with an example

Simply: A join combines rows from two tables when a column matches. Each order holds its customer’s id, which is a foreign key: a column that stores another table’s key. Think of a student card that names its class. Many students share one class, so the link sits on the student side.

What to know: A join pairs rows where its ON condition is true. Here that condition is customers.id = orders.customer_id.

Only the columns joins need are shown.

customers

idname
1Riya
2Kabir
3Meera
4Arjun

restaurants

idname
1Spice Hub
2Dosa Den
3Burger Bay

orders

idcustomer_idrestaurant_idstatuscreated_at
10111delivered2026-10-28 13:05
10222delivered2026-10-28 20:40
10313cancelled2026-10-29 19:15
10431delivered2026-10-30 12:30
10511delivered2026-10-31 21:10

The link is on the order: customer_id. The customer row has no order column. Many orders point at one customer, so the foreign key sits on the many side.

The most common mistake

Two slips that cost points

  • Writing customers.orderId. The link is orders.customer_id.
  • Thinking WHERE removes rows with no join partner. The join does that. WHERE only sees rows the join kept.

Inner join and WHERE: two filters

Simply: Think of a bouncer at a club door. The join is the bouncer: it lets in only rows that have a partner in the other table, and turns away the rest. WHERE is a second check on the rows already inside. Taking WHERE away would not let back in the rows the bouncer refused.

What to know: The join runs first, using its ON condition, and drops rows with no partner. WHERE runs after the join and only checks the rows the join kept, so it can never bring back a dropped row.

Example: every delivered order, with names, newest first.

SQL

SELECT o.id AS order_id, c.name AS customer, r.name AS restaurant, o.created_at
FROM orders o
JOIN customers c   ON c.id = o.customer_id
JOIN restaurants r ON r.id = o.restaurant_id
WHERE o.status = 'delivered'
ORDER BY o.created_at DESC;

Drizzle

db.select({ orderId: orders.id, customer: customers.name, restaurant: restaurants.name, createdAt: orders.createdAt })
  .from(orders)
  .innerJoin(customers, eq(customers.id, orders.customerId))
  .innerJoin(restaurants, eq(restaurants.id, orders.restaurantId))
  .where(eq(orders.status, 'delivered'))
  .orderBy(desc(orders.createdAt));

After the join (5 rows). Arjun is missing, because he has no order:

ordercustomerrestaurantstatus
101RiyaSpice Hubdelivered
102KabirDosa Dendelivered
103RiyaBurger Baycancelled
104MeeraSpice Hubdelivered
105RiyaSpice Hubdelivered

Result, after WHERE removes 103 (4 rows):

ordercustomerrestaurantcreated_at
105RiyaSpice Hub2026-10-31 21:10
104MeeraSpice Hub2026-10-30 12:30
102KabirDosa Den2026-10-28 20:40
101RiyaSpice Hub2026-10-28 13:05

Why this, not that:

  • The join drops a row with no partner (Arjun). WHERE drops a row that fails a condition (103, which is cancelled, though it has a partner).

Trigger card

When I need only rows with a partner in both tables, I use JOIN, because a row with no partner has nothing to pair with.

Left join and anti-join: who never ordered

Simply: Think of a class register. Every student has a line, even when absent, and an absent student gets a blank called NULL. A left join works the same way: it keeps every row of the left table, with NULL where nothing matches. To find the rows with no match, look for the blanks, which means keeping the rows where the other table’s id IS NULL.

What to know: A left join keeps every row of the left table and fills the right side with NULL where nothing matches. Test the right table’s id, such as orders.id IS NULL, and put the match in ON, because a right-table filter in WHERE turns the left join into an inner join.

Example: customers who never ordered.

SQL

SELECT c.id, c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULL;

Drizzle

db.select({ id: customers.id, name: customers.name })
  .from(customers)
  .leftJoin(orders, eq(orders.customerId, customers.id))
  .where(isNull(orders.id));

After the left join (6 rows). Riya appears three times, once per order:

c.idnameo.id
1Riya101
1Riya103
1Riya105
2Kabir102
3Meera104
4ArjunNULL

Result, after WHERE o.id IS NULL: 4 · Arjun.

Why this, not that:

  • LEFT JOIN + IS NULL works on any data. NOT IN (WHERE id NOT IN (SELECT customer_id FROM orders)) returns no rows at all if the list has one NULL, because 4 <> NULL is unknown. NOT EXISTS (WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id)) is NULL-safe.
  • Test the right table’s id (orders.id). Only the joined side gets NULLs. Testing customers.id finds nothing.
  • Match in ON, not WHERE. A right-table filter in WHERE turns a left join into an inner join: WHERE o.status = 'delivered' removes Arjun, whose NULL status fails.

Trigger card

When a question says “never”, “no”, or “even with none”, I left join from the table that must keep every row, then keep the rows where the other table’s id IS NULL.

Say it in an interview

“A join combines rows from two tables where a condition matches, usually a foreign key on the many side. An inner join keeps only matched pairs. A left join keeps every row of the left table and fills the right side with NULL. To find rows with no match, I left join and keep the rows where the right table’s key IS NULL. The match goes in ON, and a right-table filter in WHERE turns the left join into an inner join. I use NOT EXISTS rather than NOT IN, because one NULL makes NOT IN return nothing.”

Questions interviewers ask

Tap a question to see the answer.

Mental map

Joins mental map

Watch

Socratica, about 10 minutes: an overview of SQL joins and the basic join types.

Want more? The detailed Joins page adds every step table, the wrong versions and harder questions. Next: Aggregates: GROUP BY and HAVING.


← Transactions and concurrency · Aggregates: GROUP BY and HAVING →