SQL UNION Operator with Examples

UNION Operator

Introduction

The SQL UNION operator is a powerful tool that allows combining the results of multiple SELECT statements into a single result set. It merges rows from two or more tables based on compatible columns, eliminating duplicates and providing a unified dataset.

In this article, we will explore the various aspects of the SQL UNION operator, its syntax, usage, examples, advantages and  limitations.

Introduction to SQL UNION Operator 

  • The primary purpose of the SQL UNION operator is to consolidate data from multiple tables into a cohesive dataset.
  • It allows you to retrieve data from different tables as if it were coming from a single table.
  • By using the UNION operator, you can streamline your queries, simplify complex logic, and enhance the flexibility of data retrieval.
SELECT column1, column2, ...
FROM table1
UNION
SELECT column1, column2, ...
FROM table2;

Let’s break down the components of this syntax:

  1. The SELECT statement retrieves specific columns from the tables involved in the union.
  2. The FROM clause specifies the source tables from which data is retrieved.
  3. The UNION keyword combines the results of the two SELECT statements.
  4. Each SELECT statement within the UNION must have the same number of columns, and the corresponding columns must have compatible data types.
SQL UNION Operator example

Here’s the SQL query with the output:

age
---
28
31
33
35
40
49

Here’s a step-by-step explanation of the query:

  1. The query begins with the SELECT statement, which indicates that we want to retrieve specific data from the tables.

  2. The first part of the query is SELECT age FROM Team A. This selects the ‘age’ column from the ‘Team A’ table.

  3. The UNION keyword is used to combine the results of multiple SELECT statements. It allows us to merge the results of two or more queries into a single result set.

  4. The second part of the query is SELECT age FROM Team B. This selects the ‘age’ column from the ‘Team B’ table.

  5. The UNION operator combines the results of the two SELECT statements. It eliminates any duplicate values and merges the distinct ages into a single result set.

  6. The final result is a list of ages from both Team A and Team B, without any duplicates. The ages are displayed in ascending order.

  1. Improved Readability: Assigning meaningful aliases makes the result set easier to understand, especially when dealing with complex queries involving multiple tables and calculations. Aliases provide a clear and concise representation of the data.

  2. Simplified Column Names: Aliases can provide concise and descriptive names, reducing the need for lengthy or cryptic column names in the result set. This simplifies the understanding and interpretation of the data.

  3. Enhanced Presentation: SQL SELECT AS Alias allows you to present data in a more user-friendly and business-oriented manner. By using aliases, you can customize column names to align with the terminology used in the specific domain or application.

  4. Flexibility in Querying: Aliases enable you to perform calculations, transformations, and aggregations on columns and assign appropriate names to the derived values. This flexibility simplifies complex calculations and allows you to create informative and meaningful result sets.

While the SQL UNION operator is a valuable tool, it has certain limitations and considerations:

  1. Column order and data types: The columns in the SELECT statements within the UNION must be in the same order, and their data types should be compatible.
  2. Performance impact on large datasets: When dealing with large datasets, the UNION operator can have performance implications, as it requires merging and sorting of data.
  3. Union vs. Union All: The UNION operator eliminates duplicate rows from the result set, whereas the UNION ALL operator retains all rows, including duplicates. Choose the appropriate operator based on your requirements and data characteristics.

Conclusion

  • In conclusion, the SQL UNION operator is a valuable tool for consolidating data from multiple tables into a single result set.
  • It simplifies complex queries, enhances data analysis capabilities, and improves query performance. However, it is essential to consider the limitations and best practices when utilizing the UNION operator to ensure efficient and accurate results.

Question 1. Can the SQL UNION operator combine more than two tables?

Yes, the SQL UNION operator can combine data from more than two tables. You can simply extend the number of SELECT statements within the UNION to include additional tables.

Question 2. Is it possible to perform calculations within a UNION query?

Yes, you can perform calculations within a UNION query by using expressions in the SELECT statements. This allows you to manipulate and transform the data as required.

Question 3. What is the difference between UNION and UNION ALL?

The UNION operator eliminates duplicate rows from the result set, while the UNION ALL operator retains all rows, including duplicates. Therefore, UNION ALL is faster but may result in duplicate entries.

Question 4.Does the order of columns matter when using UNION?

Yes, the order of columns in the SELECT statements within the UNION must be the same. The corresponding columns from different tables are matched based on their positions.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription