UNION clause in SQL is used to combine the result sets of two or more SELECT statements into a single result set. It removes duplicate rows between the SELECT statements, ensuring that each row in the final result set is unique. This is particularly useful when you want to combine data from multiple tables or queries while eliminating duplicates.
Key Points About UNION
- Combining Results:
UNIONis used to combine rows from two or more tables or queries. - Duplicate Removal: By default,
UNIONremoves duplicate rows from the combined result set. - Column Matching: The number of columns and their data types must match in all the
SELECTstatements being combined. - Order of Columns: The columns in the
SELECTstatements must be in the same order.
Syntax of UNION
column1, column2, ...: The columns you want to retrieve.table1, table2: The tables from which you want to retrieve data.
Example of UNION
Suppose you have two tables,Employees and Managers, and you want to retrieve a list of all unique names from both tables.
Table: Employees
| Name |
|---|
| Anand |
| Bala |
| Kavitha |
| Raj |
| Kumar |
| Name |
|---|
| Ram |
| Karthik |
| David |
| Anand |
| Kannan |
| Name |
|---|
| Anand |
| Bala |
| Kavitha |
| Raj |
| Kumar |
| Ram |
| Karthik |
| David |
| Kannan |
UNION removes duplicates.
UNION vs UNION ALL
- UNION: Removes duplicate rows from the combined result set.
- UNION ALL: Includes all rows, including duplicates, in the combined result set.
Example of UNION ALL
Using the sameEmployees and Managers tables:
Result
| Name |
|---|
| Anand |
| Bala |
| Kavitha |
| Raj |
| Kumar |
| Ram |
| Karthik |
| David |
| Anand |
| Kannan |
UNION ALL does not remove duplicates.
When to Use UNION
- Use
UNIONwhen you want to combine results from multiple queries and ensure that the final result set contains only unique rows. - Use
UNION ALLwhen you want to combine results and are okay with including duplicate rows.
Important Considerations
- Performance:
UNIONcan be slower thanUNION ALLbecause it requires additional processing to remove duplicates. - Column Names: The column names in the final result set are taken from the first
SELECTstatement. - Ordering Results: If you need to order the final result set, you can use an
ORDER BYclause at the end of the lastSELECTstatement.
Example with ORDER BY
Result
| Name |
|---|
| Anand |
| Bala |
| David |
| Kannan |
| Karthik |
| Kavitha |
| Kumar |
| Raj |
| Ram |
Name column.
Practical Use Case
Imagine you have two tables,Students2022 and Students2023, and you want to create a list of all unique students who enrolled in either year.
Table: Students2022
| Name |
|---|
| Siva |
| Ramesh |
| Suresh |
| Sathish |
| Name |
|---|
| Sujatha |
| Siva |
| Ramesh |
| Kannan |
| Name |
|---|
| Siva |
| Ramesh |
| Suresh |
| Sathish |
| Sujatha |
| Kannan |
Key Takeaways
- The
UNIONclause combines results from multipleSELECTstatements into a single result set. - It removes duplicate rows by default. Use
UNION ALLto include duplicates. - The number of columns and their data types must match in all
SELECTstatements. - Use
ORDER BYto sort the final result set. UNIONis useful for combining data from multiple sources while ensuring uniqueness.