The SQL UNION and UNION ALL operators are used to combine the results of two or more SELECT queries into a single result set. SQL UNION and UNION ALL Practice questions with solutions help to build concepts.
These operators are useful when data is stored in multiple tables with the same structure, such as:
- Multiple company branches
- Monthly sales tables
- Archived and current records
- Different warehouses
- Student batches
Instead of running separate queries, you can merge the results into one report.
Difference Between UNION and UNION ALL
| UNION | UNION ALL |
|---|---|
| Removes duplicate records | Keeps duplicate records |
| Slightly slower because duplicates are removed | Faster because duplicates are not removed |
| Best for unique reports | Best when every record is important |
Rules for Using UNION
Before using UNION, remember these rules:
- Every
SELECTstatement must have the same number of columns. - Corresponding columns must have compatible data types.
- Column names in the final result are taken from the first SELECT statement.
Sample Tables
batch_a
| student_id | student_name |
|---|---|
| 101 | Rahul |
| 102 | Neha |
| 103 | Amit |
batch_b
| student_id | student_name |
|---|---|
| 104 | Sneha |
| 105 | Priya |
| 106 | Rohit |
1. SQL UNION to Combine Students from Two Batches
Problem Statement
A training institute stores students in two separate tables:
- Batch A
- Batch B
Write an SQL query to display the names of all students from both batches.
SQL Solution
SELECT student_name
FROM batch_a
UNION
SELECT student_name
FROM batch_b;
Sample Output
| student_name |
|---|
| Rahul |
| Neha |
| Amit |
| Sneha |
| Priya |
| Rohit |
Explanation
The first query retrieves students from Batch A.
The second query retrieves students from Batch B.
The UNION operator combines both results into one list while removing duplicate values if they exist.
Concepts Covered
- UNION
- Combining Multiple Queries
- Removing Duplicates
2. SQL UNION to Combine Employee Lists from Two Company Branches
Problem Statement
A company has two office branches:
- Delhi
- Noida
Write an SQL query to display all employee names.
Sample Tables
delhi_branch
| employee_id | employee_name |
|---|---|
| 1 | Aman |
| 2 | Riya |
| 3 | Karan |
noida_branch
| employee_id | employee_name |
|---|---|
| 4 | Vikas |
| 5 | Neha |
| 6 | Simran |
SQL Solution
SELECT employee_name
FROM delhi_branch
UNION
SELECT employee_name
FROM noida_branch;
Sample Output
| employee_name |
|---|
| Aman |
| Riya |
| Karan |
| Vikas |
| Neha |
| Simran |
Explanation
The UNION operator merges employee names from both branches into a single result set.
Duplicate names (if any) would appear only once.
Concepts Covered
- UNION
- Multiple Tables
- Employee Database
3. SQL UNION to Display Customer Cities from Two Databases
Problem Statement
A retail company stores customers in two regional databases.
Display all unique customer cities.
Sample Tables
north_customers
| customer_name | city |
|---|---|
| Rahul | Delhi |
| Neha | Noida |
| Amit | Lucknow |
south_customers
| customer_name | city |
|---|---|
| Priya | Chennai |
| Rohit | Hyderabad |
| Sneha | Delhi |
SQL Solution
SELECT city
FROM north_customers
UNION
SELECT city
FROM south_customers;
Sample Output
| city |
|---|
| Delhi |
| Noida |
| Lucknow |
| Chennai |
| Hyderabad |
Explanation
Although Delhi exists in both tables, it appears only once because UNION automatically removes duplicate values.
Concepts Covered
- UNION
- DISTINCT Results
- Customer Database
4. SQL UNION to Combine Product Lists from Two Warehouses
Problem Statement
An e-commerce company stores inventory in two different warehouses.
Write an SQL query to display a unique list of all available products from both warehouses.
Sample Tables
warehouse_a
| product_id | product_name |
|---|---|
| 101 | Laptop |
| 102 | Keyboard |
| 103 | Mouse |
| 104 | Monitor |
warehouse_b
| product_id | product_name |
|---|---|
| 103 | Mouse |
| 105 | Printer |
| 106 | Tablet |
| 107 | Headphones |
SQL Solution
SELECT
product_name
FROM warehouse_a
UNION
SELECT
product_name
FROM warehouse_b;
Sample Output
| product_name |
|---|
| Laptop |
| Keyboard |
| Mouse |
| Monitor |
| Printer |
| Tablet |
| Headphones |
Explanation
The UNION operator combines products from both warehouses into one list.
Although Mouse exists in both tables, it appears only once because UNION removes duplicate records.
This query is useful for:
- Inventory Reports
- Warehouse Management
- Stock Availability
- Product Catalog Generation
Concepts Covered
- UNION
- Removing Duplicate Records
- Inventory Database
5. SQL UNION to Combine Teacher Records from Two Schools
Problem Statement
An educational organization manages two schools.
Write an SQL query to display a unique list of teachers from both schools.
Sample Tables
school_a
| teacher_id | teacher_name |
|---|---|
| 1 | Anita |
| 2 | Rakesh |
| 3 | Suman |
school_b
| teacher_id | teacher_name |
|---|---|
| 4 | Karan |
| 5 | Suman |
| 6 | Megha |
SQL Solution
SELECT
teacher_name
FROM school_a
UNION
SELECT
teacher_name
FROM school_b;
Sample Output
| teacher_name |
|---|
| Anita |
| Rakesh |
| Suman |
| Karan |
| Megha |
Explanation
The teacher Suman appears in both schools.
Since the query uses UNION, duplicate names are removed automatically.
This type of report is commonly used by:
- Educational Groups
- School Management Systems
- Teacher Allocation Reports
Concepts Covered
- UNION
- Duplicate Elimination
- School Database
Difference Between UNION and UNION ALL
Consider the following example.
warehouse_a
| product_name |
|---|
| Laptop |
| Mouse |
warehouse_b
| product_name |
|---|
| Mouse |
| Printer |
Using UNION
SELECT product_name
FROM warehouse_a
UNION
SELECT product_name
FROM warehouse_b;
Result
| product_name |
|---|
| Laptop |
| Mouse |
| Printer |
Duplicate values are removed.
Using UNION ALL
SELECT product_name
FROM warehouse_a
UNION ALL
SELECT product_name
FROM warehouse_b;
Result
| product_name |
|---|
| Laptop |
| Mouse |
| Mouse |
| Printer |
Duplicate values are not removed.
6. SQL UNION ALL to Display Orders from Two Different Stores
Problem Statement
A retail company has two physical stores.
Management wants to generate a report showing all customer orders from both stores.
Even if the same customer places orders in both stores, every order should appear.
Write an SQL query using UNION ALL.
Sample Tables
store_a_orders
| order_id | customer_name | amount |
|---|---|---|
| 1001 | Rahul | 1500 |
| 1002 | Neha | 2200 |
| 1003 | Amit | 1800 |
store_b_orders
| order_id | customer_name | amount |
|---|---|---|
| 2001 | Rahul | 900 |
| 2002 | Priya | 2700 |
| 2003 | Sneha | 1600 |
SQL Solution
SELECT
customer_name,
amount
FROM store_a_orders
UNION ALL
SELECT
customer_name,
amount
FROM store_b_orders;
Sample Output
| customer_name | amount |
|---|---|
| Rahul | 1500 |
| Neha | 2200 |
| Amit | 1800 |
| Rahul | 900 |
| Priya | 2700 |
| Sneha | 1600 |
Explanation
Unlike UNION, UNION ALL does not remove duplicate records.
Rahul appears twice because he placed orders at both stores.
This is useful for:
- Sales Reports
- Order Tracking
- Revenue Analysis
Concepts Covered
- UNION ALL
- Duplicate Records
- Sales Database
7. SQL UNION ALL to Combine Monthly Sales Reports
Problem Statement
A company stores sales data separately for January and February.
Generate a report containing all sales transactions.
Sample Tables
january_sales
| sale_id | salesperson | amount |
|---|---|---|
| 1 | Aman | 25000 |
| 2 | Riya | 18000 |
february_sales
| sale_id | salesperson | amount |
|---|---|---|
| 3 | Aman | 22000 |
| 4 | Neha | 26000 |
SQL Solution
SELECT
salesperson,
amount
FROM january_sales
UNION ALL
SELECT
salesperson,
amount
FROM february_sales;
Sample Output
| salesperson | amount |
|---|---|
| Aman | 25000 |
| Riya | 18000 |
| Aman | 22000 |
| Neha | 26000 |
Explanation
The salesperson Aman appears twice because he made sales in both months.
UNION ALL preserves every sales transaction.
Concepts Covered
- UNION ALL
- Monthly Reports
- Sales Analytics
8. SQL UNION ALL to Merge Website Visitor Logs
Problem Statement
A website stores visitor logs in separate tables for desktop and mobile users.
Generate a report containing all website visits.
Sample Tables
desktop_visitors
| visitor_id | visitor_name |
|---|---|
| 101 | Rahul |
| 102 | Neha |
mobile_visitors
| visitor_id | visitor_name |
|---|---|
| 201 | Rahul |
| 202 | Sneha |
SQL Solution
SELECT
visitor_name
FROM desktop_visitors
UNION ALL
SELECT
visitor_name
FROM mobile_visitors;
Sample Output
| visitor_name |
|---|
| Rahul |
| Neha |
| Rahul |
| Sneha |
Explanation
Rahul visited from both desktop and mobile.
Since each visit is important, UNION ALL keeps both records.
Concepts Covered
- UNION ALL
- Visitor Tracking
- Website Analytics
9. SQL UNION ALL to Display Customer Support Tickets
Problem Statement
A company maintains customer support tickets in two branches.
Display all support tickets from both branches.
Sample Tables
delhi_support
| ticket_id | customer_name |
|---|---|
| 1 | Rahul |
| 2 | Neha |
noida_support
| ticket_id | customer_name |
|---|---|
| 3 | Rahul |
| 4 | Priya |
SQL Solution
SELECT
customer_name
FROM delhi_support
UNION ALL
SELECT
customer_name
FROM noida_support;
Sample Output
| customer_name |
|---|
| Rahul |
| Neha |
| Rahul |
| Priya |
Explanation
Rahul raised support tickets in both branches.
Each support request must remain separate, making UNION ALL the correct choice.
Concepts Covered
- UNION ALL
- Customer Support
- Ticket Management
10. SQL UNION ALL to Combine Inventory Transactions
Problem Statement
An inventory management system stores warehouse transactions separately.
Display all inventory transactions from both warehouses.
Sample Tables
warehouse_a_transactions
| transaction_id | product_name |
|---|---|
| 1 | Laptop |
| 2 | Mouse |
warehouse_b_transactions
| transaction_id | product_name |
|---|---|
| 3 | Laptop |
| 4 | Printer |
SQL Solution
SELECT
product_name
FROM warehouse_a_transactions
UNION ALL
SELECT
product_name
FROM warehouse_b_transactions;
Sample Output
| product_name |
|---|
| Laptop |
| Mouse |
| Laptop |
| Printer |
Explanation
The product Laptop appears twice because inventory transactions from both warehouses must be preserved.
This type of report is commonly used for:
- Stock Movement Reports
- Warehouse Audits
- Inventory Management
Concepts Covered
- UNION ALL
- Inventory Reports
- Warehouse Database
11. SQL UNION with ORDER BY
Problem Statement
A university stores student records in two different tables:
- Morning Batch
- Evening Batch
Display a combined list of students and sort the result alphabetically.
Sample Tables
morning_batch
| student_id | student_name |
|---|---|
| 101 | Rahul |
| 102 | Neha |
| 103 | Amit |
evening_batch
| student_id | student_name |
|---|---|
| 201 | Sneha |
| 202 | Priya |
| 203 | Rohit |
SQL Solution
SELECT
student_name
FROM morning_batch
UNION
SELECT
student_name
FROM evening_batch
ORDER BY student_name;
Sample Output
| student_name |
|---|
| Amit |
| Neha |
| Priya |
| Rahul |
| Rohit |
| Sneha |
Explanation
The ORDER BY clause is written after the final SELECT statement.
It sorts the combined result returned by the UNION.
Concepts Covered
- UNION
- ORDER BY
- Sorting Combined Results
12. SQL UNION with WHERE Clause
Problem Statement
A company has two branches.
Display employees whose salary is greater than ₹50,000 from both branches.
Sample Tables
delhi_employees
| employee_name | salary |
|---|---|
| Aman | 45000 |
| Riya | 65000 |
| Neha | 72000 |
noida_employees
| employee_name | salary |
|---|---|
| Vikas | 58000 |
| Karan | 47000 |
| Simran | 80000 |
SQL Solution
SELECT
employee_name,
salary
FROM delhi_employees
WHERE salary > 50000
UNION
SELECT
employee_name,
salary
FROM noida_employees
WHERE salary > 50000;
Sample Output
| employee_name | salary |
|---|---|
| Riya | 65000 |
| Neha | 72000 |
| Vikas | 58000 |
| Simran | 80000 |
Explanation
The WHERE clause filters records before the UNION operation.
Only employees earning more than ₹50,000 are included in the final result.
Concepts Covered
- UNION
- WHERE
- Filtering Data
13. SQL UNION Using Column Aliases
Problem Statement
A retail company stores customer information in two databases.
Display a single report using meaningful column names.
Sample Tables
online_customers
| customer_name |
|---|
| Rahul |
| Neha |
offline_customers
| customer_name |
|---|
| Amit |
| Priya |
SQL Solution
SELECT
customer_name AS customer
FROM online_customers
UNION
SELECT
customer_name
FROM offline_customers;
Sample Output
| customer |
|---|
| Rahul |
| Neha |
| Amit |
| Priya |
Explanation
The alias defined in the first SELECT statement becomes the column name of the final result.
Aliases improve readability in reports and dashboards.
Concepts Covered
- UNION
- Column Alias
- Reporting
14. SQL UNION with Aggregate Functions
Problem Statement
A company wants to calculate the total sales for two separate quarters.
Display the combined total sales.
Sample Tables
q1_sales
| amount |
|---|
| 15000 |
| 20000 |
| 18000 |
q2_sales
| amount |
|---|
| 17000 |
| 22000 |
| 25000 |
SQL Solution
SELECT
SUM(amount) AS total_sales
FROM q1_sales
UNION
SELECT
SUM(amount)
FROM q2_sales;
Sample Output
| total_sales |
|---|
| 53000 |
| 64000 |
Explanation
Each SELECT statement calculates the total sales for one quarter.
UNION combines both summary results into a single report.
Concepts Covered
- UNION
- Aggregate Functions
- SUM()
15. SQL UNION to Generate a Multi-Source Business Report
Problem Statement
A company wants to create a report showing all active customers from:
- Website registrations
- Mobile application registrations
Display one combined list.
Sample Tables
website_users
| customer_name |
|---|
| Rahul |
| Neha |
| Amit |
mobile_users
| customer_name |
|---|
| Rahul |
| Priya |
| Sneha |
SQL Solution
SELECT
customer_name
FROM website_users
UNION
SELECT
customer_name
FROM mobile_users;
Sample Output
| customer_name |
|---|
| Rahul |
| Neha |
| Amit |
| Priya |
| Sneha |
Explanation
The same customer may register using multiple platforms.
Since UNION removes duplicate values automatically, Rahul appears only once in the final report.
This type of query is commonly used in:
- CRM Systems
- Customer Analytics
- Business Intelligence Dashboards
- Marketing Reports
Concepts Covered
- UNION
- Duplicate Removal
- Business Reporting
Chapter Summary
In this chapter, you learned how to combine the results of multiple SQL queries using the UNION and UNION ALL operators.
These operators are extremely useful when data is stored in multiple tables with the same structure, such as different branches, warehouses, monthly reports, or archived records.
Throughout this chapter, you practiced:
- Combining records from multiple tables using
UNION - Removing duplicate records automatically with
UNION - Preserving duplicate records using
UNION ALL - Using
ORDER BYwithUNION - Using
WHEREclauses withUNION - Using column aliases in combined queries
- Combining aggregated results
- Building real-world business reports from multiple data sources
These concepts are commonly used in reporting systems, business intelligence dashboards, and SQL interview questions.
Key Takeaways
UNIONcombines the results of two or moreSELECTstatements.UNIONautomatically removes duplicate rows.UNION ALLcombines results without removing duplicates.UNION ALLis generally faster thanUNIONbecause it skips duplicate elimination.- All
SELECTstatements must return the same number of columns. - Corresponding columns should have compatible data types.
ORDER BYis written only once at the end of the final query.WHEREclauses can be applied independently within eachSELECT.UNIONis commonly used for combining data from different branches, departments, or time periods.- Understanding the difference between
UNIONandUNION ALLis a frequent SQL interview topic.
Frequently Asked Questions (FAQs)
1. What is SQL UNION?
The UNION operator combines the results of two or more SELECT statements and removes duplicate rows.
Example:
SELECT student_name
FROM batch_a
UNION
SELECT student_name
FROM batch_b;
2. What is SQL UNION ALL?
UNION ALL combines the results of multiple queries without removing duplicate records.
Example:
SELECT customer_name
FROM store_a
UNION ALL
SELECT customer_name
FROM store_b;
3. What is the difference between UNION and UNION ALL?
| UNION | UNION ALL |
|---|---|
| Removes duplicate rows | Keeps duplicate rows |
| Slightly slower | Faster |
| Best for unique reports | Best for transaction logs and complete datasets |
4. What conditions must be satisfied before using UNION?
Before using UNION:
- Both queries must return the same number of columns.
- The corresponding columns must have compatible data types.
- The column names in the final result come from the first query.
5. Can I use ORDER BY with UNION?
Yes.
The ORDER BY clause is written after the last SELECT statement.
Example:
SELECT student_name
FROM batch_a
UNION
SELECT student_name
FROM batch_b
ORDER BY student_name;
6. Can I use WHERE with UNION?
Yes.
Each SELECT statement can have its own WHERE clause.
Example:
SELECT employee_name
FROM delhi_employees
WHERE salary > 50000
UNION
SELECT employee_name
FROM noida_employees
WHERE salary > 50000;
7. When should I use UNION instead of JOIN?
Use UNION when you want to append rows from multiple queries.
Use JOIN when you want to combine related columns from different tables based on matching keys.
8. Where are UNION and UNION ALL used in real-world applications?
They are commonly used in:
- Multi-branch company reports
- Monthly sales reports
- Warehouse inventory reports
- Banking transaction history
- CRM systems
- Student databases
- Hospital records
- Business Intelligence dashboards
- Financial reports
- Data migration projects
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
