๐ SQL UNION โ Combine Results from Multiple Queries
The
UNIONoperator in SQL is used to merge the results of two or moreSELECTqueries into a single result set, eliminating duplicate rows by default.
Itโs an essential SQL concept for data integration, multi-source querying, and building consolidated views across tables.
๐ What is SQL UNION?
UNION is a set operator that combines rows from multiple SELECT statements into one unified result set.
It automatically removes duplicate records unless you use UNION ALL.
๐งพ SQL UNION Syntax
SELECT column1, column2 FROM table1
UNION
SELECT column1, column2 FROM table2;
โ Rules:
- Each
SELECTquery must have the same number of columns - The data types of corresponding columns must be compatible
- Column names in the final result are taken from the first
SELECTstatement
โ Example: Combining Employee Tables
Suppose you have two tables:
full_time_employees
| name | department |
|---|---|
| Alice | IT |
| Bob | HR |
contract_employees
| name | department |
|---|---|
| Charlie | IT |
| Alice | IT |
You want a combined list of all employees:
SELECT name, department FROM full_time_employees
UNION
SELECT name, department FROM contract_employees;
๐งพ Result:
| name | department |
|---|---|
| Alice | IT |
| Bob | HR |
| Charlie | IT |
Notice: โAliceโ appears only once because
UNIONremoves duplicates.
๐ Want to Keep Duplicates? Use UNION ALL
SELECT name, department FROM full_time_employees
UNION ALL
SELECT name, department FROM contract_employees;
๐งพ Result with duplicates:
| name | department |
|---|---|
| Alice | IT |
| Bob | HR |
| Charlie | IT |
| Alice | IT |
๐ง When to Use SQL UNION
- โ Combine rows from multiple tables with similar structures
- ๐ Consolidate historical and current records
- ๐ Merge data from archived and active sources
- ๐ผ Build unified views across systems or timeframes
โ ๏ธ Common Mistakes to Avoid
| Mistake | Solution |
|---|---|
| Mismatched number of columns | Ensure each SELECT has the same number of columns |
| Incompatible column data types | Use CAST() or CONVERT() to align data types |
| Assuming duplicate rows are retained | Use UNION ALL instead of UNION |
| Forgetting column aliases in final output | Use column aliases in the first query if needed |
๐งฎ Real-Life Use Cases
- Combine online vs offline sales reports
- Merge support ticket data from multiple systems
- Aggregate leads from various marketing sources
- Create master lists of users or products from multiple regions
๐ SQL UNION vs JOIN โ Whatโs the Difference?
| Feature | UNION | JOIN |
|---|---|---|
| Rows or columns | Combines rows | Combines columns |
| Structure | Requires same number of columns | Requires keys to match rows |
| Use case | Merging similar data from multiple tables | Enriching data with related information |
๐ Summary
- SQL
UNIONmerges results from multiple queries into one - Removes duplicates by default
- Use
UNION ALLif you want to retain all rows - Make sure the number and types of columns match
- Great for combining reports, records, or views across similar structures