Q1 What is the difference between an INNER JOIN and a LEFT JOIN?
Show the answer Hide the answer
An inner join keeps only rows that match on both sides; a left join keeps every row from the left table, filling the right side with NULLs when nothing matches.
Here, Legal has no employees. The left join below still lists it, with a count of zero. Swap LEFT JOIN for JOIN and Legal vanishes from the report entirely - no error, just a department that silently stopped existing. That is the bug this question is about.
Run on every build, against the tables above:
SELECT d.name AS department, COUNT(e.id) AS people
FROM departments d
LEFT JOIN employees e ON e.dept_id = d.id
GROUP BY d.id, d.name
ORDER BY d.name
| department | people |
|---|---|
| Engineering | 2 |
| Legal | 0 |
| Sales | 2 |
What it is really testing. Whether you have been caught by it. Everyone can recite the definition; the candidate who says "and that is how you lose the empty department" has debugged a report.