Bird
Raised Fist0
SQLquery~15 mins

EXCEPT (MINUS) for differences in SQL - Deep Dive

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Overview - EXCEPT (MINUS) for differences
What is it?
EXCEPT (also known as MINUS in some SQL dialects) is a command used to find rows that appear in one table or query but not in another. It compares two sets of data and returns only the unique rows from the first set that do not exist in the second. This helps identify differences between two lists or tables easily.
Why it matters
Without EXCEPT, finding differences between two data sets would require complex and error-prone queries. This command simplifies the process, making it easier to spot missing or extra data, which is crucial for data cleaning, auditing, and reporting. Without it, comparing data would be slower and more complicated, increasing the chance of mistakes.
Where it fits
Before learning EXCEPT, you should understand basic SQL SELECT queries and how to compare data using WHERE clauses or JOINs. After mastering EXCEPT, you can explore more advanced set operations like INTERSECT and UNION, and learn how to optimize queries for large data sets.
Mental Model
Core Idea
EXCEPT returns all rows from the first query that are not found in the second query, showing the difference between two data sets.
Think of it like...
Imagine you have two lists of friends who attended two different parties. EXCEPT helps you find which friends came to the first party but skipped the second one.
┌───────────────┐      ┌───────────────┐
│   Query A     │      │   Query B     │
│ {1, 2, 3, 4}  │      │ {3, 4, 5, 6}  │
└──────┬────────┘      └──────┬────────┘
       │                     │
       │ EXCEPT (A - B)       │
       ▼                     ▼
   Result: {1, 2}  ← Rows in A but not in B
Build-Up - 6 Steps
1
FoundationUnderstanding Basic Set Difference
🤔
Concept: Introduce the idea of comparing two lists to find unique items in one but not the other.
Imagine two lists: List A has {apple, banana, cherry} and List B has {banana, cherry, date}. The difference is the items in A that are not in B, which is {apple}. This is the core idea behind EXCEPT in SQL.
Result
The difference set is {apple}.
Understanding set difference in simple terms helps grasp how EXCEPT works in SQL to find unique rows.
2
FoundationBasic EXCEPT Syntax in SQL
🤔
Concept: Learn the basic SQL syntax to use EXCEPT between two SELECT queries.
The syntax is: SELECT column_list FROM table1 EXCEPT SELECT column_list FROM table2; Both SELECT statements must have the same number of columns and compatible data types.
Result
Returns rows from the first SELECT that are not in the second SELECT.
Knowing the syntax and requirements prevents errors and helps write correct EXCEPT queries.
3
IntermediateUsing EXCEPT with Multiple Columns
🤔Before reading on: Do you think EXCEPT compares rows based on all columns or just one column? Commit to your answer.
Concept: EXCEPT compares entire rows, meaning all columns together must match to be excluded.
If you select multiple columns, EXCEPT checks if the whole row exists in the second query. For example: SELECT id, name FROM table1 EXCEPT SELECT id, name FROM table2; Only rows with both id and name matching in table2 are excluded.
Result
Returns rows from table1 where the combination of id and name does not appear in table2.
Understanding that EXCEPT works on whole rows avoids confusion when partial matches exist.
4
IntermediateDifference Between EXCEPT and NOT IN/LEFT JOIN
🤔Before reading on: Is EXCEPT the same as using NOT IN or LEFT JOIN to find differences? Commit to yes or no.
Concept: EXCEPT is a set operation that is often simpler and more efficient than using NOT IN or LEFT JOIN for differences.
NOT IN and LEFT JOIN can find differences but require more complex conditions and can behave unexpectedly with NULLs. EXCEPT cleanly returns distinct rows from the first query not in the second without extra conditions.
Result
EXCEPT returns a clean difference set without NULL-related surprises.
Knowing when EXCEPT is simpler and safer helps write clearer and more reliable queries.
5
AdvancedHandling NULLs in EXCEPT Queries
🤔Before reading on: Do you think NULL values are treated as equal or different in EXCEPT? Commit to your answer.
Concept: In EXCEPT, NULLs are treated as equal, so rows with NULL in the same columns are considered matching.
If a row in the first query has NULL in a column and the second query has a row with NULL in the same column, EXCEPT treats them as the same for exclusion purposes. This differs from some other comparison methods where NULLs cause surprises.
Result
Rows with matching NULLs are excluded by EXCEPT.
Understanding NULL handling prevents unexpected results when comparing data with missing values.
6
ExpertPerformance and Optimization of EXCEPT
🤔Before reading on: Do you think EXCEPT always performs faster than JOIN-based difference queries? Commit to yes or no.
Concept: EXCEPT performance depends on database engine optimizations and indexing; sometimes JOINs can be faster with proper tuning.
Some databases optimize EXCEPT internally using hashing or sorting. However, for very large tables or complex queries, rewriting with LEFT JOIN and IS NULL or NOT EXISTS might be more efficient. Understanding execution plans helps choose the best approach.
Result
Performance varies; testing and indexing are key.
Knowing the internal workings and alternatives helps write efficient queries in production.
Under the Hood
EXCEPT works by comparing the result sets of two queries row by row. It typically uses hashing or sorting to identify rows in the first set that do not appear in the second. The database engine ensures that duplicates are removed from the final output, returning only distinct rows unique to the first query.
Why designed this way?
EXCEPT was designed to provide a simple, declarative way to find differences between data sets without writing complex joins or subqueries. It leverages set theory principles, making queries easier to read and maintain. Alternatives like joins were more error-prone and less intuitive for this purpose.
┌───────────────┐      ┌───────────────┐
│   Query A     │      │   Query B     │
│  Result Set   │      │  Result Set   │
└──────┬────────┘      └──────┬────────┘
       │                     │
       │  Hashing/Sorting     │
       └────────────┬────────┘
                    │
             Compare Rows
                    │
           ┌────────┴────────┐
           │ Rows in A not B │
           └─────────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does EXCEPT return rows that appear in both queries? Commit yes or no.
Common Belief:EXCEPT returns all rows from both queries combined.
Tap to reveal reality
Reality:EXCEPT only returns rows from the first query that do NOT appear in the second query.
Why it matters:Misunderstanding this leads to expecting more results than actually returned, causing confusion in data analysis.
Quick: Do you think EXCEPT compares only one column if multiple columns are selected? Commit yes or no.
Common Belief:EXCEPT compares only the first column when multiple columns are selected.
Tap to reveal reality
Reality:EXCEPT compares entire rows, meaning all columns together must match to exclude a row.
Why it matters:Assuming single-column comparison causes wrong query results and missed differences.
Quick: Does EXCEPT treat NULL values as different or the same? Commit your answer.
Common Belief:EXCEPT treats NULL values as different, so rows with NULLs are never matched.
Tap to reveal reality
Reality:EXCEPT treats NULLs as equal, so rows with NULL in the same columns are considered matching and excluded.
Why it matters:Not knowing this can cause unexpected missing rows or duplicates in results.
Quick: Is EXCEPT always faster than using JOINs for differences? Commit yes or no.
Common Belief:EXCEPT is always the fastest way to find differences between tables.
Tap to reveal reality
Reality:Performance depends on the database and query; sometimes JOINs with proper indexing are faster.
Why it matters:Blindly using EXCEPT without testing can lead to slow queries in large or complex databases.
Expert Zone
1
EXCEPT removes duplicates from the result, so it behaves like SELECT DISTINCT on the difference set, which can affect results if duplicates matter.
2
Some SQL dialects use MINUS instead of EXCEPT, but they behave similarly; knowing dialect differences is crucial for cross-platform compatibility.
3
EXCEPT requires both queries to have the same number and types of columns; implicit type conversions can cause subtle errors or unexpected results.
When NOT to use
Avoid EXCEPT when you need to preserve duplicates or when comparing complex data types that are not supported. Use JOINs or NOT EXISTS for more control over matching conditions or when working with NULL-sensitive comparisons.
Production Patterns
In production, EXCEPT is often used for data validation, such as checking missing records between source and target tables during ETL processes. It is also used in auditing to find discrepancies between expected and actual data sets.
Connections
Set Theory
EXCEPT is a direct application of set difference from set theory.
Understanding set difference in math clarifies how EXCEPT works to find unique elements in one set compared to another.
Data Cleaning
EXCEPT helps identify missing or extra data during cleaning processes.
Knowing how to find differences between data sets is essential for ensuring data quality and consistency.
Version Control Systems
EXCEPT conceptually resembles 'diff' operations that show differences between file versions.
Recognizing this connection helps understand how difference operations work across domains, from databases to code management.
Common Pitfalls
#1Using EXCEPT with different column orders or counts causes errors.
Wrong approach:SELECT id, name FROM table1 EXCEPT SELECT name, id FROM table2;
Correct approach:SELECT id, name FROM table1 EXCEPT SELECT id, name FROM table2;
Root cause:EXCEPT requires the same number and order of columns; swapping columns breaks compatibility.
#2Expecting EXCEPT to return duplicates when source has repeated rows.
Wrong approach:SELECT name FROM table1 EXCEPT SELECT name FROM table2; -- expecting duplicates if present
Correct approach:Use SELECT name FROM table1 EXCEPT SELECT name FROM table2; -- but remember EXCEPT removes duplicates
Root cause:EXCEPT inherently returns distinct rows, so duplicates are removed automatically.
#3Using EXCEPT without considering NULL behavior leads to missing rows.
Wrong approach:SELECT id FROM table1 EXCEPT SELECT id FROM table2; -- assuming NULLs won't match
Correct approach:Be aware that rows with NULL in matching columns are treated as equal and excluded.
Root cause:Misunderstanding how NULLs are compared in EXCEPT causes unexpected results.
Key Takeaways
EXCEPT finds rows in the first query that do not appear in the second, showing differences clearly.
It compares entire rows, so all selected columns must match to exclude a row.
EXCEPT removes duplicates and treats NULLs as equal, which affects the result set.
Performance varies; sometimes JOINs or NOT EXISTS are better for large or complex data.
Understanding EXCEPT helps simplify data comparison tasks essential for data quality and auditing.

Practice

(1/5)
1. What does the SQL EXCEPT operator do?
easy
A. Returns rows from the first query that are not in the second query
B. Returns rows common to both queries
C. Combines all rows from both queries including duplicates
D. Deletes rows from the first table that exist in the second

Solution

  1. Step 1: Understand the purpose of EXCEPT

    The EXCEPT operator compares two query results and returns only those rows that appear in the first query but not in the second.
  2. Step 2: Differentiate from other set operations

    Unlike INTERSECT which returns common rows, EXCEPT returns unique rows from the first set only.
  3. Final Answer:

    Returns rows from the first query that are not in the second query -> Option A
  4. Quick Check:

    EXCEPT = difference = unique first query rows [OK]
Hint: EXCEPT returns what first query has but second does not [OK]
Common Mistakes:
  • Confusing EXCEPT with INTERSECT
  • Thinking EXCEPT deletes rows
  • Assuming EXCEPT returns all combined rows
2. Which of the following is the correct syntax to find rows in table1 not in table2 using EXCEPT?
easy
A. SELECT * FROM table1 WHERE NOT IN table2;
B. SELECT * FROM table1 MINUS table2;
C. SELECT * FROM table1 EXCEPT SELECT * FROM table2;
D. SELECT * FROM table1 JOIN table2 EXCEPT;

Solution

  1. Step 1: Recall correct EXCEPT syntax

    The EXCEPT operator is used between two SELECT statements: SELECT ... FROM ... EXCEPT SELECT ... FROM ....
  2. Step 2: Check each option

    SELECT * FROM table1 EXCEPT SELECT * FROM table2; uses correct syntax with two SELECT statements separated by EXCEPT. SELECT * FROM table1 MINUS table2; is incorrect because MINUS requires SELECT before both tables. SELECT * FROM table1 WHERE NOT IN table2; is invalid syntax. SELECT * FROM table1 JOIN table2 EXCEPT; misuses JOIN and EXCEPT.
  3. Final Answer:

    SELECT * FROM table1 EXCEPT SELECT * FROM table2; -> Option C
  4. Quick Check:

    Correct EXCEPT syntax = SELECT ... EXCEPT SELECT ... [OK]
Hint: EXCEPT goes between two SELECT queries, no JOIN needed [OK]
Common Mistakes:
  • Using EXCEPT without two SELECT statements
  • Confusing MINUS syntax with EXCEPT
  • Trying to use EXCEPT with JOIN
3. Given these tables:
table1:
id
1
2
3

table2:
id
2
4
5

What is the result of:
SELECT id FROM table1 EXCEPT SELECT id FROM table2;
medium
A. [1, 3]
B. [2]
C. [4, 5]
D. [1, 2, 3, 4, 5]

Solution

  1. Step 1: Identify rows in table1

    table1 has ids 1, 2, 3.
  2. Step 2: Identify rows in table2

    table2 has ids 2, 4, 5.
  3. Step 3: Apply EXCEPT logic

    EXCEPT returns rows in table1 not in table2, so ids 1 and 3 remain.
  4. Final Answer:

    [1, 3] -> Option A
  5. Quick Check:

    table1 - table2 = [1, 3] [OK]
Hint: Subtract table2 rows from table1 rows [OK]
Common Mistakes:
  • Including ids from table2
  • Returning common ids instead
  • Confusing EXCEPT with UNION
4. What is wrong with this query?
SELECT name FROM employees EXCEPT name FROM managers;
medium
A. EXCEPT cannot be used with column names
B. Missing SELECT keyword before second query
C. Syntax requires JOIN instead of EXCEPT
D. No error, query is correct

Solution

  1. Step 1: Check syntax of EXCEPT usage

    EXCEPT requires two full SELECT statements. The second query must start with SELECT.
  2. Step 2: Identify error in given query

    The second part is missing SELECT before name FROM managers, causing syntax error.
  3. Final Answer:

    Missing SELECT keyword before second query -> Option B
  4. Quick Check:

    EXCEPT needs two SELECTs [OK]
Hint: Always write SELECT before both queries with EXCEPT [OK]
Common Mistakes:
  • Omitting SELECT in second query
  • Using EXCEPT with columns only
  • Confusing EXCEPT with JOIN syntax
5. You have two tables:
orders_2023 and orders_2024, both with columns order_id and customer_id.

You want to find all orders from 2023 that were NOT repeated in 2024.

Which query correctly finds these unique 2023 orders?
hard
A. SELECT order_id, customer_id FROM orders_2023 INTERSECT SELECT order_id, customer_id FROM orders_2024;
B. SELECT order_id, customer_id FROM orders_2024 EXCEPT SELECT order_id, customer_id FROM orders_2023;
C. SELECT order_id, customer_id FROM orders_2023 UNION SELECT order_id, customer_id FROM orders_2024;
D. SELECT order_id, customer_id FROM orders_2023 EXCEPT SELECT order_id, customer_id FROM orders_2024;

Solution

  1. Step 1: Understand the requirement

    We want orders in 2023 that do NOT appear in 2024.
  2. Step 2: Choose correct set operation

    EXCEPT returns rows in first query not in second, so orders_2023 EXCEPT orders_2024 fits.
  3. Step 3: Check other options

    SELECT order_id, customer_id FROM orders_2024 EXCEPT SELECT order_id, customer_id FROM orders_2023; reverses order, giving 2024 unique orders. SELECT order_id, customer_id FROM orders_2023 INTERSECT SELECT order_id, customer_id FROM orders_2024; returns common orders. SELECT order_id, customer_id FROM orders_2023 UNION SELECT order_id, customer_id FROM orders_2024; combines all orders.
  4. Final Answer:

    SELECT order_id, customer_id FROM orders_2023 EXCEPT SELECT order_id, customer_id FROM orders_2024; -> Option D
  5. Quick Check:

    2023 orders minus 2024 orders = unique 2023 orders [OK]
Hint: Use EXCEPT with 2023 first, 2024 second to find unique 2023 orders [OK]
Common Mistakes:
  • Swapping table order in EXCEPT
  • Using INTERSECT instead of EXCEPT
  • Using UNION which combines all rows