Discover how to instantly find what two lists share or don't share without tedious checking!
Why INTERSECT and EXCEPT in PostgreSQL? - Purpose & Use Cases
Imagine you have two lists of friends from different social events written on paper. You want to find who attended both events or who attended only the first event. Doing this by hand means checking each name one by one, which is tiring and easy to mess up.
Manually comparing lists is slow and mistakes happen easily. You might miss names, count duplicates, or forget to check all entries. This wastes time and causes frustration, especially with long lists.
Using INTERSECT and EXCEPT in SQL lets the database quickly find common or unique entries between two sets. It does the hard work instantly and accurately, so you get reliable results without the hassle.
Check each name in list1 against list2 one by one.SELECT name FROM list1 INTERSECT SELECT name FROM list2; SELECT name FROM list1 EXCEPT SELECT name FROM list2;
It enables fast and error-free comparison of data sets to find common or unique records effortlessly.
A company wants to find customers who bought both product A and product B (INTERSECT) or those who bought product A but not product B (EXCEPT) to target marketing campaigns.
Manual comparison is slow and error-prone.
INTERSECT finds common data between sets.
EXCEPT finds data in one set but not the other.
Practice
INTERSECT operator do in PostgreSQL?Solution
Step 1: Understand INTERSECT purpose
INTERSECT returns only rows that appear in both query results.Step 2: Compare with other set operations
Unlike UNION or EXCEPT, INTERSECT finds common rows, not all or unique rows.Final Answer:
Returns rows common to both queries -> Option DQuick Check:
INTERSECT = common rows [OK]
- Confusing INTERSECT with UNION
- Thinking INTERSECT returns all rows from first query
- Mixing INTERSECT with EXCEPT
table1 but not in table2 using EXCEPT?Solution
Step 1: Identify EXCEPT syntax
The correct syntax is: SELECT ... EXCEPT SELECT ... to get rows in first query not in second.Step 2: Check options for correct usage
SELECT * FROM table1 EXCEPT SELECT * FROM table2; matches correct syntax; others misuse INTERSECT, UNION, or EXCEPT ALL (which is valid but different).Final Answer:
SELECT * FROM table1 EXCEPT SELECT * FROM table2; -> Option BQuick Check:
EXCEPT syntax = SELECT ... EXCEPT SELECT ... [OK]
- Using INTERSECT instead of EXCEPT
- Adding UNION incorrectly
- Using EXCEPT ALL when not needed
table_a:id
1
2
3
table_b:id
2
3
4
What is the result of:
SELECT id FROM table_a INTERSECT SELECT id FROM table_b;Solution
Step 1: List rows from both tables
table_a has ids 1, 2, 3; table_b has ids 2, 3, 4.Step 2: Find common ids using INTERSECT
Common ids are 2 and 3 only.Final Answer:
[2, 3] -> Option CQuick Check:
INTERSECT = common rows = [2, 3] [OK]
- Including ids only in one table
- Confusing INTERSECT with UNION
- Expecting all ids combined
SELECT id, name FROM table1 EXCEPT SELECT id FROM table2;
Solution
Step 1: Check column counts in both SELECTs
First SELECT returns two columns (id, name), second returns one column (id).Step 2: Understand EXCEPT requirements
EXCEPT requires both SELECTs to have the same number and compatible types of columns.Final Answer:
The number of columns in both SELECTs differ -> Option AQuick Check:
EXCEPT needs equal columns [OK]
- Ignoring column count mismatch
- Thinking EXCEPT needs WHERE clause
- Assuming table names cause error
employees with columns id, namecontractors with columns id, nameYou want to find people who are either employees or contractors but not both.
Which query correctly returns this list?
Solution
Step 1: Understand the requirement
We want people in employees or contractors but not in both (symmetric difference).Step 2: Use EXCEPT and UNION to find symmetric difference
EXCEPT finds those in one table but not the other; UNION combines both exclusive sets.Step 3: Analyze options
SELECT id, name FROM employees EXCEPT SELECT id, name FROM contractors UNION SELECT id, name FROM contractors EXCEPT SELECT id, name FROM employees; correctly combines EXCEPT in both directions with UNION to get symmetric difference.Final Answer:
SELECT id, name FROM employees EXCEPT SELECT id, name FROM contractors UNION SELECT id, name FROM contractors EXCEPT SELECT id, name FROM employees; -> Option AQuick Check:
Symmetric difference = EXCEPT + UNION [OK]
- Using only INTERSECT or UNION
- Using EXCEPT one way only
- Confusing symmetric difference with intersection
Ready to prove it?
Pass the test and this pattern moves unknown
