- SQL Basics
- SQL Introduction
- SQL Syntax
- SQL Sample Database
- SQL SELECT
- SQL WHERE
- SQL ORDER BY
- SQL DISTINCT
- SQL LIMIT
- SQL FETCH
- SQL AND
- SQL OR
- SQL BETWEEN
- SQL IN
- SQL LIKE
- SQL IS NULL
- SQL Comparison Operators
- SQL Logical Operators
- SQL Alias
- SQL CASE
- Joins and Subqueries
- SQL INNER JOIN
- SQL LEFT JOIN
- SQL RIGHT JOIN
- SQL FULL OUTER JOIN
- SQL SELF JOIN
- SQL CROSS JOIN
- SQL Subquery
- SQL Correlated Subquery
- SQL UNION
- SQL INTERSECT
- SQL EXCEPT
- Aggregate Functions
- SQL AVG
- SQL COUNT
- SQL MAX
- SQL MIN
- SQL SUM
- SQL GROUP BY
- SQL HAVING
- SQL ROLLUP
- SQL CUBE
- SQL GROUPING SETS
- Database Management
- SQL CREATE DATABASE
- SQL ALTER DATABASE
- SQL DROP DATABASE
- SQL BACKUP DATABASE
- SQL SHOW DATABASES
- SQL SELECT DATABASE
- Table Management
- SQL CREATE TABLE
- SQL ALTER TABLE
- SQL ADD COLUMN
- SQL DROP COLUMN
- SQL DROP TABLE
- SQL TRUNCATE TABLE
- SQL SHOW TABLES
- SQL RENAME TABLE
- SQL Constraints
- SQL Primary Key
- SQL Foreign Key
- SQL UNIQUE Constraint
- SQL CHECK Constraint
- SQL NOT NULL Constraint
SQL UNION
The UNION operator in SQL is a powerful tool used to combine the results of two or more SELECT statements into a single, unified result set. Think of it as "stacking" the results of one query on top of another vertically.
By default, UNION performs a distinct operation, meaning it automatically removes duplicate rows from the final output. For a UNION to work, all SELECT statements must follow a strict set of rules regarding column count and data types.
Syntax of SQL UNION
SELECT column1, column2, ...
FROM table1
WHERE condition
UNION
SELECT column1, column2, ...
FROM table2
WHERE condition;
- SELECT statements: These are the individual queries you want to merge. You can union as many SELECT statements as you need, not just two.
- UNION: The operator that tells the database to merge the result sets and filter out duplicates.
- Columns: Every
SELECTstatement must have the same number of columns in the same order.
Key Points
- Duplicate Removal: The standard
UNIONoperator removes duplicate records, which requires the database to perform extra work (sorting and filtering). - Union All: If you want to keep every single row, including duplicates, use
UNION ALL. This is significantly faster because the database doesn't have to scan for duplicates. - Data Compatibility: Corresponding columns must have compatible data types. For example, you cannot union a column of strings with a column of integers in many SQL dialects without explicit casting.
- Column Names: The column names in the final result set are usually taken from the column headers of the first
SELECTstatement.
UNION ALL by default if you know for a fact that your datasets won't overlap or if you don't care about duplicates. It is much more performant than a standard UNION.
Example of SQL UNION
Imagine you are managing a company that keeps employee records for different departments in separate tables. You need a master list of all staff names and their departments for a company-wide directory.
Employees Table 1 (Department 1):
Employees Table 2 (Department 2):
To generate a single list of all unique employees across both departments, run this query:
SELECT name, department
FROM employees_1
UNION
SELECT name, department
FROM employees_2;
Output:
Explanation:
- The SQL engine executes the first query, then the second.
- It merges the two lists and looks for any identical rows.
- Since all employees in our example are unique, they all appear in the final output.
ORDER BY inside each individual SELECT statement. You can only use ORDER BY once, at the very end of the final SELECT statement, to sort the entire combined result set.
SQL UNION with Different Conditions
You can apply different WHERE clauses to each part of the UNION. This is particularly useful when you need to pull specific, different subsets of data into one report. For instance, if you only want HR staff from the first table and specific IT staff from the second:
SELECT name, department
FROM employees_1
WHERE department = 'HR'
UNION
SELECT name, department
FROM employees_2
WHERE department = 'IT' AND name LIKE 'S%';
Output:
Using UNION ALL
The UNION ALL operator works exactly like UNION, but it skips the duplicate removal step. If "John" appeared in both tables, UNION ALL would show him twice.
-- This will show every row from both tables, duplicates included.
SELECT name, department
FROM employees_1
UNION ALL
SELECT name, department
FROM employees_2;
UNION ALL is frequently used in Views to present multiple historical tables as one single table to the end user without the performance hit of a distinct check.
Summary
The UNION operator is an essential tool for merging data from multiple sources into a single result set. Use UNION when you need a clean list of unique values, and use UNION ALL when performance is a priority or when duplicates are expected and necessary. Always ensure that your column counts and data types align across all queries to prevent runtime errors.