SQL is an important tool that every data scientist and data analyst should know. Its UNION statement allows you to combine the results of two or more SQL SELECT statements. The SELECT command may be on the same table or a different table. We will delve into the basics of UNION and explore different ways to use it. Furthermore, we will use some sample tables to experiment with UNION commands.
Overview:
The SQL UNION combines two or more SELECT statements, i.e., the result sets we get from SELECT statements. We stack them on one other to get the UNION operation done. There are some conditions to execute a UNION statement. Each SELECT statement within the UNION must have the same number of columns in the result sets with similar data types. The SQL UNION operator removes duplicate rows from the result set by default.
Below is the basic syntax for the SQL UNION of two result sets from two SELECT statements.
SELECT column1, column2, ...
FROM table1
UNION
SELECT column1, column2, ...
FROM table2;
CREATE TABLE Employees (
employee_id INT PRIMARY KEY,
name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10, 2)
);
INSERT INTO Employees (employee_id, name, department, salary)
VALUES
(1, 'Alice', 'HR', 60000.00),
(2, 'Bob', 'IT', 75000.00),
(3, 'Charlie', 'Finance', 70000.00),
(4, 'Dana', 'IT', 80000.00);
This will create our first table of Employees and insert sample data into our table.
CREATE TABLE Contractors (
contractor_id INT PRIMARY KEY,
name VARCHAR(50),
department VARCHAR(50),
hourly_rate DECIMAL(10, 2)
);
INSERT INTO Contractors (contractor_id, name, department, hourly_rate)
VALUES
(1, 'David', 'IT', 50.00),
(2, 'Eve', 'Finance', 45.00),
(3, 'Frank', 'HR', 40.00),
(4, 'Grace', 'IT', 55.00);
This will create our second table, Contractors, and insert sample data into our table.
SELECT *
FROM Employees
UNION
SELECT *
FROM Contractors;
The column name comes from the Employee table. Note that SQL UNION takes the column names from the first SELECT statement.
SELECT name, department
FROM Employees
UNION
SELECT name, department
FROM Contractors
ORDER BY salary;
In our final result set, we have sorted the records by salary. Hence, you can use ORDER BY to sort records in the final result set. Note that you can only order based on the selected columns, not those not selected.
SELECT name, department
FROM Employees
WHERE department = 'IT'
UNION
SELECT name, department
FROM Contractors
WHERE department = 'IT';
In the above image, only the selected columns and records that satisfy the WHERE clause are present. The WHERE clause will help you filter records as your condition in SQL UNION.
JOIN and UNION are both used to combine data from multiple tables. They both have different purposes.
JOIN | UNION | |
Purpose | JOIN combines columns from two or more tables based on a related column between them. | Combines the results of two or more SELECT statements into a single result set, stacking them vertically. |
Structure | Merges tables horizontally by adding columns from the second table to the columns of the first table. | Merges tables vertically by adding rows from the second SELECT statement to the rows of the first SELECT statement. |
SQL UNION gives you diverse solutions for combining results from SELECT statements from the same or different tables. With a good understanding of UNION statements and their behavior, we should be able to effectively manage and manipulate data. UNION operators simplify the process and make it easy to combine results.
Also Read: Most Important SQL Queries for Beginners
A. All SELECT statements used in the UNION must have the same number of columns, and their corresponding columns must have compatible data types.
A. Yes, you can use ORDER BY with UNION, but it should be placed after the last SELECT statement to sort the entire result set.
A. JOIN can be more resource-intensive, especially with large tables or multiple joins, due to the need for matching rows across tables. UNION can also be costly, particularly when removing duplicates. Use UNION ALL if duplicates are acceptable to improve performance.