A Complete Guide - SQL Server UNION and INTERSECT
SQL Server UNION and INTERSECT: Detailed Explanation and Important Information
In SQL Server, the UNION and INTERSECT operators are set-based operations used to combine results from multiple SELECT queries into a single result set. These operators help in merging datasets while applying specific rules to eliminate duplicates or retain only common rows.
UNION Operator
Purpose: The
UNIONoperator combines the result sets of two or moreSELECTstatements into a single result set.Syntax:
SELECT column1, column2, ... FROM table1 UNION [ALL] SELECT column1, column2, ... FROM table2;UNION: Removes duplicate rows from the final result set.UNION ALL: Includes all rows from the result sets including duplicates.
Important Points:
- Matching Columns: Both
SELECTstatements must have the same number of columns, and corresponding columns must have compatible data types. - Column Aliases: Column aliases should be provided in the first
SELECTstatement. They will apply to the entire result set. - Ordering: The
ORDER BYclause can only appear after the lastSELECTin aUNIONstatement when usingUNION ALLorUNION.
- Matching Columns: Both
Detailed Example Using UNION
Consider two tables, Employees_A and Employees_B, with the following data:
-- Employees_A table
CREATE TABLE Employees_A
(
EmployeeID INT PRIMARY KEY,
EmployeeName NVARCHAR(150),
Department NVARCHAR(50)
);
INSERT INTO Employees_A VALUES (1, 'John Doe', 'Sales'), (2, 'Jane Smith', 'Finance');
-- Employees_B table
CREATE TABLE Employees_B
(
EmployeeID INT PRIMARY KEY,
EmployeeName NVARCHAR(150),
Department NVARCHAR(50)
);
INSERT INTO Employees_B VALUES (2, 'Jane Smith', 'HR'), (3, 'Alice Brown', 'Marketing');
Performing a UNION ALL:
SELECT EmployeeID, EmployeeName, Department
FROM Employees_A
UNION ALL
SELECT EmployeeID, EmployeeName, Department
FROM Employees_B;
Result:
EmployeeID | EmployeeName | Department
--------------------------------------
1 | John Doe | Sales
2 | Jane Smith | Finance
2 | Jane Smith | HR
3 | Alice Brown | Marketing
Without ALL, performing UNION:
SELECT EmployeeID, EmployeeName, Department
FROM Employees_A
UNION
SELECT EmployeeID, EmployeeName, Department
FROM Employees_B;
Result:
EmployeeID | EmployeeName | Department
--------------------------------------
1 | John Doe | Sales
2 | Jane Smith | Finance
3 | Alice Brown | Marketing
2 | Jane Smith | HR
Here, the result includes all unique EmployeeID entries. However, because the combination of EmployeeID 2 and "Jane Smith" is not completely identical (i.e., different departments), both rows are retained. Note that UNION automatically orders the combined result set in ascending order by default.
INTERSECT Operator
Purpose: The
INTERSECToperator returns distinct rows that are common between the result sets of twoSELECTstatements.Syntax:
SELECT column1, column2, ... FROM table1 INTERSECT SELECT column1, column2, ... FROM table2;Important Points:
- Similar to
UNION,INTERSECTrequires the same number and type of columns across allSELECTstatements involved. INTERSECTautomatically removes duplicate rows and ensures that only unique matching rows are returned.- The
ORDER BYclause can only appear once at the end of the entireINTERSECToperation.
- Similar to
Example Using INTERSECT
Continuing from the previous example:
SELECT EmployeeID, EmployeeName
FROM Employees_A
INTERSECT
SELECT EmployeeID, EmployeeName
FROM Employees_B;
Result:
EmployeeID | EmployeeName
-------------------------
2 | Jane Smith
This shows only the employees who exist in both Employees_A and Employees_B. Note how the Department column is not included; it's necessary to include only those columns which have identical definitions in all SELECT statements.
Key Considerations and Best Practices
Data Types Compatibility: Ensure that columns selected in each
SELECTstatement are of compatible data types.DISTINCT Behavior: Remember that
UNIONby default performs aDISTINCTon the combined result, whereasINTERSECTinherently provides distinct matches.Performance: Be mindful of performance, especially when dealing with large datasets, as these set-based operations may involve sorting and elimination of duplicates.
Null Handling:
UNIONandINTERSECTtreat twoNULLs as equal; thus, they may return rows that are considered duplicates based onNULLvalues.Sorting: Sorting applies across the entire result set only if an
ORDER BYclause is specified at the end of the query.Use Cases:
UNION: Useful when you need to combine data from multiple tables but ensure no duplicated entries.INTERSECT: Ideal for scenarios where you want to find records common to two or more tables, such as shared customers or overlapping projects.
Index and Statistics: Proper use of indexes and up-to-date statistics can significantly improve performance of
UNIONandINTERSECToperations.
Online Code run
Step-by-Step Guide: How to Implement SQL Server UNION and INTERSECT
SQL Server UNION and INTERSECT
Basic Understanding
- UNION: Combines the result sets of two or more
SELECTstatements into a single result set. Duplicate rows are automatically removed unlessUNION ALLis used. - INTERSECT: Returns only the rows that are common between the result sets of two or more
SELECTstatements.
Assumptions and Environment
Let's assume we have a simple database with two tables: EmployeesEast and EmployeesWest. Each table contains employee details such as EmployeeID and EmployeeName.
Step 1: Create Sample Tables and Insert Data
-- Step 1: Create the EmployeesEast Table
CREATE TABLE EmployeesEast (
EmployeeID INT PRIMARY KEY,
EmployeeName VARCHAR(50)
);
-- Step 2: Create the EmployeesWest Table
CREATE TABLE EmployeesWest (
EmployeeID INT PRIMARY KEY,
EmployeeName VARCHAR(50)
);
-- Step 3: Insert Sample Data into EmployeesEast
INSERT INTO EmployeesEast (EmployeeID, EmployeeName) VALUES
(1, 'John Doe'),
(2, 'Jane Smith'),
(3, 'Emily Davis');
-- Step 4: Insert Sample Data into EmployeesWest
INSERT INTO EmployeesWest (EmployeeID, EmployeeName) VALUES
(2, 'Jane Smith'),
(4, 'Michael Brown'),
(5, 'Sarah Johnson');
Step 2: Using UNION
Objective: Retrieve a list of all employees from both EmployeesEast and EmployeesWest without any duplicates.
Query:
SELECT EmployeeID, EmployeeName
FROM EmployeesEast
UNION
SELECT EmployeeID, EmployeeName
FROM EmployeesWest;
Output:
EmployeeID EmployeeName
----------- -------------
1 John Doe
2 Jane Smith
3 Emily Davis
4 Michael Brown
5 Sarah Johnson
Note: Jane Smith is only listed once because duplicates are removed by UNION.
Step 3: Using UNION ALL
Objective: Retrieve a list of all employees from both EmployeesEast and EmployeesWest, including duplicates.
Query:
SELECT EmployeeID, EmployeeName
FROM EmployeesEast
UNION ALL
SELECT EmployeeID, EmployeeName
FROM EmployeesWest;
Output:
EmployeeID EmployeeName
----------- -------------
1 John Doe
2 Jane Smith
3 Emily Davis
2 Jane Smith
4 Michael Brown
5 Sarah Johnson
Note: Jane Smith appears twice because UNION ALL does not remove duplicates.
Step 4: Using INTERSECT
Objective: Retrieve a list of employees who are present in both EmployeesEast and EmployeesWest.
Query:
SELECT EmployeeID, EmployeeName
FROM EmployeesEast
INTERSECT
SELECT EmployeeID, EmployeeName
FROM EmployeesWest;
Output:
Login to post a comment.