Free access is closing! 700+ free courses are waiting.
Ends in:
Start Now
The SQL MINUS operator subtracts one result set from another, returning only the unique rows from the first set that are not present in the second.
Let’s imagine we have two employee tables, one from 2023 and one from 2024. We want to find out which employees were present in Employees2023 but are not in Employees2024. We could use an SQL MINUS statement to get the answer.
SELECT EmployeeID, Name, Department
FROM Employees2023
MINUS
SELECT EmployeeID, Name, Department
FROM Employees2024;Employees2023:
| EmployeeID | Name | Department |
|------------|---------|------------|
| 1 | Alice | HR |
| 2 | Bob | IT |
| 3 | Charlie | Sales |
| 4 | David | IT |
Employees2024:
| EmployeeID | Name | Department |
|------------|---------|------------|
| 2 | Bob | IT |
| 3 | Charlie | Sales |
| 4 | David | IT |
| 5 | Eve | Marketing |
Expected Output:
| EmployeeID | Name | Department |
|------------|-------|------------|
| 1 | Alice | HR |The SQL MINUS operator compares two SELECT statements and returns rows from the first SELECT statement that are absent in the second. Specifically, it performs a set difference operation, which is crucial for various data analysis tasks. This is particularly useful for data cleaning, validation, and consolidation efforts where we must pinpoint discrepancies between datasets.
The basic syntax of the MINUS operator is as follows:
The components of the query are:
SELECT statement specifies the desired columns and the table from which to retrieve them.MINUS operator subtracts the second result set from the first.WHERE clause can be included to filter the data.SELECT column1, column2, ...
FROM table1
WHERE condition
MINUS
SELECT column1, column2, ...
FROM table2
WHERE condition;To use the MINUS operator correctly, keep in mind these key points:
SELECT statements must have the same number of columns.MINUS removes duplicate rows from the final result set, returning only distinct rows.Let’s now take a look at some practical examples to solidify our understanding.
We use the following query to find the unique departments in 2019 that are not present in 2020:
We use the following query to get the unique employees in 2019, sorted by their names:
SELECT EmployeeName
FROM Employees2019
MINUS
SELECT EmployeeName
FROM Employees2020
ORDER BY EmployeeName;The SQL MINUS operator is a versatile tool that can be applied in various scenarios to improve data analysis and management. Below, we discuss several everyday use cases where the MINUS operator proves particularly useful.
Ensure data consistency and accuracy in ETL (Extract, Transform, Load) processes. The MINUS operator can validate data by comparing source and target datasets.
Let's take a look an an example. To validate that all records from the staging table have been loaded into the final table, we can use the following query:
When consolidating data from multiple sources, it is essential to identify any missing records to ensure completeness:
While the SQL MINUS operator is a powerful tool for comparing datasets, it has certain limitations and may not always be the best choice for every scenario. Understanding these limitations and knowing the available alternatives can help us make better decisions when working with SQL queries.
MINUS operator is not universally supported across all SQL database management systems. While it is available in Oracle, it is not supported in SQL Server, which uses the EXCEPT operator.SELECT statements used with the MINUS operator must have the same number of columns, and corresponding columns must have compatible data types. This can be restrictive when working with complex queries involving different datasets.MINUS operator is case-sensitive. For example, 'Apple' and 'apple' are considered different values. If case sensitivity is not considered, this can lead to unexpected results.MINUS operator is not sorted. If a specific order is required, an additional ORDER BY clause must be used.MINUS operator can be slow when dealing with large datasets, as it needs to perform a complete comparison between the two result sets. This can lead to performance bottlenecks in large-scale applications.An alternative to the MINUS operator is the EXCEPT operator, which is functionally similar and widely supported across different SQL databases.
Understanding the SQL MINUS operator is crucial for effective SQL querying and data manipulation. This operator helps us identify unique rows between two datasets, making it valuable for data validation, cleanup, and consolidation tasks. Practice with different datasets to master the use of the MINUS operator and enhance your SQL skills.
If you really want to improve your SQL skills, take a DataCamp course like Introduction to SQL. This course gives a good foundation for more advanced topics. If you feel like you have a good grasp on the basics, try our Intermediate SQL course, which dives deeper into SQL complexities, including set operations like MINUS.

Tech writer specializing in AI, ML, and data science, making complex ideas clear and accessible.
The SQL MINUS operator is used to subtract one result set from another, returning only the unique rows from the first set that are not present in the second.
Both SELECT statements in the MINUS operation must have the same number of columns, and the data types of corresponding columns must be the same.
No, the MINUS operator is not supported by all SQL databases. For example, it is not supported in MySQL but is available in Oracle.
Common use cases include data validation in ETL processes, identifying missing data between two datasets, and cleaning up and consolidating data from multiple sources.
The MINUS operator is primarily used in Oracle and some other databases, while the EXCEPT operator serves a similar function and is used in SQL Server, PostgreSQL, and other databases. Both operators return unique rows from the first SELECT statement that are not present in the second.
The MINUS operator in SQL is used to return all rows in the first query that are not present in the second query. It is a set operation that compares the results of two queries and returns the difference. An anti join, on the other hand, is a type of join that returns rows from the first table where no matching rows exist in the second table. This can be implemented using a LEFT JOIN combined with a WHERE clause to filter out rows where the join condition is not met.
튜토리얼
튜토리얼

튜토리얼
Learn SQL with DataCamp
In SQL, the
MINUSoperator is a powerful tool for subtracting one result set from another. This operator is essential for scenarios where we must identify the unique rows in one dataset but not another.In this article, we will explore the purpose, syntax, and practical applications of the SQL
MINUSoperator, ensuring a deep understanding of effective SQL querying and data manipulation.