SQL EXCEPT Explained Visually (SQL Server, PostgreSQL & MySQL)
See how SQL EXCEPT subtracts query rows step by step: SQL Server & PostgreSQL syntax, MySQL 8.0.31+ & LEFT JOIN alternatives, and the NOT IN NULL trap.
The EXCEPT operator in SQL subtracts the rows of a second SELECT query from the first SELECT query—returning only the distinct rows that exist in Query A and NOT in Query B.
1. Interactive Trace: Watch EXCEPT Subtract & Deduplicate Rows
Suppose an e-commerce team wants every customer who signed up (all_signups) but never placed a paid order (paid_buyers). Step through how the database engine executes EXCEPT:
| user_id | |
|---|---|
| 101 | aarav@dev.io |
| 102 | meera@dev.io |
| 102 | meera@dev.io |
| 103 | kabir@dev.io |
2. Dialect Support: SQL Server vs PostgreSQL vs Oracle vs MySQL
Before using EXCEPT in production or an interview, check your database engine's dialect rules:
| Database Engine | EXCEPT (Distinct) | EXCEPT ALL (Keep Duplicates) | Notes / Equivalent |
|---|---|---|---|
| SQL Server (T-SQL) | ✅ Supported | ❌ Not supported | Use ROW_NUMBER() + EXCEPT if you need bag subtraction in SQL Server |
| PostgreSQL | ✅ Supported | ✅ Supported | Full ANSI SQL standard implementation |
| MySQL 8.0.31+ | ✅ Supported | ✅ Supported | Older MySQL < 8.0.31 requires LEFT JOIN ... IS NULL or NOT EXISTS |
| Oracle | MINUS (EXCEPT in 21c+) | MINUS ALL (21c+) | Classic Oracle interviews call this operator MINUS |
| SQLite | ✅ Supported | ❌ Not supported | Deduplicates results automatically |
3. The 3 Strict Rules of EXCEPT
- Same Column Count: Both
SELECTstatements must return the exact same number of columns. - Compatible Data Types: Column
1in Query A must match Column1in Query B (e.g.,INTwithINT,VARCHARwithVARCHAR). - Single
ORDER BYat the Very Bottom: Column names in the final result come from the topSELECT, andORDER BYis allowed only once at the very end of the combined query.
-- Works identically in SQL Server, PostgreSQL, SQLite & MySQL 8.0.31+
SELECT product_id, sku
FROM warehouse_inventory
WHERE active = 1
EXCEPT
SELECT product_id, sku
FROM discontinued_products
ORDER BY product_id;
4. Why EXCEPT Beats NOT IN When NULL Exists (Classic Interview Trap!)
Many developers reach for WHERE user_id NOT IN (SELECT ...)—and get burned in production when a single NULL sneaks into the subquery:
-- ❌ DANGEROUS: If cancelled_orders contains EVEN ONE row where user_id IS NULL,
-- SQL three-valued logic evaluates (id != NULL) -> UNKNOWN, returning 0 ROWS!
SELECT user_id
FROM users
WHERE user_id NOT IN (SELECT user_id FROM cancelled_orders);
-- ✅ SAFE OPTION 1: EXCEPT treats NULL = NULL as matching set values (never wipes rows!)
SELECT user_id FROM users
EXCEPT
SELECT user_id FROM cancelled_orders;
-- ✅ SAFE OPTION 2: NOT EXISTS (works on every SQL version & preserves index seeks)
SELECT u.user_id
FROM users u
WHERE NOT EXISTS (
SELECT 1
FROM cancelled_orders c
WHERE c.user_id = u.user_id
);
5. When to Choose EXCEPT vs NOT EXISTS / LEFT ANTI JOIN
- Use
EXCEPTwhen: You are comparing multi-column row snapshots (e.g., data migration QA comparingstaging_ordersvsprod_ordersacross 8 columns at once, where writing 8NULL-safeORjoin conditions by hand is error-prone!). - Use
NOT EXISTSwhen: You need columns from Table A that don't exist in Table B (like returningu.name, u.created_atwhile checkingorders.user_id), or when you want keep duplicate rows in SQL Server without deduplicating Table A.
Quick Check — SQL EXCEPT Mastery
Master the Complete Curriculum Visually
Liked this interactive breakdown? Jump into our full, 100% free A–Z visual course with zero sign-up wall.
SQL EXCEPT Explained Visually (SQL Server, PostgreSQL & MySQL) — FAQ
Quick answers to the most common edge cases and interview questions.
How does EXCEPT work in SQL Server (T-SQL) and PostgreSQL?
In both SQL Server and PostgreSQL, QueryA EXCEPT QueryB returns all distinct rows from QueryA that do not appear in QueryB. Both queries must have the exact same number of columns and compatible data types.
Does MySQL support the SQL EXCEPT operator?
MySQL added native EXCEPT and EXCEPT ALL support in MySQL 8.0.31. On older MySQL 5.7 or 8.0.x versions, use LEFT JOIN ... WHERE right.id IS NULL or WHERE NOT EXISTS (...).
Why is SQL EXCEPT safer than NOT IN when columns contain NULL?
If the subquery inside WHERE id NOT IN (SELECT user_id FROM ...) contains even a single NULL, SQL three-valued logic evaluates the entire filter to UNKNOWN and returns 0 rows! EXCEPT compares NULL = NULL as equal row values, so NULLs never wipe out your result set.
What is the difference between EXCEPT and EXCEPT ALL?
Standard EXCEPT deduplicates rows automatically (supported in SQL Server, PostgreSQL, SQLite, MySQL 8.0.31+). EXCEPT ALL preserves duplicates using bag subtraction (supported in PostgreSQL and MySQL 8.0.31+, but not in SQL Server T-SQL).