Explain UNION, INTERSECT, and EXCEPT in SQL and when to use them for set comparison and reconciliation

Quick Overview

Asks you to explain the SQL set operators UNION, INTERSECT and EXCEPT, how they differ, and when to use each one. Tests your understanding of duplicate handling, NULL comparison, operand order, and set-based reconciliation between two tables.

Explain UNION, INTERSECT, and EXCEPT in SQL and when to use them for set comparison and reconciliation

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Explain the SQL set operators `UNION`, `INTERSECT`, and `EXCEPT`. What are their differences, and when would you use them for set comparison or reconciliation analysis? ```hint Duplicates and direction For each operator, ask what happens to a row that appears twice, and whether swapping the two queries changes the result. ``` ### What a Strong Answer Covers - What each operator returns and what the two queries must have in common for it to run. - How duplicates are handled by default and by the `ALL` variants, and what that costs. - Which operator depends on operand order, and why that matters when comparing two tables. - How set operators compare NULLs compared with a join condition. - A concrete pattern for reconciling two tables or extracts, and its limits. ### Follow-up Questions - How would you reconcile two tables when duplicate rows are legitimate and their counts must match? - When would you use `NOT EXISTS` or a `LEFT JOIN ... WHERE ... IS NULL` anti-join instead of `EXCEPT`? - Two tables share a primary key but some rows differ. How would you report which columns differ for each key?

Overview: Asks you to explain the SQL set operators UNION, INTERSECT and EXCEPT, how they differ, and when to use each one. Tests your understanding of duplicate handling, NULL comparison, operand order, and set-based reconciliation between two tables.

General logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Explain the SQL set operators UNION, INTERSECT, and EXCEPT. What are their differences, and when would you use them for set comparison or reconciliation analysis?

What a Strong Answer Covers Guidance

  • What each operator returns and what the two queries must have in common for it to run.
  • How duplicates are handled by default and by the ALL variants, and what that costs.
  • Which operator depends on operand order, and why that matters when comparing two tables.
  • How set operators compare NULLs compared with a join condition.
  • A concrete pattern for reconciling two tables or extracts, and its limits.

Follow-up Questions Guidance

  • How would you reconcile two tables when duplicate rows are legitimate and their counts must match?
  • When would you use NOT EXISTS or a LEFT JOIN ... WHERE ... IS NULL anti-join instead of EXCEPT ?
  • Two tables share a primary key but some rows differ. How would you report which columns differ for each key?