Loading…
Loading…
Charting your lesson…
Opening your lesson…
SQL Joins
Lesson 2 of 4
6 steps•about 12 min•12 flashcards
Step 1/ 61 min
Joins are how relational databases reassemble the data that normalization split across tables. Choosing the right join type — and predicting exactly which rows survive — is one of the most practical and heavily tested SQL skills.
Core idea: a join combines rows from two tables based on a related column. INNER JOIN keeps only matching rows; LEFT/RIGHT OUTER JOIN keeps all rows from one side (filling non-matches with NULL); FULL OUTER JOIN keeps all rows from both; CROSS JOIN produces the Cartesian product.
Step 2/ 61 min
SELECT e.name, d.dept_name
FROM Employee e
JOIN Department d ON e.dept_id = d.id; -- only employees WITH a matching dept
The ON condition is the match rule. An INNER JOIN returns a row only when both sides match — unmatched rows on either side disappear.
SELECT e.name, d.dept_name
FROM Employee e
LEFT JOIN Department d ON e.dept_id = d.id; -- unmatched employees appear with NULL dept
The classic use of LEFT JOIN + WHERE d.id IS NULL is to find rows with no match (employees in no department).
ON). Rarely intended; usually the result of forgetting a join condition.Employee(id, manager_id) table.SELECT e.name AS emp, m.name AS manager
FROM Employee e JOIN Employee m ON e.manager_id = m.id;
NULL gotcha: a join predicate e.dept_id = d.id never matches when dept_id is NULL (NULL = anything is unknown, not true), so those rows drop from an INNER JOIN.
Step 3/ 61 min
Employee: (Ann, dept 1), (Bob, dept 2), (Cy, dept NULL). Department: (1, HR), (2, IT).
-- INNER JOIN e.dept_id = d.id
Ann | HR
Bob | IT -> 2 rows (Cy dropped: NULL dept has no match)
-- LEFT JOIN e.dept_id = d.id
Ann | HR
Bob | IT
Cy | NULL -> 3 rows (Cy kept, dept columns NULL)
The difference is Cy: an INNER JOIN discards the unmatched row, a LEFT JOIN preserves it with NULLs. Predicting these counts — especially remembering that NULL keys never match — is the graded skill.
Step 4/ 61 min
Very common: "difference between INNER and OUTER joins", "how do you find rows with no match" (LEFT JOIN + IS NULL), "what does a CROSS JOIN produce", and reading query output. A frequent trap: a query that accidentally becomes a Cartesian product because the join condition was omitted or wrong.
Step 5/ 61 min
ON silently yields a CROSS JOIN (row explosion) — always check row counts.Filtering an outer join's optional (NULL-filled) side in the WHERE clause and accidentally turning it back into an inner join. Writing LEFT JOIN Department d ON e.dept_id = d.id WHERE d.status = 'active' discards the NULL-filled unmatched rows (NULL = 'active' is unknown, never true). To preserve them, put the extra condition in the ON clause, not WHERE.
Step 6/ 6· Worked examples2 min
Problem
Given Employees(id, name, dept_id) and Departments(dept_id, dname), with one employee (Zoe) having dept_id = NULL and one department (Legal) having no employees, write and compare the results of an INNER JOIN and a LEFT JOIN, and show how to list departments with no employees.
Employees: (1,Amy,10), (2,Ben,20), (3,Zoe,NULL)
Departments: (10,Sales), (20,Tech), (30,Legal)
Try it on paper first.
Recall
Keep this
SQL joins are the most-tested SQL topic in placement rounds — expect "types of joins", "INNER vs LEFT JOIN", and query-writing tasks. Knowing exactly which rows and NULLs each join produces is the core skill.
| Join | Returns |
|---|---|
| INNER | only rows matching in both tables |
| LEFT (OUTER) | all left rows + matching right (NULLs where no match) |
| RIGHT (OUTER) | all right rows + matching left (NULLs where no match) |
| FULL OUTER | all rows from both (NULLs where no match) |
| CROSS | Cartesian product (every left × every right) |
| SELF | a table joined to itself |
An INNER JOIN drops unmatched rows on both sides. A LEFT JOIN keeps every left row, filling right-side columns with NULL when there's no match — ideal for "find records with no matching child" (add WHERE right.id IS NULL). A CROSS JOIN of m×n rows produces m·n rows.
JOIN with no qualifier means INNER JOIN by default in SQL.ON."Types of joins / difference between INNER and LEFT", "write a query to find unmatched rows", "what does CROSS JOIN produce?", "what is a self join?", and predicting the row/NULL output of a given join.
INNER = matches only; LEFT/RIGHT = all of one side + NULLs; FULL OUTER = all rows both sides; CROSS = Cartesian (m×n); SELF = table to itself. Use LEFT JOIN + IS NULL for anti-joins. Unqualified JOIN = INNER. Keep filters on the optional (right-hand) table in ON, not WHERE.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.