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
104 practiced

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.

MediumTechnical
58 practiced

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.

HardTechnical
62 practiced

Given three tables joined as A LEFT JOIN B LEFT JOIN C, explain how changing the order in which the outer joins are evaluated can change which rows survive in the final result, not just how fast the query runs. Construct a small example that demonstrates it, and show how you'd restructure the query (e.g. via a derived table or CTE) to get the semantics you actually want.

MediumTechnical
121 practiced

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.

MediumTechnical
111 practiced

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.

Unlock Full Question Bank

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

Sign in to Continue

Join thousands of developers preparing for their dream job.