Chapter 18 โ Set Operations: UNION, UNION ALL, INTERSECT & EXCEPT
๐ก Intermediate
---
๐ Definition & Fundamental Concepts
An SQL Set Operation combines the result sets of two or more independent SELECT queries into a single unified result.
While SQL Joins combine data horizontally by attaching matching columns from related tables side-by-side, Set Operations combine data vertically by stacking rows from compatible queries on top of one another.
1. HORIZONTAL COMBINATION (SQL JOINS):
[ Table A Columns ] + [ Table B Columns ] ---> Combined Columns Side-by-Side
2. VERTICAL COMBINATION (SET OPERATIONS):
[ Query 1 Result Rows ]
+ ---> Combined Stacked Rows
[ Query 2 Result Rows ]
---
๐ฎ๐ณ Hindi Explanation
SQL Set Operations do ya zyada alag-alag SELECT queries ke results ko vertically ek ke neeche ek jodti hain.
- Joins do tables ke columns ko aamne-saamne (horizontally) jodte hain.
- Set Operations queries ke rows ko upar-neeche (vertically) stack karti hain.
UNIONsaari unique rows ko ek sath laata hai aur duplicate rows hata deta hai.UNION ALLbina duplicate hataye saari rows waise ki waise jodta hai.INTERSECTsirf wo rows deta hai jo dono queries mein common hain.EXCEPTpehli query ki wo rows deta hai jo doosri query mein nahi hain.
---
๐ฉ Marathi Explanation
SQL Set Operations don kiwa tyapeksha jast swatantra SELECT queries che results vertically ekamekanchya khali ekatra kartat.
- Joins dona tables che columns aamne-saamne (horizontally) jodtat.
- Set Operations queries chya rows khali-war (vertically) stack kartat.
UNIONsarva unique rows ekatra karto aani duplicate rows kadhto.UNION ALLduplicate na kadhta sarva rows aahe tya swaroopat thevto.INTERSECTfatkt tyaach rows deto ja donhi queries madhye saman (common) ahet.EXCEPTpahilya query madhil asha rows deto ja dusrya query madhye nahit.
---
๐ The Four Primary Set Operators
UNION UNION ALL INTERSECT EXCEPT / MINUS
+---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+
/ ### \ / ### \ / ### \ / ### \ / \ / \ / ### \ / \
##### X ##### ##### X ##### #####X##### ##### X
\ ### / \ ### / \ ### / \ ### / \ / \ / \ ### / \ /
+---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+
Combines & Removes Combines & Keeps Returns ONLY Common Returns First Minus
Duplicates Duplicates Matches Second Matches
1. UNION
Combines result sets from multiple SELECT statements and eliminates duplicate rows.
2. UNION ALL
Combines result sets from multiple SELECT statements and retains all rows, including duplicates.
[!NOTE]
Performance Insight:
UNION ALLgenerally avoids the duplicate-elimination step (which involves sorting or hashing the intermediate result set) required byUNION. However, actual execution speed depends on data volume, query structure, indexes, and the database optimizer's execution plan.
3. INTERSECT
Returns only distinct rows that are present in both query results.
4. EXCEPT (or MINUS in Oracle)
Returns distinct rows from the first query that are absent from the second query.
[!IMPORTANT]
Whole-Row Duplicate Elimination: Duplicate evaluation in
UNION,INTERSECT, andEXCEPTcompares the entire selected row (every selected column in combination), NOT just a single column such asnameor
---
๐ Essential Rules for Combining SELECT Statements
To combine two or more SELECT queries using set operations, all queries MUST strictly adhere to the following rules:
1. Equal Number of Columns: Every participating SELECT statement must return the exact same number of columns.
2. Compatible Data Types: Corresponding columns (1st with 1st, 2nd with 2nd) must have matching or implicitly convertible data types.
3. Identical Logical Column Order: Columns must be positioned in the same logical order across all SELECT queries.
4. Inherited Column Headers: Output column names in the final result set are inherited from the first SELECT statement.
5. Sorting with ORDER BY:
- A top-level
ORDER BYclause appears at the very end of the compound query and sorts the entire combined result set. - Individual
SELECTstatements cannot have standaloneORDER BYclauses unless enclosed in parentheses with aLIMITclause (e.g.(SELECT ... ORDER BY col LIMIT n) UNION (SELECT ... ORDER BY col LIMIT n)), or used inside derived tables/subqueries.
---
โ๏ธ MySQL Version Compatibility & Version Check
Different MySQL versions support different native set operators:
UNIONandUNION ALL: Supported natively across ALL MySQL versions.INTERSECTandEXCEPT: Supported natively starting with MySQL 8.0.31 (released October 2022).- Older MySQL Versions (< 8.0.31): Require alternative query patterns (such as
INNER JOIN,EXISTS, orNOT EXISTS).
To check the exact MySQL version running on your environment (e.g. WampServer or phpMyAdmin), execute:
SELECT VERSION();
---
๐ Reproducible Sample Setup Data
Run the following script to create and populate two sample tables: current_clients and event_attendees.
[!NOTE]
DROP TABLE IF EXISTSensures the script is completely reproducible by removing any pre-existing tables and data before creation.
-- Clean up pre-existing sample tables
DROP TABLE IF EXISTS event_attendees;
DROP TABLE IF EXISTS current_clients;
-- 1. Create current_clients table
CREATE TABLE current_clients (
client_id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL,
city VARCHAR(50) NOT NULL
);
-- 2. Create event_attendees table
CREATE TABLE event_attendees (
attendee_id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL,
city VARCHAR(50) NOT NULL
);
-- 3. Populate current_clients
INSERT INTO current_clients (client_id, name, email, city) VALUES
(1, 'Rahul Sharma', 'rahul@example.com', 'Mumbai'),
(2, 'Priya Patel', 'priya@example.com', 'Delhi'),
(3, 'Amit Verma', 'amit@example.com', 'Bangalore');
-- 4. Populate event_attendees
INSERT INTO event_attendees (attendee_id, name, email, city) VALUES
(101, 'Priya Patel', 'priya@example.com', 'Delhi'), -- Exact match with client_id 2
(102, 'Neha Gupta', 'neha@example.com', 'Pune'),
(103, 'Suresh Kumar', 'suresh@example.com', 'Mumbai');
---
๐งญ Detailed Operators Breakdown with Examples & Outputs
1. UNION (Combines & Removes Duplicate Rows)
Merges rows from both tables and removes exact duplicate rows across all selected columns.
SQL Query (All MySQL Versions)
SELECT name, email, city FROM current_clients
UNION
SELECT name, email, city FROM event_attendees
ORDER BY name;
Output
| name | city | |
|---|---|---|
| Amit Verma | amit@example.com | Bangalore |
| Neha Gupta | neha@example.com | Pune |
| Priya Patel | priya@example.com | Delhi |
| Rahul Sharma | rahul@example.com | Mumbai |
| Suresh Kumar | suresh@example.com | Mumbai |
Explanation
Priya Patel(priya@example.com,Delhi) exists in bothcurrent_clientsandevent_attendees.- Because
UNIONevaluates the entire row (name,email,city), it identifiesPriya Patelas a duplicate and returns her once. - Output contains 5 distinct rows.
---
2. UNION ALL (Combines & Retains Duplicate Rows)
Merges rows from both queries and keeps every single row, including duplicates.
SQL Query (All MySQL Versions)
SELECT name, email, city FROM current_clients
UNION ALL
SELECT name, email, city FROM event_attendees
ORDER BY name, email;
Output
| name | city | |
|---|---|---|
| Amit Verma | amit@example.com | Bangalore |
| Neha Gupta | neha@example.com | Pune |
| Priya Patel | priya@example.com | Delhi |
| Priya Patel | priya@example.com | Delhi |
| Rahul Sharma | rahul@example.com | Mumbai |
| Suresh Kumar | suresh@example.com | Mumbai |
Explanation
UNION ALLbypasses duplicate removal.Priya Patelappears twice (once fromcurrent_clientsand once fromevent_attendees).- Output contains all 6 total rows.
---
3. INTERSECT (Returns Common Rows Only)
Returns only rows that exist in both query result sets.
Native SQL Query (Requires MySQL 8.0.31 or Later)
-- Native INTERSECT (MySQL 8.0.31+)
SELECT name, email, city FROM current_clients
INTERSECT
SELECT name, email, city FROM event_attendees
ORDER BY name;
Output
| name | city | |
|---|---|---|
| Priya Patel | priya@example.com | Delhi |
Alternatives for Older MySQL Versions (< 8.0.31)
##### Alternative A: Using EXISTS (Broadly Compatible)
-- Works in ALL MySQL versions
SELECT DISTINCT
c.name,
c.email,
c.city
FROM current_clients AS c
WHERE EXISTS (
SELECT 1
FROM event_attendees AS a
WHERE a.name = c.name
AND a.email = c.email
AND a.city = c.city
)
ORDER BY c.name;
##### Alternative B: Using INNER JOIN with DISTINCT
-- Works in ALL MySQL versions
SELECT DISTINCT
c.name,
c.email,
c.city
FROM current_clients AS c
INNER JOIN event_attendees AS a
ON c.name = a.name
AND c.email = a.email
AND c.city = a.city
ORDER BY c.name;
---
4. EXCEPT (Returns Rows Exclusive to First Query)
Returns rows from the first query that do NOT appear in the second query.
Native SQL Query (Requires MySQL 8.0.31 or Later)
-- Native EXCEPT (MySQL 8.0.31+)
SELECT name, email, city FROM current_clients
EXCEPT
SELECT name, email, city FROM event_attendees
ORDER BY name;
Output
| name | city | |
|---|---|---|
| Amit Verma | amit@example.com | Bangalore |
| Rahul Sharma | rahul@example.com | Mumbai |
Explanation
Priya Patelis present inevent_attendees, so she is excluded.Amit VermaandRahul Sharmaexist only incurrent_clients, so they are returned.
Alternatives for Older MySQL Versions (< 8.0.31)
##### Alternative A: Using NOT EXISTS (Recommended Anti-Join Pattern)
-- Works in ALL MySQL versions
SELECT DISTINCT
c.name,
c.email,
c.city
FROM current_clients AS c
WHERE NOT EXISTS (
SELECT 1
FROM event_attendees AS a
WHERE a.name = c.name
AND a.email = c.email
AND a.city = c.city
)
ORDER BY c.name;
##### Alternative B: Anti-Join with LEFT JOIN and IS NULL
-- Works in ALL MySQL versions
SELECT DISTINCT
c.name,
c.email,
c.city
FROM current_clients AS c
LEFT JOIN event_attendees AS a
ON c.name = a.name
AND c.email = a.email
AND c.city = a.city
WHERE a.attendee_id IS NULL
ORDER BY c.name;
[!NOTE]
Caveats for Older MySQL Emulations:
1.
WHERE a.attendee_id IS NULLchecks a non-nullable primary key to reliably identify unmatched rows.
2.
SELECT DISTINCTis required if the left table contains internal duplicates.
3. Standard
=comparisons treatNULL = NULLasUNKNOWN. In this sample dataset, all columns are definedNOT NULL. If columns can containNULLs, use MySQL's NULL-safe equality operator<=>in join conditions.
---
๐ Database Compatibility Reference Table
| Database Engine | UNION / UNION ALL | INTERSECT | EXCEPT / MINUS | Notes |
|---|---|---|---|---|
| MySQL | โ All Versions | โ 8.0.31+ | โ 8.0.31+ | Use EXISTS / NOT EXISTS for older versions. |
| PostgreSQL | โ All Versions | โ All Versions | โ
All Versions (EXCEPT) | Full ANSI SQL support. |
| SQLite | โ All Versions | โ All Versions | โ
All Versions (EXCEPT) | Supported across all standard releases. |
| SQL Server (T-SQL) | โ All Versions | โ All Versions | โ
All Versions (EXCEPT) | Full support for set operators. |
| Oracle DB | โ All Versions | โ All Versions | โ
All Versions (MINUS) | Oracle uses the keyword MINUS instead of EXCEPT. |
---
โ ๏ธ Common Mistakes & How to Avoid Them
1. Column Count Mismatch
-- โ INCORRECT: First query returns 3 columns, second returns 2 columns!
SELECT name, email, city FROM current_clients
UNION
SELECT name, email FROM event_attendees;
-- โ
CORRECT: Both queries return exactly 3 columns
SELECT name, email, city FROM current_clients
UNION
SELECT name, email, city FROM event_attendees;
2. Incompatible Data Types or Misaligned Column Order
-- โ INCORRECT: Matching name (VARCHAR) with client_id (INT)
SELECT name, client_id FROM current_clients
UNION
SELECT attendee_id, name FROM event_attendees;
-- โ
CORRECT: Align corresponding column types in the same logical order
SELECT client_id, name FROM current_clients
UNION
SELECT attendee_id, name FROM event_attendees;
3. Placing ORDER BY Inside Individual Statements
-- โ INCORRECT: ORDER BY inside the first query causes syntax error!
SELECT name, email FROM current_clients ORDER BY name
UNION
SELECT name, email FROM event_attendees;
-- โ
CORRECT: Place ORDER BY once at the very end of the compound statement
SELECT name, email FROM current_clients
UNION
SELECT name, email FROM event_attendees
ORDER BY name;
4. Assuming Duplicate Removal Compares Only One Column
-- โ MISTAKE: Expecting UNION to deduplicate based solely on 'name'
-- If emails or cities differ, two 'Rahul Sharma' rows WILL be returned!
SELECT name, email, city FROM current_clients
UNION
SELECT name, email, city FROM event_attendees;
-- โ
FACT: UNION compares every selected column in combination (entire row).
5. Assuming Output Row Order Without ORDER BY
-- โ MISTAKE: Relying on default row order without ORDER BY
SELECT name FROM current_clients UNION SELECT name FROM event_attendees;
-- โ
CORRECT: Always add explicit ORDER BY when output order matters
SELECT name FROM current_clients UNION SELECT name FROM event_attendees ORDER BY name;
---
๐งช Step-by-Step Practice Exercises
Run this setup script to create tables for the practice exercises:
DROP TABLE IF EXISTS event_subscribers;
DROP TABLE IF EXISTS online_subscribers;
CREATE TABLE online_subscribers (
sub_id INT PRIMARY KEY,
full_name VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL,
city VARCHAR(50) NOT NULL
);
CREATE TABLE event_subscribers (
sub_id INT PRIMARY KEY,
full_name VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL,
city VARCHAR(50) NOT NULL
);
INSERT INTO online_subscribers VALUES
(1, 'Aarav Patel', 'aarav@example.com', 'Mumbai'),
(2, 'Diya Sen', 'diya@example.com', 'Kolkata'),
(3, 'Karan Roy', 'karan@example.com', 'Delhi');
INSERT INTO event_subscribers VALUES
(10, 'Diya Sen', 'diya@example.com', 'Kolkata'), -- Common subscriber
(20, 'Rohan Verma', 'rohan@example.com', 'Bangalore'),
(30, 'Karan Roy', 'karan@example.com', 'Delhi'); -- Common subscriber
---
Exercise 1: Combine All Unique Contacts (UNION)
Task: Retrieve a unified, deduplicated list of subscriber emails and full names from both online and event sources, sorted by full name.
Required Columns: full_name, email
-- Answer
SELECT full_name, email FROM online_subscribers
UNION
SELECT full_name, email FROM event_subscribers
ORDER BY full_name;
Expected Output
| full_name | |
|---|---|
| Aarav Patel | aarav@example.com |
| Diya Sen | diya@example.com |
| Karan Roy | karan@example.com |
| Rohan Verma | rohan@example.com |
---
Exercise 2: Combine All Registrations Preserving Duplicates (UNION ALL)
Task: Combine all registration records from both tables into a complete activity log without removing duplicate entries.
Required Columns: full_name, email, city
-- Answer
SELECT full_name, email, city FROM online_subscribers
UNION ALL
SELECT full_name, email, city FROM event_subscribers
ORDER BY full_name;
Expected Output
| full_name | city | |
|---|---|---|
| Aarav Patel | aarav@example.com | Mumbai |
| Diya Sen | diya@example.com | Kolkata |
| Diya Sen | diya@example.com | Kolkata |
| Karan Roy | karan@example.com | Delhi |
| Karan Roy | karan@example.com | Delhi |
| Rohan Verma | rohan@example.com | Bangalore |
---
Exercise 3: Find Common Subscribers (INTERSECT)
Task: Find subscribers who registered through both online and event channels.
Required Columns: full_name, email
-- Native Solution (MySQL 8.0.31+)
SELECT full_name, email FROM online_subscribers
INTERSECT
SELECT full_name, email FROM event_subscribers
ORDER BY full_name;
-- Alternative Solution (All MySQL Versions)
SELECT DISTINCT o.full_name, o.email
FROM online_subscribers AS o
WHERE EXISTS (
SELECT 1 FROM event_subscribers AS e
WHERE e.full_name = o.full_name AND e.email = o.email
)
ORDER BY o.full_name;
Expected Output
| full_name | |
|---|---|
| Diya Sen | diya@example.com |
| Karan Roy | karan@example.com |
---
Exercise 4: Find Online-Only Subscribers (EXCEPT)
Task: Find subscribers who registered online but have never attended an event.
Required Columns: full_name, email
-- Native Solution (MySQL 8.0.31+)
SELECT full_name, email FROM online_subscribers
EXCEPT
SELECT full_name, email FROM event_subscribers
ORDER BY full_name;
-- Alternative Solution (All MySQL Versions)
SELECT DISTINCT o.full_name, o.email
FROM online_subscribers AS o
WHERE NOT EXISTS (
SELECT 1 FROM event_subscribers AS e
WHERE e.full_name = o.full_name AND e.email = o.email
)
ORDER BY o.full_name;
Expected Output
| full_name | |
|---|---|
| Aarav Patel | aarav@example.com |
---
Exercise 5: Multi-Source City Directory with Sorting
Task: Create a consolidated list of distinct cities where our subscribers live across both channels, sorted alphabetically.
Required Columns: city
-- Answer
SELECT city FROM online_subscribers
UNION
SELECT city FROM event_subscribers
ORDER BY city;
Expected Output
| city |
|---|
| Bangalore |
| Delhi |
| Kolkata |
| Mumbai |
---
๐ฏ Mini Challenge
Find all products in the catalog that have never been ordered in any customer transaction.
Setup Data
DROP TABLE IF EXISTS order_items;
DROP TABLE IF EXISTS products;
CREATE TABLE products (
product_id INT PRIMARY KEY,
product_name VARCHAR(50) NOT NULL,
price DECIMAL(10, 2) NOT NULL
);
CREATE TABLE order_items (
item_id INT PRIMARY KEY,
order_id INT NOT NULL,
product_id INT NOT NULL,
quantity INT NOT NULL
);
INSERT INTO products (product_id, product_name, price) VALUES
(101, 'Mechanical Keyboard', 89.99),
(102, 'Ergonomic Mouse', 49.99),
(103, '4K Monitor', 349.99),
(104, 'USB-C Hub', 29.99);
INSERT INTO order_items (item_id, order_id, product_id, quantity) VALUES
(1, 5001, 101, 1),
(2, 5001, 102, 2),
(3, 5002, 101, 1);
Challenge Solution
Option A: Native EXCEPT Solution (MySQL 8.0.31+)
SELECT product_id, product_name FROM products
EXCEPT
SELECT p.product_id, p.product_name
FROM products AS p
JOIN order_items AS oi ON p.product_id = oi.product_id
ORDER BY product_id;
Option B: Broadly Compatible NOT EXISTS Anti-Join (All MySQL Versions)
SELECT p.product_id, p.product_name, p.price
FROM products AS p
WHERE NOT EXISTS (
SELECT 1
FROM order_items AS oi
WHERE oi.product_id = p.product_id
)
ORDER BY p.product_id;
Expected Output
| product_id | product_name | price |
|---|---|---|
| 103 | 4K Monitor | 349.99 |
| 104 | USB-C Hub | 29.99 |
[!TIP]
Why
NOT EXISTSis Preferred OverNOT IN:
If a column evaluated inside
NOT IN (SELECT col FROM ...)returns a singleNULLvalue, the entireNOT INexpression evaluates toUNKNOWN, returning 0 rows! UsingNOT EXISTSevaluates row existence directly and is completely safe from the NULL trap.
---
๐ Related Topics
---
๐งญ Navigation
โ SQL Home | โ Previous: Subqueries | Next: Normalization โ