đ§Ē SQL Practice: Combine and Compare Queries
Use SQL set operators like UNION, INTERSECT, and MINUS to merge and compare data from different sources.
đ Sample Tables
sales_team
| name | region |
|---|---|
| Arif | Dhaka |
| Rafi | Chattogram |
| Nasrin | Dhaka |
marketing_team
| name | region |
|---|---|
| Nasrin | Dhaka |
| Ritu | Khulna |
| Faruk | Chattogram |
đ§ Practice 1: Combine All Unique Employees
Goal: Show all distinct employee names from both teams.
SELECT name FROM sales_team
UNION
SELECT name FROM marketing_team;
Output:
| name |
|---|
| Arif |
| Faruk |
| Nasrin |
| Rafi |
| Ritu |
đ§ Practice 2: Find Common Employees
Goal: Find employees who work in both teams.
SELECT name FROM sales_team
INTERSECT
SELECT name FROM marketing_team;
Output:
| name |
|---|
| Nasrin |
đ§ Practice 3: Find Sales-Only Members
Goal: Show employees in sales team but not in marketing.
SELECT name FROM sales_team
MINUS
SELECT name FROM marketing_team;
Output:
| name |
|---|
| Arif |
| Rafi |
đ§ Practice 4: Combine with Duplicates (UNION ALL)
Goal: Combine names from both teams including duplicates.
SELECT name FROM sales_team
UNION ALL
SELECT name FROM marketing_team;
Output: Includes "Nasrin" twice since she’s in both.
| name |
|---|
| Arif |
| Rafi |
| Nasrin |
| Nasrin |
| Ritu |
| Faruk |
đ Practice Tips:
- Try changing regions or adding new employees and rerun the queries.
- Try using
ORDER BYafter UNION for sorting. - Use
UNION ALLto keep duplicates if needed.
āĻোāύ āĻŽāύ্āϤāĻŦ্āϝ āύেāĻ:
āĻāĻāĻি āĻŽāύ্āϤāĻŦ্āϝ āĻĒোāϏ্āĻ āĻāϰুāύ