SQL Tutorial

SQL Clauses / Operators

SQL-Injection

SQL Functions

SQL Queries

PL/SQL

MySQL

SQL Server

Misc

SQL | MINUS Operator

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.

Syntax:

SELECT column1, column2, ...
FROM table1
MINUS
SELECT column1, column2, ...
FROM table2;

Example:

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.

Important Points:

  1. Order of columns: The columns should be in the same order in both SELECT statements, and they should have similar data types.

  2. Duplicates: The MINUS operator will remove duplicates, so the result will only contain distinct values.

  3. 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.

  4. 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.

  1. How to use MINUS in SQL:

    • Description: The MINUS operator in SQL is used to retrieve the rows from the first query result that are not present in the second query result.
    • Example Code:
      SELECT column1, column2 FROM table1
      MINUS
      SELECT column1, column2 FROM table2;
      
  2. Set operations in SQL with MINUS:

    • Description: MINUS is a set operation in SQL that performs a set difference, retrieving distinct rows from the first result set that are not present in the second result set.
    • Example Code:
      SELECT product_id FROM products
      MINUS
      SELECT product_id FROM out_of_stock_products;
      
  3. MINUS vs. UNION in SQL:

    • Description: MINUS performs a set difference, while UNION performs a set union. MINUS retrieves rows from the first query that are not present in the second query, whereas UNION combines distinct rows from both queries.
    • Example Code:
      -- 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;
      
  4. Using MINUS with multiple columns in SQL:

    • Description: MINUS can be applied to queries with multiple columns to find unique rows across all specified columns.
    • Example Code:
      SELECT column1, column2, column3 FROM table1
      MINUS
      SELECT column1, column2, column3 FROM table2;
      
  5. Handling NULL values with MINUS in SQL:

    • Description: MINUS handles NULL values by treating them as equivalent, meaning rows with NULL values in corresponding columns will be considered identical.
    • Example Code:
      SELECT column1, column2 FROM table1
      MINUS
      SELECT column1, column2 FROM table2
      WHERE column1 IS NOT NULL AND column2 IS NOT NULL;
      
  6. MINUS operator with subqueries in SQL:

    • Description: MINUS can be used with subqueries to perform set differences based on the results of the subqueries.
    • Example Code:
      SELECT column1, column2 FROM table1
      MINUS
      (SELECT column1, column2 FROM excluded_rows_table);
      
  7. Alternatives to MINUS in SQL:

    • Description: Depending on the database system, alternatives to MINUS include NOT IN, NOT EXISTS, or LEFT JOIN with NULL check.
    • Example Code:
      -- 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;
      
  8. Set operations and duplicates with MINUS:

    • Description: MINUS removes duplicates from the result set. If a row appears multiple times in the first query result and not in the second query result, it will be included only once.
    • Example Code:
      SELECT product_id FROM products
      MINUS
      SELECT product_id FROM discounted_products;
      
  9. MINUS vs. EXCEPT in SQL:

    • Description: MINUS and EXCEPT are similar set difference operators. MINUS is used in Oracle, while EXCEPT is used in databases like PostgreSQL and SQL Server.
    • Example Code:
      -- 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;
      
  10. MINUS vs. NOT EXISTS in SQL:

    • Description: MINUS and NOT EXISTS can be used to achieve similar results, but they have different syntax. MINUS is a set operator, while NOT EXISTS is a predicate.
    • Example Code:
      -- 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
      );
      
  11. Combining MINUS with other operators in SQL:

    • Description: MINUS can be combined with other operators like UNION, INTERSECT, and subqueries to perform complex set operations.
    • Example Code:
      SELECT column1, column2 FROM table1
      WHERE column1 = 'A'
      MINUS
      SELECT column1, column2 FROM table2
      WHERE column2 = 'B';