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.
You need one canonical row per key from a table containing duplicates (keep the most recent by a timestamp column), and you're told to do it with a join-based approach rather than a window function. Write that query, and discuss the trade-offs against a ROW_NUMBER()-based approach.
How does NULL behave in a join's equality condition, and separately in UNION/INTERSECT/EXCEPT comparisons? Build a small two-table example where a NULL join key causes a row to silently drop out of an INNER JOIN, show how a LEFT JOIN's NULLs can distort a downstream SUM or AVG if you're not careful, and demonstrate how COALESCE can defend against both.
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.
Rewrite a per-row correlated scalar subquery (one that runs once per outer row to compute a count or sum) into an equivalent JOIN with an aggregation, and explain why the rewritten version is usually faster on large tables.
You need two rows to match on a key even when both sides are NULL for that key. Explain why a plain equality join condition won't do that, then show how COALESCE and (in Postgres) IS NOT DISTINCT FROM each solve it, and what changes for index usage and performance once you introduce either.
Unlock Full Question Bank
Get access to all 35 SQL Joins and Set Operations interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.