Last Chance to Join Data Science Interview MasterClass (this week) 🚀 | Just 2 spots remaining...

PRO SQL Modules

SQL Basics
1h 20m • 4 lessons
  1. SQL SELECT
  2. LIMIT
  3. ORDER BY
  4. DISTINCT
Data Filtering
1h 20m • 4 lessons
  1. WHERE
  2. LIKE
  3. IN
  4. BETWEEN
Aggregations
40m • 2 lessons
  1. GROUP BY
  2. HAVING
Multiple Tables
1h 40m • 5 lessons
  1. INNER JOIN
  2. LEFT & RIGHT JOIN
  3. FULL OUTER JOIN
  4. SELF JOIN
  5. INTERSECT & EXCEPT
Query Restructuring
40m • 2 lessons
  1. Subquery
  2. Common Table Expressions (CTE)
Data Transformation
1h 40m • 5 lessons
  1. CASE
  2. String
  3. Date Manipulation
  4. Math Operations
  5. COALESCE
Analytical SQL
2h • 6 lessons
  1. Ranking - ROW_NUMBER(), RANK(), DENSE_RANK()
  2. Running Totals
  3. LAG and LEAD
  4. FIRST_VALUE, LAST_VALUE, Nth_VALUE
  5. PARTITION BY
  6. SQL BETWEEN and PRECEDING
Unlock Premium
FREE

INTERSECT & EXCEPT

Course Author Daniel Lee
Instructor

Dan Lee

Learn SQL for free with interactive exercises designed to help aspiring Data Engineers, Data Analysts, and Data Scientists. Learn the basics and advanced SQL concepts by solving real-world problems from MANGO (Meta, Amazon, Netflix, Google, OpenAI)

Lesson Objectives

By the end of this lesson, you will:

  • Learn how to use INTERSECT to find common records between queries
  • Understand how EXCEPT removes values from one query that exist in another
  • Write SQL queries to analyze Google Ads campaign data using set operations

🎬 Scenario: Analyzing Advertisers and Their Campaigns

Your manager at Google Ads, Jordan, wants to compare different sets of advertisers and campaigns.

“Can you find advertisers who have run both Search and Display ads? Also, can we check which advertisers signed up but never launched a campaign?”

Your task is to use INTERSECT to find common advertisers and EXCEPT to identify advertisers without campaigns.


1. Understanding INTERSECT & EXCEPT

OperatorWhat It DoesRemoves Duplicates?Use Case
INTERSECTReturns rows that exist in both queriesâś… YesFinding common records
EXCEPTReturns rows from the first query that don’t exist in the second✅ YesFinding differences between two datasets

Syntax for INTERSECT

1SELECT column1 FROM table1  
2INTERSECT  
3SELECT column1 FROM table2;
  • Returns only rows that appear in both queries.

Syntax for EXCEPT

1SELECT column1 FROM table1  
2EXCEPT  
3SELECT column1 FROM table2;
  • Returns only rows from the first query that are NOT in the second query.

2. Using INTERSECT to Find Common Advertisers

Jordan wants to find advertisers who have run both Search and Display campaigns.

Example Query: Finding Advertisers Running Both Search and Display Ads

1SELECT user_id FROM google_ads_campaigns WHERE ad_type = 'Search'  
2INTERSECT  
3SELECT user_id FROM google_ads_campaigns WHERE ad_type = 'Display';
user_id
2
4

What’s Happening?

  • The first query retrieves advertisers with Search ads.
  • The second query retrieves advertisers with Display ads.
  • INTERSECT returns only advertisers who appear in both queries.

3. Using EXCEPT to Find Advertisers Without Campaigns

Jordan also wants to see which advertisers have signed up but never launched a campaign.

Example Query: Finding Advertisers Without Campaigns

1SELECT user_id FROM google_ads_users  
2EXCEPT  
3SELECT DISTINCT user_id FROM google_ads_campaigns;
user_id
3

What’s Happening?

  • The first query retrieves all registered advertisers.
  • The second query retrieves advertisers who have launched at least one campaign.
  • EXCEPT removes advertisers who have campaigns, leaving only those without campaigns.

4. INTERSECT vs. EXCEPT Performance

ScenarioBest ChoiceWhy?
Finding users present in both datasetsINTERSECTEnsures only common results are kept
Identifying missing or excluded dataEXCEPTFinds records in one dataset but not in another
Comparing lists of customers, transactions, or activity logsBothDepends on whether you want common or missing records

Tip:

  • Since INTERSECT and EXCEPT remove duplicates, they can be slower on large datasets.
  • Use UNION ALL or filtering (WHERE NOT IN) if performance is a concern.

✍️ SQL Exercises

Exercise 1: Finding Advertisers Running Both Search and Display Ads

Jordan wants to list advertisers who have run both Search and Display campaigns.

Filter condition:

  • Retrieve user IDs of advertisers who have both Search and Display ads.
  • Ensure no duplicates appear.
  • Sort by user_id in ascending order.
user_id
2
4

Write an SQL query to return the requested data.

Upgrade to Pro

Exercise 2: Finding Advertisers Without Campaigns

Jordan also wants to see which advertisers signed up but never launched a campaign.

Filter condition:

  • Retrieve user IDs of advertisers who haven’t launched a campaign.
user_id
3

Write an SQL query to return the requested data.

Upgrade to Pro
👉 Found this lesson helpful?