union in sql

Galaxy Glossary

How can I combine the results of two or more SELECT statements?

The UNION operator in SQL combines the result sets of two or more SELECT statements into a single result set. It's crucial for merging data from different sources or filtering results from multiple queries. Crucially, it removes duplicate rows.
Sign up for the latest in SQL knowledge from the Galaxy Team!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Description

The UNION operator is a powerful tool in SQL for combining the output of multiple SELECT statements. Imagine you have two tables: one containing customer orders and another containing customer returns. You might want to see all orders and returns in a single view. UNION allows you to do precisely that. It takes the result sets from each SELECT statement and stacks them vertically, creating a single, unified result. A key aspect of UNION is that it automatically eliminates duplicate rows, ensuring data integrity in the combined result. This is particularly useful when dealing with data from multiple sources that might have overlapping entries. For example, if both SELECT statements return a row with the same customer ID, UNION will only include that row once. This is different from the UNION ALL operator, which includes all rows, even duplicates. Understanding the difference between UNION and UNION ALL is essential for accurate data manipulation.

Why union in sql is important

UNION is essential for data analysts and developers who need to synthesize information from various sources. It simplifies complex queries and ensures data accuracy by eliminating redundant rows. This makes it a fundamental tool for creating comprehensive reports and analyses.

Example Usage

```sql CREATE TABLE Orders ( OrderID INT PRIMARY KEY, CustomerID INT, OrderDate DATE ); CREATE TABLE Returns ( ReturnID INT PRIMARY KEY, CustomerID INT, ReturnDate DATE ); INSERT INTO Orders (OrderID, CustomerID, OrderDate) VALUES (1, 101, '2023-10-26'), (2, 102, '2023-10-27'), (3, 101, '2023-10-28'); INSERT INTO Returns (ReturnID, CustomerID, ReturnDate) VALUES (1, 101, '2023-10-27'), (2, 103, '2023-10-29'); SELECT CustomerID, OrderDate FROM Orders UNION SELECT CustomerID, ReturnDate FROM Returns; ```

Common Mistakes

Want to learn about other SQL terms?