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.
A multi-table query is returning a far larger result set than expected. Walk through how you'd confirm it's an accidental Cartesian product (missing or wrong join predicate) rather than a genuine data issue, using row-count checks and progressive query construction, then show the corrected query. What would you add to code review or CI to catch this class of bug before it ships?
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.
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.
Two tables you need to join don't line up cleanly on a plain equality: one side stores a timestamp and the other a truncated date, or the identifiers differ in case, formatting, or timezone. Show how you'd write a correct join across the mismatch (using an expression on the join key, or normalizing beforehand), and explain the performance cost of joining on a transformed expression versus normalizing the data first.
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.
Unlock Full Question Bank
Get access to all 33 SQL Joins and Set Operations interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.