INTERSECT

SQL · SET Operators

Theory

INTERSECT returns only the rows that appear in the results of both queries, with duplicates removed. SELECT email FROM newsletter INTERSECT SELECT email FROM customers gives the emails of people who are both subscribers and customers. As with UNION, both queries need the same number of columns with compatible types, and rows are compared on all columns.

Unlike a join condition, INTERSECT treats two NULLs as matching, which makes it handy for comparing full rows. PostgreSQL, SQL Server, Oracle, SQLite and Snowflake support it, as do recent MySQL versions; on older MySQL you emulate it with INNER JOIN or EXISTS. INTERSECT ALL, which keeps duplicate counts, exists in PostgreSQL but not in every engine.

Why we use it

Finding overlap between two sets is a common question: customers who bought in both years, users in both lists, rows present in both source and target. INTERSECT states it directly without join logic.

When to use it

Use INTERSECT to find rows common to two result sets with the same shape. If you also need extra columns from either table, use INNER JOIN or EXISTS. For rows in one set but not the other, use EXCEPT / MINUS.

How it works

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

Syntax

SELECT columns FROM t1
INTERSECT
SELECT columns FROM t2;

Example

SELECT email FROM newsletter
INTERSECT
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