Lesson 5 · Milestone: Join + group

INNER JOIN vs LEFT JOIN

Real data is split across tables: names in one, lockers in another, orders in a third. A join glues rows from two tables together when a condition matches. The main interview question is always the same: what happens to a row that has no partner?

What you will be able to do:

Four words you need

ONThe match test: ON s.id = l.student_id. A pair of rows is joined when it's TRUE.
left tableThe table written after FROM. LEFT JOIN protects its rows.
INNER JOINKeeps only rows that found a partner. Plain JOIN means the same.
LEFT JOINKeeps every left row. If it has no partner, the right columns become NULL.

1. The mental model: every left row looks for partners

For each row of the left table, the database looks through the right table for rows where ON is TRUE. Then:

found 1 partner    →  1 joined row
found 2 partners   →  2 joined rows   (the left row is repeated!)
found 0 partners   →  INNER JOIN: row disappears
                      LEFT JOIN:  row stays, right columns = NULL

In the run order, the join is part of step 1. FROM … JOIN … builds one combined table before WHERE, GROUP BY and SELECT run:

1. FROM + JOIN  →  2. WHERE  →  3. GROUP BY  →  4. SELECT  →  5. ORDER BY

Click through the steps. The example uses a school's lockers, not the exercise tables:

SELECT s.name, l.locker_no
FROM Students s                       -- s = alias, the LEFT table
LEFT JOIN Lockers l                   -- l = alias, the right table
  ON s.id = l.student_id;             -- the match test
Students s (left) id name 1  Ana 2  Bo 3  Caio 4  Dee Lockers l (right) student_id locker 1          A12 3          B07 9          C01 Bo, Dee: no partner locker 9: no student ON s.id = l.student_id finds 2 pairs: Ana–A12, Caio–B07 INNER JOIN name locker_no Ana  A12 Caio B07 only rows with a partner LEFT JOIN name locker_no Ana  A12 Bo   NULL Caio B07 Dee  NULL every student stays; no partner → NULL locker 9 is still gone: it is on the RIGHT side

Two tables. Students is written after FROM, so it is the left table. Lockers is the right table.

Each student looks for lockers where s.id = l.student_id. Ana and Caio find one each. Bo and Dee find none. Locker 9 belongs to a student who isn't in the table.

INNER JOIN keeps only the pairs. Bo and Dee disappear, and so does locker 9.

LEFT JOIN keeps every left row. Bo and Dee stay, with NULL in the locker column. Locker 9 is still dropped, because LEFT JOIN only protects the left table.

2. Rules that cost points in interviews

Practice from memory

Try not to scroll up.

1. Students INNER JOIN Lockers: what happens to a student with no locker?

2. Students LEFT JOIN Lockers: the same student?

3. Students LEFT JOIN Lockers: what happens to a locker whose student is missing?

4. Which table is the "left" table?

5. Ana has two lockers. With INNER JOIN, how often is Ana in the result?

6. Both tables have a column id. You write SELECT id …. What happens?

Fill in the blanks: a different table

Customers(id, name) and Phones(customer_id, phone). List every customer with their phone. Customers with no phone must still appear.

SELECT c.name, p.phone
FROM  c
 Phones p
   c.id = p.;

🏆 Challenge: LeetCode 1378 (Replace Employee ID With The Unique Identifier)

Your task: open 1378 and choose MySQL. Tables: Employees(id, name) and EmployeeUNI(id, unique_id). Show each employee's unique_id and name. If an employee has no unique id, show null. Time box: 8 minutes.

Send me in the chat:

To pass (check these yourself first):

Read next

Main source: SQLBolt Lesson 7: OUTER JOINs. It's short and interactive. Do the exercises, and look at which rows get NULLs.

💬 Is something still unclear? Ask me in the chat. I'm your tutor for this course.