β SQL EXCEPT & MINUS β Compare and Subtract Query Results
π§² Introduction β What is SQL EXCEPT / MINUS?
EXCEPT and MINUS are SQL set operators used to return rows from one SELECT query that do not exist in another. Think of it as query subtraction.
- EXCEPTis used in PostgreSQL and SQL Server
- MINUSis used in Oracle
π― In this guide, you’ll learn:
- Syntax and rules for EXCEPT/MINUS
- How they compare to JOIN,INTERSECT, andNOT IN
- Workarounds for unsupported databases
β 1. EXCEPT Syntax (PostgreSQL / SQL Server)
SELECT column1, column2 FROM table1
EXCEPT
SELECT column1, column2 FROM table2;
β Returns rows from the first query not present in the second.
β 2. MINUS Syntax (Oracle)
SELECT column1, column2 FROM table1
MINUS
SELECT column1, column2 FROM table2;
β
 Oracle equivalent of EXCEPT
π 3. EXCEPT vs INTERSECT vs UNION
| Operator | Result Description | Duplicates Removed | 
|---|---|---|
| UNION | All distinct rows from both queries | β Yes | 
| INTERSECT | Rows present in both queries | β Yes | 
| EXCEPT/MINUS | Rows in first, not in second | β Yes | 
π§ 4. MySQL Workaround for EXCEPT
SELECT column1, column2
FROM table1
WHERE (column1, column2) NOT IN (
  SELECT column1, column2 FROM table2
);
β
 MySQL does not support EXCEPT or MINUSβuse NOT IN or LEFT JOIN instead.
π 5. Example β Customers with No Orders
SELECT customer_id FROM customers
EXCEPT
SELECT customer_id FROM orders;
β Returns customer IDs that do not appear in the orders table.
π Best Practices
| β Recommended | β Avoid This | 
|---|---|
| Use for data difference analysis | Using with different column counts | 
| Combine with INTERSECTfor completeness | Expecting MySQL to support it directly | 
| Match column positions and types | Ignoring NULL impact on comparisons | 
π Summary β Recap & Next Steps
EXCEPT / MINUS helps you find rows that are unique to one result set. Itβs useful for anti-joins, integrity checks, and de-duplication.
π Key Takeaways:
- EXCEPT: Subtract second query from first
- MINUS: Same in Oracle
- Both remove duplicates automatically
- Not available in MySQLβuse JOIN or NOT IN
βοΈ Real-World Relevance:
Used in audit reports, data quality checks, and access control testing.
β‘οΈ Next: Learn CASE, COALESCE, or write anti-joins with LEFT JOIN and NULL filters.
β FAQ β SQL EXCEPT / MINUS
β What does SQL EXCEPT do?
β Returns rows from the first query that are not in the second.
β What is the difference between MINUS and EXCEPT?
β
 Functionally the same; MINUS is Oracle-specific.
β Does EXCEPT remove duplicates?
β
 Yes. Use EXCEPT ALL (if supported) to keep duplicates.
β How can I do EXCEPT in MySQL?
β
 Use NOT IN, LEFT JOIN with IS NULL, or subqueries.
Share Now :
