Chapter 12 — Set Operations: UNION & UNION ALL (Set Operations: UNION aur UNION ALL)
1. What is it? (Ye Kya Hai?)
Relational algebra mein, set operators do ya do se zyada independent SELECT queries ke results ko ek single unified result set mein combine karte hain. Jahan ek taraf JOIN tables ko horizontally combine karta hai (ek table ke columns ke aage doosri table ke columns jodkar), wahin Set Operation queries ko vertically combine karta hai (ek query ki rows ke upar doosri query ki rows ko stack karke).
SQL do main set combination operators provide karta hai:
UNION: Do ya do se zyada queries ke output ko combine karta hai, aur automatically ek implicit deduplication phase perform karta hai. Multiple queries mein aane wali identical rows ko condense karke ek single unique row bana deta hai.UNION ALL: Do ya do se zyada queries ke output ko bina kisi deduplication ke combine karta hai. Sabhi queries ki matching rows as it is preserve rehti hain, duplicate rows samet.
Kyunki UNION ko duplicate rows identify aur eliminate karne ke liye combined dataset ko memory mein sort karna padta hai ya temporary hash table banani padti hai, isliye isme significant computational overhead lagta hai. Iske opposite, UNION ALL simply har query ki rows ko sequentially stream kar deta hai, jisse ye kaafi zyada fast hota hai.
2. Why do we use it? (Hum Iska Use Kyun Karte Hain?)
- Aggregating Disparate Tables: Alag-alag tables se similar business entities ko ek unified feed ya report mein merge karna—jaise internal employees aur external contractors ko ek single contact directory mein combine karna.
- Combining Partitioned Datasets: Historic/archival tables aur active transactional tables ko single report ke liye bina schema change ke quickly combine karna.
- High-Speed Non-Overlapping Merges: Jab hume pata hota hai ki dono datasets disjoint hain (unme koi duplicate row nahi ho sakti), toh
UNION ALLuse karke bina sorting overhead ke superfast vertical combination achieve karna.
Strict Schema Compatibility Rules (Schema Compatibility Ke Niyam)
UNION ya UNION ALL use karne ke liye participating queries ko 3 strict relational rules satisfy karne padte hain:
- Identical Column Count: Compound query ke har
SELECTstatement mein exact same number of columns project hone chahiye. - Compatible Data Types: Har query ke corresponding position wale columns (Column 1 to Column 1, Column 2 to Column 2) compatible ya implicitly convertible data types ke hone chahiye. For example,
VARCHARcolumn ko bina explicit cast keDATEcolumn ke sath match nahi kiya ja sakta. - Column Naming Precedence: Final output ke column names, data types, aur aliases chain ki pehli
SELECTquery se decide hote hain.
flowchart TD
subgraph U ["UNION (Deduplicated)"]
direction TB
Q1["Query A (Rows: 1, 2, 3)"] --- O1["Engine Sort & Deduplicate"]
Q2["Query B (Rows: 2, 3, 4)"] --- O1
O1 --> R1["Result: Rows 1, 2, 3, 4"]
end
subgraph UA ["UNION ALL (Fast Concatenation)"]
direction TB
Q3["Query A (Rows: 1, 2, 3)"] --- O2["Direct Stream"]
Q4["Query B (Rows: 2, 3, 4)"] --- O2
O2 --> R2["Result: Rows 1, 2, 3, 2, 3, 4"]
end3. Syntax
-- Standard UNION (Implicit Deduplication)
SELECT column1, column2, ...
FROM table1
WHERE condition1
UNION
SELECT column1, column2, ...
FROM table2
WHERE condition2;
-- High-Performance UNION ALL (Preserves Duplicates)
SELECT column1, column2, ...
FROM table1
UNION ALL
SELECT column1, column2, ...
FROM table2;
-- Global Ordering and Pagination of a Compound Query
(SELECT id, name, created_at FROM table1)
UNION ALL
(SELECT id, name, created_at FROM table2)
ORDER BY created_at DESC
LIMIT 20;4. Basic Example
Geographic locations ke across UNION vs UNION ALL ka example:
USE sql_mastery;
-- UNION: Distinct list of cities where we have either customers OR suppliers
SELECT city, country, 'Customer Base' AS entity_source
FROM customers
WHERE country = 'USA'
UNION
SELECT city, country, 'Supplier Base' AS entity_source
FROM suppliers
WHERE country = 'USA';
-- Compare without the entity_source column:
-- UNION removes duplicate cities (e.g. Seattle)
SELECT city, country FROM customers WHERE country = 'USA'
UNION
SELECT city, country FROM suppliers WHERE country = 'USA';
-- UNION ALL preserves both occurrences of duplicate cities
SELECT city, country FROM customers WHERE country = 'USA'
UNION ALL
SELECT city, country FROM suppliers WHERE country = 'USA';5. Real-World Example
Enterprise security aur audit office ko ek aggregated Corporate Directory & Activity Feed chahiye jo merge kare:
- Internal employees (
employeestable) unke contact details, department, aur'Staff'role ke sath. - External supplier contacts (
supplierstable) contact details, company name, aur'Vendor'role ke sath. - Customer contacts (
customerstable) city, country, aur'Customer'role ke sath. - Final unified directory ko contact name ke according alphabetically sort karna hai.
USE sql_mastery;
(
SELECT
CONCAT(first_name, ' ', last_name) AS full_name,
email AS contact_email,
phone AS contact_phone,
'Internal Staff' AS entity_type,
d.department_name AS affiliation
FROM employees e
LEFT JOIN departments d ON e.department_id = d.department_id
)
UNION ALL
(
SELECT
contact_name AS full_name,
contact_email AS contact_email,
contact_phone AS contact_phone,
'External Vendor' AS entity_type,
supplier_name AS affiliation
FROM suppliers
)
UNION ALL
(
SELECT
CONCAT(first_name, ' ', last_name) AS full_name,
email AS contact_email,
phone AS contact_phone,
'Registered Customer' AS entity_type,
CONCAT(city, ', ', country) AS affiliation
FROM customers
)
ORDER BY full_name ASC;6. Step-by-Step Explanation
- First Query (
employees):- Full name, email, phone extract karta hai, aur
departmentsko join karke internal personnel label karta hai. - Final output column schema define karta hai:
full_name,contact_email,contact_phone,entity_type,affiliation.
- Full name, email, phone extract karta hai, aur
- Second Query (
suppliers):- Supplier contact metadata ko exact usi 5-column positional structure par map karta hai.
supplier_namekoaffiliationcolumn populate karne ke liye position kiya gaya hai.
- Supplier contact metadata ko exact usi 5-column positional structure par map karta hai.
- Third Query (
customers):- Customer personal details ko exact same 5-column layout par map karta hai.
UNION ALLProcessing:- Database engine in-memory deduplication sorting skip kar deta hai aur sabhi source tables ke tuples ko directly ek intermediate result set mein stream kar deta hai.
- Global
ORDER BY full_name ASC:- Teeno source tables ke combined result set ko
full_nameke basis par alphabetically sort kiya jata hai.
- Teeno source tables ke combined result set ko
7. Expected Result
Unified Corporate Directory query ka partial output:
+-------------------+----------------------------+---------------+---------------------+------------------------+
| full_name | contact_email | contact_phone | entity_type | affiliation |
+-------------------+----------------------------+---------------+---------------------+------------------------+
| Aisha Khan | aisha.khan@domain.in | 555-0305 | Registered Customer | Bengaluru, India |
| Alex Morgan | alex.morgan@company.com | 555-0100 | Internal Staff | Engineering |
| Astrid Lind | lind@nordictm.se | 555-0205 | External Vendor | Nordic Timber & Metal |
| Carlos Mendoza | carlos.mendoza@company.com | 555-0108 | Internal Staff | Supply Chain |
| Chloe Dubois | chloe.dubois@orange.fr | 555-0309 | Registered Customer | Lyon, France |
| David Kim | david.kim@company.com | 555-0104 | Internal Staff | Data & Analytics |
| Elena Rostova | elena.rostova@company.com | 555-0105 | Internal Staff | Sales & Marketing |
| Emily Watson | emily.watson@gmail.com | 555-0301 | Registered Customer | San Francisco, USA |
| Greta Weber | weber@eurosmart.de | 555-0203 | External Vendor | EuroSmart Manufacturing|
+-------------------+----------------------------+---------------+---------------------+------------------------+8. Common Mistakes
- Column Count Mismatch:
- Mistake:sql
SELECT employee_id, first_name, email FROM employees UNION SELECT customer_id, first_name FROM customers; -- ONLY 2 COLUMNS! - Error:
ERROR 1222 (21000): The used SELECT statements have a different number of columns. - Rule: Participating sabhi queries mein exact same number of columns project hone chahiye.
- Mistake:
- Defaulting to
UNIONInstead ofUNION ALL:- Mistake: Aise case mein
UNIONlikhna jahan aap pehle se jaante hain ki dono datasets overlap ho hi nahi sakte (jaisecustomersaursuppliersko combine karna). - Consequence: Engine unnecessary duplicate rows check karne ke liye ek expensive temporary table banata hai aur in-memory sort karta hai, jisse query execution kaafi slow ho jata hai.
- Mistake: Aise case mein
- Placing
ORDER BYInside Individual Queries Without Parentheses:- Is tarah likhna:sqlSyntax error throw karta hai. Agar merge se pehle local ordering ya limits apply karni hain, toh har individual query ko parentheses mein enclose karna zaroori hai:
SELECT name FROM tableA ORDER BY name UNION SELECT name FROM tableB;sql(SELECT name FROM tableA ORDER BY name LIMIT 5) UNION ALL (SELECT name FROM tableB ORDER BY name LIMIT 5);
- Is tarah likhna:
- Expecting Column Names from Later Queries to Matter:
- Agar Query 1 column ka alias
account_idrakhti hai aur Query 2 usi position ke column ka aliascustomer_numberrakhti hai, toh output column ka naamaccount_idhi rahega. Hamesha pehleSELECTstatement ke column aliases verify karo.
- Agar Query 1 column ka alias
9. Best Practices
- Default to
UNION ALLUnless Deduplication Is Explicitly Required:- By default hamesha
UNION ALLuse karo.UNIONtabhi use karo jab duplicate rows aane ki possibility ho aur business requirement unhe remove karna mandate karti ho.
- By default hamesha
- Always Align Column Data Types Positively:
- Implicit type coercion par rely mat karo (jaise integer column ko string column ke sath merge karna). Types ko harmonize karne ke liye explicit
CAST()functions use karo:sqlSELECT CAST(employee_id AS CHAR(20)) FROM employees UNION ALL SELECT reference_code FROM external_partners;
- Implicit type coercion par rely mat karo (jaise integer column ko string column ke sath merge karna). Types ko harmonize karne ke liye explicit
- Use Static Literal Tags to Identify Row Provenance:
- Jab alag-alag tables ko combine karein, toh ek constant string literal (jaise
'Order','Refund','Adjustment') zaroor include karein taaki client code har row ka origin asaani se identify kar sake.
- Jab alag-alag tables ko combine karein, toh ek constant string literal (jaise
10. Practice Questions
Easy
customerstable kecitycolumn aurdepartmentstable kelocationcolumn koUNIONuse karke single list mein combine karne ke liye query likho.employeesaurcustomersdono tables ke sabhi email addresses ko list karne ke liyeUNION ALLquery likho.- Question 1 aur Question 2 ke answers mein row count ka jo farak aayega use explain karo.
Medium
- Ek aisi query likho jo
unit_price > 500wale sabhi products aurstock_quantity < 20wale sabhi products ko combine kare, jismeUNIONka use ho taaki dono criteria satisfy karne wale products sirf ek hi baar list hon. - Financial movements ka unified ledger generate karne ke liye query likho:
orderstable se positive order values jahanstatus = 'Delivered'ho (tagged as'REVENUE')orderstable se shipping costs jahanshipping_fee > 0ho (tagged as'EXPENSE')- Unified ledger ko date descending ke according order karo.
- Active customers aur inactive customers ke names ko do alag partitions mein combine karne ki query likho, jahan har row unke respective status ke sath labeled ho.
Difficult
departmentsauremployeeske beechLEFT JOIN,RIGHT JOIN, aurUNIONuse karkeFULL OUTER JOINemulate karne wali query likho. Verify karo ki bina employees wale departments aur bina department wale employees dono result mein shamil hon.- Aisi query construct karo jo top 2 highest-paid employees aur top 2 lowest-paid employees ko
UNION ALLke zariye merge kare, aur overall result salary descending ke hisab se sorted ho. (Hint: Parenthesized subqueries ke sath individualLIMITclauses use karo).
11. Interview Questions
Q1: What is the mechanical difference between UNION and UNION ALL in terms of execution mechanics and performance?
Answer:
UNIONdo queries ke result sets ko concatenate karta hai aur phir ek implicit deduplication step perform karta hai. Aisa karne ke liye, database engine ko combined rows ko memory ya on-disk temporary table mein dump karna padta hai, sabhi projected columns par records ko sort karna padta hai (ya hash set build karna padta hai), aur duplicate rows ko eliminate karna padta hai. Isme heavy CPU, memory, aur disk I/O lagti hai.UNION ALLpure vertical concatenation perform karta hai. Engine Query 1 se aane wali rows ko directly client ya parent pipeline ko stream karta hai, jiske turant baad Query 2 ki rows stream hoti hain. Isme zero sorting, hashing, ya row comparisons hote hain. Is wajah seUNION ALLbohot zyada fast hota hai aur jab records distinct hote hain ya duplicates acceptable hote hain toh hamesha isi ko prefer karna chahiye.
Q2: What are the three relational rules that two queries must satisfy to be combined using a Set Operator?
Answer:
- Identical Degree (Column Count): Dono
SELECTqueries mein exact same number of columns project hone chahiye. - Type Compatibility: Corresponding positions wale columns ke data types identical ya database engine dwara implicitly convertible hone chahiye (jaise integer aur float chal jayenge, lekin date aur binary blob nahi).
- Order of Evaluation: Final output result set ke column names, aliases, aur character collations union chain ke pehli
SELECTstatement se decide hote hain.
Q3: How can you apply an ORDER BY to an entire compound query versus applying an ORDER BY to an individual branch of a UNION?
Answer:
- Poore compound result set par
ORDER BYlagane ke liye, aakhiri query ke bilkul end mein bina parentheses ke singleORDER BYclause likha jata hai. Ye poore combined output par evaluate hota hai. - Individual branches par
ORDER BY(aur usually sath meinLIMIT) lagane ke liye, har branch query ko uske apne parentheses mein wrap karna padta hai:sql(SELECT * FROM table1 ORDER BY score DESC LIMIT 5) UNION ALL (SELECT * FROM table2 ORDER BY score DESC LIMIT 5) ORDER BY score DESC;
12. Quick Revision
UNIONquery results ko vertically stack karta hai aur duplicate rows eliminate karta hai (sorting overhead lagta hai).UNION ALLquery results ko vertically stack karta hai bina duplicates hataye (maximum performance).- Sabhi combined queries mein same number of columns aur compatible data types hone chahiye.
- Final result set ke column names aur aliases pehli query decide karti hai.
- Branch-level
LIMITyaORDER BYuse karte waqt individual queries ko parentheses mein wrap karein.