<aside> 🔀 SET operations combine results from multiple SELECT statements into a single result.

</aside>


Rules of SET Operations


1. UNION — Combine without duplicates

Combines results and removes duplicates (slower than UNION ALL).

SELECT FirstName, LastName FROM Sales.Customers
UNION
SELECT FirstName, LastName FROM Sales.Employees;

2. UNION ALL — Combine including duplicates

Combines results and keeps ALL rows including duplicates (faster than UNION).

SELECT FirstName, LastName FROM Sales.Customers
UNION ALL
SELECT FirstName, LastName FROM Sales.Employees;

<aside> 💡 Prefer UNION ALL over UNION for better performance when duplicates are acceptable.

</aside>

3. EXCEPT — Rows in first but NOT in second

Returns rows from the first query that do not appear in the second query.

-- Employees who are NOT customers
SELECT FirstName, LastName FROM Sales.Employees
EXCEPT
SELECT FirstName, LastName FROM Sales.Customers;

4. INTERSECT — Rows in BOTH queries

Returns only rows that appear in both queries.