SQL UNION and UNION ALL
A join puts columns side by side; UNION stacks rows on top of each other. Use it to merge similar lists from different tables or conditions: all people in a mailing list (customers and employees), or a report row plus a total row. UNION removes duplicates, UNION ALL keeps them.
- Stack the results of two queries with
UNION - Know the column rules
- Choose between
UNIONandUNION ALL - Sort and label the combined result
UNION syntax
SELECT column1, column2 FROM table1UNION [ALL]SELECT column1, column2 FROM table2[ORDER BY ...];Rules:
- every
SELECTmust return the same number of columns; - columns are matched by position, and should have compatible types;
- the column names come from the first
SELECT; - one
ORDER BYat the very end sorts the combined result.
Combine two lists
Every city where we have an employee or a customer:
SELECT city FROM employeesUNIONSELECT city FROM customersORDER BY city;| city |
|---|
| Bengaluru |
| Chennai |
| Delhi |
| Goa |
| Hyderabad |
| Kolkata |
| Mumbai |
| Pune |
- The first query returns 12 employee cities, the second 7 customer cities.
UNIONmerges them and removes duplicates, such as Mumbai, which appears in both.ORDER BY citysorts the final list.
UNION ALL keeps duplicates
SELECT city FROM employeesUNION ALLSELECT city FROM customersORDER BY city;| city |
|---|
| Bengaluru |
| Bengaluru |
| Bengaluru |
| Bengaluru |
| Bengaluru |
| Chennai |
| Delhi |
| Delhi |
| Goa |
| Hyderabad |
| Kolkata |
| Kolkata |
| Mumbai |
| Mumbai |
| Mumbai |
| Mumbai |
| Pune |
| Pune |
| Pune |
UNION ALLsimply appends the second result to the first: 12 + 7 = 19 rows.- No duplicate check is done, so it is faster. Use it whenever duplicates are impossible or wanted.
Label where each row came from
A contact list from two tables, with a type column:
SELECT CONCAT(first_name, ' ', last_name) AS name, city, 'Employee' AS typeFROM employeesWHERE city = 'Mumbai'UNION ALLSELECT customer_name, city, 'Customer'FROM customersWHERE city = 'Mumbai'ORDER BY type, name;| name | city | type |
|---|---|---|
| Ananya Sharma | Mumbai | Customer |
| Meera Kapoor | Mumbai | Employee |
| Neha Gupta | Mumbai | Employee |
| Vikram Singh | Mumbai | Employee |
- Both queries return three columns: a name, a city and a constant label.
- The second query’s columns have no aliases; the names
name,cityandtypecome from the first query. ORDER BY type, nameuses those names.
A total row
SELECT category, SUM(stock) AS unitsFROM productsGROUP BY categoryUNION ALLSELECT 'TOTAL', SUM(stock)FROM products;| category | units |
|---|---|
| Electronics | 165 |
| Furniture | 65 |
| Stationery | 550 |
| TOTAL | 780 |
Column count must match
SELECT first_name, city FROM employeesUNIONSELECT customer_name FROM customers;ERROR 1222 (21000): The used SELECT statements have a different number of columns
UNION vs JOIN
| UNION | JOIN | |
|---|---|---|
| Combines | rows (vertically) | columns (horizontally) |
| Needs | same number of columns | a matching condition |
| Typical use | merge similar lists | relate different tables |
Exercise: All people named with A
List every employee first name and every customer name that starts with “A”, in one alphabetical list without duplicates.
| name |
|---|
| Amit |
| Ananya Sharma |
| Arjun |
| Asha |
Hint 1
Two SELECTs with LIKE 'A%', combined with UNION.
SELECT first_name AS name FROM employees WHERE first_name LIKE 'A%'UNIONSELECT customer_name FROM customers WHERE customer_name LIKE 'A%'ORDER BY name;| name |
|---|
| Amit |
| Ananya Sharma |
| Arjun |
| Asha |
Quick quiz
1. What is the difference between UNION and UNION ALL?
UNION adds a duplicate-elimination step.
2. Where do the column names of a UNION result come from?
Later queries only supply values by position.
3. Which is faster when duplicates do not matter?
UNION ALL skips the duplicate check.
UNION FAQ
What does UNION do in SQL?
It combines the result rows of two or more SELECT statements into one result and removes duplicates.
Can I use ORDER BY with UNION?
Yes, once, at the end, to sort the combined result. To sort or limit one part only, wrap it in parentheses.
Do the column names have to match in a UNION?
No. Only the number of columns and compatible types matter; the result uses the first query’s names.