đ The UNION ALL operator in SQL is used to combine the results of two or more SELECT statementsinto a single result set without removing duplicate rows. Unlike UNION, every row returned by each query is included in the final result, making UNION ALL faster and more efficient when duplicate elimination is not required.
đ What is UNION ALL?
The UNION ALL operator merges the results of multiple SELECT statements while preserving every row, including duplicates. It is commonly used when combining data from similar tables or multiple queries where duplicate values are meaningful.
Information
đ¯ Why Use UNION ALL?
Use UNION ALL when every row is important, including duplicate records.
- đ Combine results without removing duplicates.
- đ Improve query performance.
- đ Merge data from multiple sources.
- đ Preserve repeated values for accurate reporting.
- đ Process large datasets efficiently.
đ Basic Syntax
UNION ALL Syntax
SELECT column1, column2
FROM table1
UNION ALL
SELECT column1, column2
FROM table2;đ Rules for Using UNION ALL
| Rule | Description |
|---|---|
| Same Number of Columns | Every SELECT statement must return the same number of columns. |
| Compatible Data Types | Corresponding columns should have compatible data types. |
| Column Order | The columns must appear in the same order in every query. |
| Duplicates | Duplicate rows are preserved. |
đ Sample Tables
Consider the following UndergraduateStudents table:
| StudentID | Name |
|---|---|
| 101 | Alice |
| 102 | Bob |
| 103 | Charlie |
Consider the following PostgraduateStudents table:
| StudentID | Name |
|---|---|
| 201 | David |
| 202 | Emma |
| 103 | Charlie |
đĄ Basic UNION ALL Example
Combine the names of undergraduate and postgraduate students while preserving duplicates.
Basic UNION ALL
SELECT Name
FROM UndergraduateStudents
UNION ALL
SELECT Name
FROM PostgraduateStudents;Result:
| Name |
|---|
| Alice |
| Bob |
| Charlie |
| David |
| Emma |
| Charlie |
Important
đ UNION ALL with WHERE
Each SELECT statement can include its own WHERE clause.
UNION ALL with WHERE
SELECT Name
FROM UndergraduateStudents
WHERE StudentID > 101
UNION ALL
SELECT Name
FROM PostgraduateStudents
WHERE StudentID > 201;đ UNION ALL with ORDER BY
Use a single ORDER BY clause after the final SELECT statement to sort the combined result.
UNION ALL with ORDER BY
SELECT Name
FROM UndergraduateStudents
UNION ALL
SELECT Name
FROM PostgraduateStudents
ORDER BY Name ASC;Tip
đˇī¸ Using Aliases with UNION ALL
Column aliases improve the readability of the final output.
UNION ALL with Alias
SELECT Name AS StudentName
FROM UndergraduateStudents
UNION ALL
SELECT Name
FROM PostgraduateStudents;đ UNION ALL Across Different Tables
UNION ALL can combine results from different tables as long as the selected columns are compatible.
Combine Employees and Teachers
SELECT Name,
City
FROM Employees
UNION ALL
SELECT Name,
City
FROM Teachers;âī¸ UNION vs UNION ALL
| Feature | UNION | UNION ALL |
|---|---|---|
| Duplicate Rows | Removed | Preserved |
| Performance | Slower | Faster |
| Duplicate Check | Required | Not Required |
| Typical Use | Unique results | Complete results |
đŧ Real-World Example
A university wants to generate a report containing all student registrations from undergraduate and postgraduate programs, including duplicate entries because some students are enrolled in both programs.
Combined Registration Report
SELECT StudentID,
Name
FROM UndergraduateStudents
UNION ALL
SELECT StudentID,
Name
FROM PostgraduateStudents
ORDER BY Name;This query combines every registration record without removing duplicates, ensuring complete reporting.
â ī¸ Common Mistakes
- â Returning different numbers of columns in each SELECT.
- â Using incompatible data types.
- â Assuming duplicate rows will be removed automatically.
- â Placing ORDER BY before the final SELECT.
Warning
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ UNION ALL combines multiple query results.
- đ Duplicate rows are preserved.
- đ It is generally faster than UNION.
- đ All queries must return the same number of columns.
- đ Corresponding columns must have compatible data types.
- đ Use a single ORDER BY clause at the end of the combined query.