SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
The MINUS
operator is used in SQL to return all rows from the first query that are not returned by the second query. Each SQL statement within the MINUS
query must have the same number of fields, with similar data types and in the same order. The result is similar to a "difference" operation in set theory.
The MINUS
operator is supported in some RDBMS like Oracle. Other databases might use different operators or approaches to achieve the same result, for instance, SQL Server uses the EXCEPT
operator.
SELECT column1, column2, ... FROM table1 MINUS SELECT column1, column2, ... FROM table2;
Consider two tables, employees
and retired_employees
. If you wanted to retrieve the id
of all employees who are not yet retired, you could use:
SELECT id FROM employees MINUS SELECT id FROM retired_employees;
This query would give you the list of employee IDs who are in the employees
table but not in the retired_employees
table.
Order of columns: The columns should be in the same order in both SELECT
statements, and they should have similar data types.
Duplicates: The MINUS
operator will remove duplicates, so the result will only contain distinct values.
Alternative in other databases: As mentioned earlier, not all databases support the MINUS
operator. For example, in SQL Server, you would use the EXCEPT
operator to achieve the same result. Always consult the specific database's documentation for details.
Performance: Depending on the dataset's size, the MINUS
operation might be expensive. Always ensure you have proper indexing and optimizations in place when working with large datasets.
How to use MINUS in SQL:
SELECT column1, column2 FROM table1 MINUS SELECT column1, column2 FROM table2;
Set operations in SQL with MINUS:
SELECT product_id FROM products MINUS SELECT product_id FROM out_of_stock_products;
MINUS vs. UNION in SQL:
-- Using MINUS SELECT employee_id FROM employees MINUS SELECT employee_id FROM terminated_employees; -- Using UNION SELECT employee_id FROM employees UNION SELECT employee_id FROM new_employees;
Using MINUS with multiple columns in SQL:
SELECT column1, column2, column3 FROM table1 MINUS SELECT column1, column2, column3 FROM table2;
Handling NULL values with MINUS in SQL:
SELECT column1, column2 FROM table1 MINUS SELECT column1, column2 FROM table2 WHERE column1 IS NOT NULL AND column2 IS NOT NULL;
MINUS operator with subqueries in SQL:
SELECT column1, column2 FROM table1 MINUS (SELECT column1, column2 FROM excluded_rows_table);
Alternatives to MINUS in SQL:
-- Using NOT EXISTS SELECT column1, column2 FROM table1 WHERE NOT EXISTS ( SELECT 1 FROM table2 WHERE table1.column1 = table2.column1 AND table1.column2 = table2.column2 ); -- Using LEFT JOIN and NULL check SELECT t1.column1, t1.column2 FROM table1 t1 LEFT JOIN table2 t2 ON t1.column1 = t2.column1 AND t1.column2 = t2.column2 WHERE t2.column1 IS NULL AND t2.column2 IS NULL;
Set operations and duplicates with MINUS:
SELECT product_id FROM products MINUS SELECT product_id FROM discounted_products;
MINUS vs. EXCEPT in SQL:
-- Using MINUS (Oracle) SELECT column1, column2 FROM table1 MINUS SELECT column1, column2 FROM table2; -- Using EXCEPT (PostgreSQL, SQL Server) SELECT column1, column2 FROM table1 EXCEPT SELECT column1, column2 FROM table2;
MINUS vs. NOT EXISTS in SQL:
-- Using MINUS SELECT column1, column2 FROM table1 MINUS SELECT column1, column2 FROM table2; -- Using NOT EXISTS SELECT column1, column2 FROM table1 t1 WHERE NOT EXISTS ( SELECT 1 FROM table2 t2 WHERE t1.column1 = t2.column1 AND t1.column2 = t2.column2 );
Combining MINUS with other operators in SQL:
SELECT column1, column2 FROM table1 WHERE column1 = 'A' MINUS SELECT column1, column2 FROM table2 WHERE column2 = 'B';