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.
Explain semi-join and anti-join as concepts distinct from a regular INNER or LEFT JOIN: what question each one answers and why. Then write a semi-join with EXISTS to check whether a customer has any qualifying transaction, and explain why this can outperform (and avoid duplicate rows compared to) an INNER JOIN followed by DISTINCT.
What does a CROSS JOIN produce? Given two small tables like colors(color) and sizes(size), write the query that generates every combination for a product feed, and show a handful of example input/output rows.
You need to combine rows from two or more sources whose schemas don't quite match (different column names, or one source missing a column the others have). Write a UNION ALL that normalizes the columns first, and explain the choices you made for any column that only exists on one side.
When would you restructure a deeply nested, hard-to-review multi-join query into a series of CTEs instead of leaving it as nested subqueries? Take a query chaining several joins and rewrite it as staged CTEs, explaining how you decided where to break it up and any performance caveats worth knowing about.
EXCEPT and INTERSECT require matching columns on both sides, but engines differ in how they treat NULLs in the compared columns and in dialect-specific behavior (MINUS vs EXCEPT, availability). Given two tables with NULLs in a key column, show a case where the result is not what a newcomer would expect, and how to explicitly include or exclude NULL rows.
Unlock Full Question Bank
Get access to all 42 SQL Joins and Set Operations interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.