MINUS

SQL · SET Operators

Theory

MINUS returns the rows from the first query that do not appear in the second, with duplicates removed. Oracle calls it MINUS; the SQL standard, PostgreSQL, SQL Server and SQLite call it EXCEPT (Snowflake accepts both). SELECT email FROM newsletter MINUS SELECT email FROM customers lists subscribers who have not become customers.

Order matters: A MINUS B is not the same as B MINUS A, just like subtraction. Both queries need the same number of columns with compatible types, and NULLs are treated as equal, so it is safer than NOT IN when NULLs are present. It is a quick way to test whether two tables hold the same data: if A EXCEPT B and B EXCEPT A both return zero rows, the tables match. Older MySQL versions lack EXCEPT and use LEFT JOIN ... IS NULL or NOT EXISTS.

Why we use it

Finding what is missing is a daily task: subscribers who never bought, records in the source but not in the target, products never sold. MINUS / EXCEPT answers this with a single set operation.

When to use it

Use MINUS / EXCEPT to compare whole rows between two result sets, especially in data validation. If you need other columns from the first table, use NOT EXISTS or LEFT JOIN ... WHERE key IS NULL.

How it works

  1. Run the first SELECT (A)
  2. Run the second SELECT (B)
  3. Take a row from A
  4. Does the same row exist in B? Yes: Discard it. No: Keep it (once).
  5. Return rows found only in A

Syntax

SELECT columns FROM t1
MINUS
SELECT columns FROM t2;

Example

SELECT email FROM newsletter
MINUS
SELECT email FROM customers;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic