UNION, UNION All, INTERSECT, EXCEPT used in SQL Server
UNION : The UNION operator is used to combine the result sets of two or more SELECT statements into a single result set. It removes duplicate rows from the…
Author
SQLDataDev Editorial Team
Mar 19, 2026 2 min read
UNION: The UNION operator is used to combine the result sets of two or more SELECT statements into a single result set. It removes duplicate rows from the result set.
UNION ALL: The UNION ALL operator is similar to UNION, but it includes all rows from the result sets, including duplicates.
INTERSECT: The INTERSECT operator is used to return the common rows that appear in two SELECT statements.
EXCEPT/ MINUS: The EXCEPT (or MINUS in some databases) operator is used to return all rows from the first SELECT statement that are not present in the result of the second SELECT statement.
SQL
-- Create @Employee tableDECLARE@EmployeeTABLE (
EmployeeID INT,
Name VARCHAR(50),
Department VARCHAR(50)
);
-- Insert dummy data into @Employee tableINSERT INTO@Employee (EmployeeID, Name, Department)
VALUES
(1, 'John Doe', 'HR'),
(2, 'Jane Smith', 'IT'),
(3, 'Alice Johnson', 'Finance');
-- Create @EmployeeBackup tableDECLARE@EmployeeBackupTABLE (
EmployeeID INT,
Name VARCHAR(50),
Department VARCHAR(50)
);
-- Insert dummy data into @EmployeeBackup tableINSERT INTO@EmployeeBackup (EmployeeID, Name, Department)
VALUES
(1, 'John Doe', 'HR'),
(4, 'Bob Brown', 'Marketing');
-- Insert common data into both tablesINSERT INTO@Employee (EmployeeID, Name, Department)
VALUES
(5, 'Tom White', 'Sales');
INSERT INTO@EmployeeBackup (EmployeeID, Name, Department)
VALUES
(5, 'Tom White', 'Sales');
SELECT*FROM@EmployeeSELECT*FROM@EmployeeBackupSELECT*FROM@EmployeeUNIONSELECT*FROM@EmployeeBackupSELECT*FROM@EmployeeUNIONALLSELECT*FROM@EmployeeBackupSELECT*FROM@EmployeeINTERSECTSELECT*FROM@EmployeeBackupSELECT*FROM@EmployeeEXCEPTSELECT*FROM@EmployeeBackup