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:
- Write a join with
ONand table aliases. - Predict which rows
INNER JOINandLEFT JOINkeep, and where NULLs appear. - Choose which table goes on the left.
- Solve LeetCode 1378 alone.
Four words you need
ON s.id = l.student_id. A pair of rows is joined when it's TRUE.FROM. LEFT JOIN protects its rows.JOIN means the same.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
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
- The left table is the one you must not lose. Ask: "Which table should have all its rows in the answer?" Put that table after
FROM, andLEFT JOINthe other one. MySQL recommendsLEFT JOINoverRIGHT JOIN(MySQL manual), so you never need RIGHT. - Prefix your columns. If both tables have an
idcolumn, plainSELECT idfails with "Column 'id' in field list is ambiguous". Writes.idorl.student_id. - The NULL comes from the join, not the data. Bo's row in
Lockersdoesn't exist. LEFT JOIN fills the gap with NULL. You'll use this in the next lesson to find rows that have no partner. - Joins can multiply rows. If Ana had two lockers, Ana would appear twice. Always ask: "Can one left row match many right rows?"
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:
- Your query, and the LeetCode result.
- One sentence, from memory: why LEFT JOIN and not INNER JOIN? Name one employee from the LeetCode example who would disappear with INNER JOIN.
To pass (check these yourself first):
- LeetCode says Accepted.
Employeesis the left table, because every employee must appear.- Your sentence names a real employee from the example.
Read next
Main source: SQLBolt Lesson 7: OUTER JOINs. It's short and interactive. Do the exercises, and look at which rows get NULLs.
- SQLBolt Lesson 6: Multi-table queries with JOINs 🛠 Interactive · 10 min · INNER JOIN with instant feedback.
- SQLBolt Lesson 7: OUTER JOINs 🛠 Interactive · 10 min · LEFT/RIGHT/FULL joins. Notice where NULLs appear.
- JOIN Clause (MySQL 8.0 manual) 📄 Docs · skim · Find the bullet that starts "If there is no matching row for the right table". It's the LEFT JOIN rule, and its example is next lesson's trick.
💬 Is something still unclear? Ask me in the chat. I'm your tutor for this course.