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.
Joins combine tables horizontally (adding columns); set operations combine result sets vertically (stacking or comparing rows). Give two concrete examples where the right tool is a set operation and two where it's a join, and explain what goes wrong with duplicate and NULL handling if you reach for the wrong shape.
What happens if you put an ORDER BY and LIMIT inside each branch of a UNION, then union those branches together? Explain the standard SQL behavior here and how it differs from applying ORDER BY/LIMIT to the final unioned result, and show the correct way to get 'top M rows per branch' out of a unioned query.
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.
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.
You're joining on a composite key, but for some rows one part of the key is NULL and the business rule says that NULL should act as a wildcard matching any value on the other side, not as 'no match'. Write the join that implements this, and separately explain why casually treating NULL as a literal sentinel value (e.g. coalescing it to a string like 'NULL') is dangerous when that string could itself be a legitimate value in the data.
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.