đ SQL Set Operators: UNION, INTERSECT, MINUS
Set operators combine the results of two or more SELECT queries. Let's see how UNION, INTERSECT, and MINUS work using Bangladeshi employee examples.
đ Sample Tables
sales_team
name | department |
---|---|
Arif | Sales |
Rafi | Sales |
Nasrin | Sales |
marketing_team
name | department |
---|---|
Nasrin | Marketing |
Ritu | Marketing |
Faruk | Marketing |
1️⃣ UNION
Goal: Combine all unique employee names from both teams.
SELECT name FROM sales_team
UNION
SELECT name FROM marketing_team;
Output:
name |
---|
Arif |
Faruk |
Nasrin |
Rafi |
Ritu |
2️⃣ INTERSECT
Goal: Find names common in both teams (same name must exist in both queries).
SELECT name FROM sales_team
INTERSECT
SELECT name FROM marketing_team;
Output:
name |
---|
Nasrin |
3️⃣ MINUS (or EXCEPT)
Goal: Find employees who are in sales_team but not in marketing_team.
SELECT name FROM sales_team
MINUS
SELECT name FROM marketing_team;
Output:
name |
---|
Arif |
Rafi |
đ Summary:
- UNION: Combines unique rows from both queries.
- INTERSECT: Returns only common rows in both queries.
- MINUS: Returns rows in first query that are not in the second.
- ⚠ Note: Some databases use
EXCEPT
instead ofMINUS
.
āĻোāύ āĻŽāύ্āϤāĻŦ্āϝ āύেāĻ:
āĻāĻāĻি āĻŽāύ্āϤāĻŦ্āϝ āĻĒোāϏ্āĻ āĻāϰুāύ