SQL Joins and Set Operations Questions

Combining data across multiple tables using inner, outer, cross, and self joins, plus set operations (UNION, INTERSECT, EXCEPT). Covers join-key selection, fan-out and row-multiplication pitfalls, merge strategies, and integrating data from disparate sources. A high-frequency interview surface for anyone who queries relational data.

MediumTechnical
123 practiced

A query uses NOT IN (SELECT ... ) to find rows in one table with no match in another, and it unexpectedly returns zero rows. Explain why NOT IN behaves this way when the subquery can produce a NULL, then rewrite it correctly using NOT EXISTS (or LEFT JOIN ... IS NULL) and say which of the two you'd reach for by default and why.

EasyTechnical
63 practiced

Given an employees table that references itself for a manager relationship (employee_id, name, manager_id), write a query that returns each employee alongside their manager's name, showing NULL (or an explicit 'no manager') for anyone at the top of the org. Explain why a self-join is the natural tool here, and how you'd extend the same pattern one more level up (manager's manager) or decide you need a recursive CTE instead.

HardTechnical
124 practiced

A join chain across three source tables is returning noticeably fewer rows than you expected, and nothing obviously errored. Walk through how you'd systematically track down where the rows are being lost. For each hypothesis you consider, describe the specific SQL check you'd run and what a bad result would tell you.

MediumTechnical
57 practiced

Explain what a LATERAL join (or CROSS APPLY) does that a plain JOIN can't, then use it to attach each parent row's most recent (or top-N) related child rows, for example each customer's most recent invoice or each order's top 3 shipments. Compare it to solving the same problem with a window function instead, and say when you'd reach for each.

EasyTechnical
70 practiced

Rewrite this old-style comma-separated join (FROM a, b WHERE a.id = b.a_id AND ...) into explicit JOIN ... ON syntax, and explain the practical reasons explicit joins are preferred in production code, including what silently goes wrong if someone forgets a WHERE condition in the old style.

Unlock Full Question Bank

Get access to all 35 SQL Joins and Set Operations interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.