How do you use the EXCEPT operator to retrieve distinct rows from the left query that are not in the right query?
Posted by DavidLee
Last Updated: July 27, 2024
The EXCEPT operator in SQL is used to return distinct rows from the first (left) query that are not found in the second (right) query. This is particularly useful for identifying differences between two result sets. Here's a general syntax for using the EXCEPT operator:
SELECT column1, column2, ...
FROM table1
WHERE condition

EXCEPT

SELECT column1, column2, ...
FROM table2
WHERE condition;
Key Points:
1. Distinct Rows: The result set of the EXCEPT operator contains unique rows (duplicates are removed). 2. Column Matching: The two queries must have the same number of columns and the corresponding columns must have compatible data types. 3. Order of Execution: The left query is executed first, followed by the right query. Rows from the right query are subtracted from the result of the left query.
Example:
Suppose you have two tables, Employees_2022 and Employees_2023. If you want to find employees who were in Employees_2022 but not in Employees_2023, you could write:
SELECT EmployeeID, EmployeeName
FROM Employees_2022

EXCEPT

SELECT EmployeeID, EmployeeName
FROM Employees_2023;
In this example: - The first SELECT statement retrieves the EmployeeID and EmployeeName from the Employees_2022 table. - The EXCEPT operator then takes the results from the first query and removes any rows that also exist in the results of the second SELECT statement (from Employees_2023). - The final output will be a list of employees who were in Employees_2022 but are not present in Employees_2023.
Important Note:
- Not all SQL databases support the EXCEPT operator. For RDBMS systems that don’t support it, similar results can be achieved using a LEFT JOIN or NOT IN clause. Here's how you might achieve the same result using a LEFT JOIN:
SELECT e2022.EmployeeID, e2022.EmployeeName
FROM Employees_2022 e2022
LEFT JOIN Employees_2023 e2023 ON e2022.EmployeeID = e2023.EmployeeID
WHERE e2023.EmployeeID IS NULL;
This will give you the same result by selecting rows from Employees_2022 that do not have a matching EmployeeID in Employees_2023.