- Welcome to the SQL Tutorial Series
- Introduction to SQL and Databases: A Beginner’s Guide
- Demystifying DBMS vs. RDBMS: Unraveling the Database Jargon
- How to Install MySQL and MySQL Workbench on macOS
- Demystifying SQL Syntax: Key Aspects and Details
- Understanding SQL Tables and Keys in Relational Databases
- Demystifying SQL: A Beginner’s Guide with Practical Examples
- A Comprehensive Guide to Creating Databases and Tables in SQL
- A Step-by-Step Guide to Inserting Data into SQL Tables
- A Comprehensive Guide to Updating Data in SQL Tables with Examples
- Exploring the SELECT and FROM Keywords in SQL with Examples
- Mastering SQL’s WHERE Clause
- Demystifying the SQL ORDER BY Clause
- Understanding Database Normalization in SQL
- Understanding Primary and Foreign Keys in SQL
- A Comprehensive Guide to Aggregate Functions
- A Comprehensive Guide to GROUP BY and HAVING Clause in SQL
- A Comprehensive Guide to SQL Unions
- Comprehensive Guide to SQL Joins
In SQL, the UNION
operator is a powerful tool that allows you to combine rows from multiple result sets into a single result set. This operation is particularly useful when you need to retrieve data from different tables or queries that have the same structure. In this comprehensive guide, we’ll explore SQL UNION
with practical examples and table results to help you understand and utilize it effectively.
Understanding the UNION Operator
The UNION
operator is used to combine the result sets of two or more SELECT
statements. It removes duplicate rows by default, ensuring that the final result set contains only distinct rows. To use UNION
, the following conditions must be met:
- The number of columns in each
SELECT
statement must be the same. - The data types of corresponding columns in each
SELECT
statement must be compatible.
Let’s dive into various examples to illustrate how to use the UNION
operator.
Example Tables
Throughout this guide, we will use two sample tables: Customers
and Suppliers
. Both tables have the same structure, containing information about entities and their respective contact details. Here are the tables:
Customers Table:
CustomerID | CustomerName | ContactEmail |
---|---|---|
1 | Customer A | [email protected] |
2 | Customer B | [email protected] |
3 | Customer C | [email protected] |
Suppliers Table:
SupplierID | SupplierName | ContactEmail |
---|---|---|
101 | Supplier X | [email protected] |
102 | Supplier Y | [email protected] |
103 | Supplier Z | [email protected] |
Example 1: Basic UNION
Let’s start with a basic example. Suppose you want to retrieve a list of all entities (customers and suppliers) along with their contact details. You can achieve this using a UNION
between the Customers
and Suppliers
tables:
SQL Query:
SELECT CustomerName AS EntityName, ContactEmail
FROM Customers
UNION
SELECT SupplierName AS EntityName, ContactEmail
FROM Suppliers;
Table Results:
EntityName | ContactEmail |
---|---|
Customer A | [email protected] |
Customer B | [email protected] |
Customer C | [email protected] |
Supplier X | [email protected] |
Supplier Y | [email protected] |
Supplier Z | [email protected] |
In this query, we retrieve the customer and supplier names along with their contact emails, combining the results using UNION
.
Example 2: UNION with ORDER BY
You can also use UNION
with an ORDER BY
clause to sort the combined result set. For instance, let’s retrieve a list of all entities, ordered by their names:
SQL Query:
SELECT CustomerName AS EntityName, ContactEmail
FROM Customers
UNION
SELECT SupplierName AS EntityName, ContactEmail
FROM Suppliers
ORDER BY EntityName;
Table Results:
EntityName | ContactEmail |
---|---|
Customer A | [email protected] |
Customer B | [email protected] |
Customer C | [email protected] |
Supplier X | [email protected] |
Supplier Y | [email protected] |
Supplier Z | [email protected] |
In this query, we use ORDER BY
to sort the combined result set alphabetically by the entity names.
Example 3: UNION ALL
By default, UNION
removes duplicate rows from the result set. If you want to include duplicate rows, you can use UNION ALL
. Let’s see an example where we retrieve all entities, including duplicates:
SQL Query:
SELECT CustomerName AS EntityName, ContactEmail
FROM Customers
UNION ALL
SELECT SupplierName AS EntityName, ContactEmail
FROM Suppliers;
Table Results:
EntityName | ContactEmail |
---|---|
Customer A | [email protected] |
Customer B | [email protected] |
Customer C | [email protected] |
Supplier X | [email protected] |
Supplier Y | [email protected] |
Supplier Z | [email protected] |
Customer A | [email protected] |
Supplier X | [email protected] |
Customer B | [email protected] |
In this query, we use UNION ALL
, which includes duplicate rows in the result set.
Conclusion
The UNION
operator in SQL is a valuable tool for combining results from multiple SELECT
statements into a single result set. Whether you need to merge data from different tables, eliminate duplicate rows, or sort the combined data, UNION
provides flexibility and efficiency in your database queries. By mastering UNION
and its variations, you can retrieve and manipulate data from various sources effectively in your SQL applications.