Table of Contents
- Introduction
- What is
UNIONin SQL? - When to Use
UNION - Basic Syntax of
UNION - What is
UNION ALL? - Difference Between
UNIONandUNION ALL - Requirements for Using
UNION - Matching Columns and Data Types
- Using Aliases in
UNIONQueries UNIONwith Literal Data- Ordering the Final Output
- Filtering Results in
UNION UNIONwith Different Table Sources- Real-World Example: Employees and Contractors
- Performance Comparison:
UNIONvsUNION ALL - Using
DISTINCTwithUNION ALL - Combining
UNIONwith Joins or Subqueries - Common Mistakes with
UNION - Best Practices for
UNIONUsage - Summary and What’s Next
1. Introduction
In SQL, there are situations where you need to combine results from multiple queries. Whether it’s data coming from different tables or different sources, UNION and UNION ALL help you stitch them together into a single result set.
2. What is UNION in SQL?
UNION is a SQL operator used to combine the result sets of two or more SELECT statements, removing duplicate rows.
3. When to Use UNION
- To merge results from multiple tables with similar structure
- To show combined data (e.g., full employee list from different departments)
- To perform reporting or data consolidation tasks
- To avoid multiple queries in application code
4. Basic Syntax of UNION
sqlCopyEditSELECT column1, column2 FROM table1
UNION
SELECT column1, column2 FROM table2;
5. What is UNION ALL?
UNION ALL works like UNION but does not eliminate duplicate rows. It returns all records, including duplicates.
sqlCopyEditSELECT name FROM employees
UNION ALL
SELECT name FROM contractors;
6. Difference Between UNION and UNION ALL
| Feature | UNION | UNION ALL |
|---|---|---|
| Duplicate removal | Yes | No |
| Performance | Slower (due to sorting) | Faster |
| Use case | When unique values needed | When preserving duplicates |
7. Requirements for Using UNION
- Number of columns in all
SELECTstatements must match - Data types must be compatible (e.g.,
INTwithINT,VARCHARwithVARCHAR) - Order of columns matters
8. Matching Columns and Data Types
Valid Example:
sqlCopyEditSELECT id, name FROM employees
UNION
SELECT id, name FROM contractors;
Invalid Example (throws error):
sqlCopyEditSELECT id, name FROM employees
UNION
SELECT salary, hire_date FROM contractors;
Column meanings must align across queries.
9. Using Aliases in UNION Queries
Only the first SELECT query’s aliases are retained in the final result:
sqlCopyEditSELECT name AS full_name FROM employees
UNION
SELECT name FROM contractors;
Result column will be full_name.
10. UNION with Literal Data
sqlCopyEditSELECT 'employee' AS type, name FROM employees
UNION
SELECT 'contractor', name FROM contractors;
Adds a column to indicate data origin.
11. Ordering the Final Output
You must use ORDER BY after the last SELECT:
sqlCopyEditSELECT name FROM employees
UNION
SELECT name FROM contractors
ORDER BY name;
You cannot use ORDER BY inside each individual SELECT.
12. Filtering Results in UNION
Use WHERE clauses in individual queries:
sqlCopyEditSELECT name FROM employees WHERE status = 'active'
UNION
SELECT name FROM contractors WHERE status = 'active';
Filters are applied before combining results.
13. UNION with Different Table Sources
Even if table structures differ, you can align columns using SELECT:
sqlCopyEditSELECT id, name, NULL AS rate FROM employees
UNION
SELECT id, NULL, hourly_rate FROM freelancers;
Use NULL or constants to fill missing values.
14. Real-World Example: Employees and Contractors
sqlCopyEditSELECT id, name, 'Employee' AS role FROM employees
UNION
SELECT id, name, 'Contractor' FROM contractors;
Combines people from both tables and labels their source.
15. Performance Comparison: UNION vs UNION ALL
| Operator | Speed | Use When |
|---|---|---|
UNION | Slower | You need unique rows |
UNION ALL | Faster | Duplicates are acceptable or desired |
16. Using DISTINCT with UNION ALL
You can simulate UNION using DISTINCT:
sqlCopyEditSELECT DISTINCT * FROM (
SELECT name FROM table1
UNION ALL
SELECT name FROM table2
) AS combined;
Sometimes useful when applying DISTINCT after a transformation.
17. Combining UNION with Joins or Subqueries
sqlCopyEditSELECT u.id, u.name FROM users u
JOIN orders o ON u.id = o.user_id
UNION
SELECT id, name FROM archived_users;
You can UNION a join result with another query as long as the columns align.
18. Common Mistakes with UNION
| Mistake | Explanation |
|---|---|
| Mismatched columns | Number or type of columns must match |
Using ORDER BY inside both queries | Use it only at the end |
| Expecting both aliases to persist | Only first SELECT defines column labels |
19. Best Practices for UNION Usage
- Use
UNION ALLwhen performance matters and duplicates don’t - Always align column order and data types
- Use column aliases in the first
SELECT - Comment each
SELECTfor readability if complex - Use subqueries to preprocess before
UNIONif needed
20. Summary and What’s Next
UNION and UNION ALL are powerful SQL tools for combining multiple datasets into a single result set. Use UNION for uniqueness and UNION ALL for speed and completeness. Mastering these helps you write efficient and expressive SQL for reporting, analytics, and data integration.

