Explain UNION, INTERSECT, and EXCEPT commands. What are their differences, and when would you use them for set comparison or reconciliation analysis?
Last updated
These are set operators that combine the results of two queries by rows (not columns like joins). UNION returns rows in either set, INTERSECT returns rows in both, and EXCEPT returns rows in the first but not the second. All require both queries to have compatible column counts and types.
UNION: Combines both result sets and removes duplicates; useUNION ALLto keep duplicates (faster, no dedup sort).INTERSECT: Returns only rows appearing in both sets: useful to find common records between two populations.EXCEPT(calledMINUSin Oracle): Returns rows in the first query not present in the second: order matters, it is directional.Reconciliation use: To find what differs between two tables, run
A EXCEPT BandB EXCEPT A; empty results on both means the sets are identical.Note:
INTERSECTandEXCEPTimplicitly remove duplicates and compare NULLs as equal, unlike a join's equality.
- https://medium.com/@krthiak/30-sql-window-functionion-questions-for-interviews-day-47-of-100-days-of-data-engineering-ai-and-638bef1af0f6
- https://www.datacamp.com/blog/top-sql-interview-questions-and-answers-for-beginners-and-intermediate-practitioners
- https://builder.ai2sql.io/blog/sql-window-functions-interview
- https://medium.com/activated-thinker/top-20-sql-interview-questions-for-data-analyst-data-scientist-ml-engineers-688bd22bb139
- https://datalemur.com/blog/sql-window-functions-interview-questions
- https://thedataforge.medium.com/50-sql-interview-questions-every-data-engineer-must-know-advanced-edition-part-2-c6a5fe8dd18b
- https://medium.com/towards-data-engineering/mastering-sql-recursive-ctes-key-concepts-and-interview-questions-04f69c408e81
- https://code.likeagirl.io/cte-vs-subquery-vs-recursive-cte-the-interview-question-every-data-scientist-should-know-ce6163487841
- https://www.youtube.com/watch?v=Hv54WLTHN70
- https://leetcode.com/discuss/study-guide/1601295/database-sql-primer-part-4-recursive-ctes/
- https://www.geeksforgeeks.org/sql-server/recursive-cte-in-sql-server/
- https://www.datainterview.com/blog/top-100-sql-interview-questions
- https://www.red-gate.com/simple-talk/databases/sql-server/t-sql-programming-sql-server/questions-about-cube-rollup-and-grouping-sets-that-you-were-too-shy-to-ask/
- https://lolithasherley7.medium.com/sql-interview-questions-and-answers-0a51debd30e7
- https://learnsql.com/blog/group-by-sql-interview-questions/
- https://builder.ai2sql.io/blog/sql-aggregate-functions-interview
- https://medium.com/@piyush.devspace/rollup-vs-cube-for-group-by-439dde93036e
- https://stackoverflow.com/questions/28550679/what-is-the-difference-between-lateral-join-and-a-subquery-in-postgresql
- https://www.baeldung.com/sql/lateral-join-vs-subquery
- https://medium.com/@goldengrisha/a-deep-dive-into-sql-lateral-join-7b09fcb3b745
- https://dba.stackexchange.com/questions/237219/when-to-use-lateral-joins-vs-correlated-subqueries-in-postgres
- https://upesonline.ac.in/blog/advanced-sql-interview-questions
- https://www.geeksforgeeks.org/sql/difference-between-nested-subquery-correlated-subquery-and-join-operation/
- https://www.practicewindowfunctions.com/learn/gap_and_island
- https://www.youtube.com/watch?v=18_cInZr0Gc
- https://www.red-gate.com/simple-talk/databases/sql-server/t-sql-programming-sql-server/introduction-to-gaps-and-islands-analysis/
- https://medium.com/@mvanshika23/sql-interview-patterns-you-must-know-part-1-n-gaps-and-islands-e9db5c97140f
- https://medium.com/analytics-vidhya/sql-classic-problem-identifying-gaps-and-islands-across-overlapping-date-ranges-5681b5fcdb8
- https://www.interviewbit.com/sql-interview-questions/
- https://github.com/Devinterview-io/sql-interview-questions
- https://www.tredence.com/blog/30-sql-interview-questions-2026