Learn SQL

SQL UNION, INTERSECT, and EXCEPT Explained: Combine and Compare Query Results

Set operations stack or compare whole query results. They are the quickest way to merge yearly exports or find rows that appear in one list but not the other.

Published 2026-10-29·Updated 2026-10-29

Joins combine columns, set operations combine rows

A JOIN puts matching rows side by side. Set operations put whole query results on top of each other, or compare them. They answer questions like: who is on either list, who is on both, and who is only on the first one?

The examples use two newsletter lists from the Set Operations chapter in SQL for Files, a browser SQL editor that runs DuckDB locally on CSV, JSON, and Parquet files.

UNION vs UNION ALL

UNION ALL stacks every row from both queries. UNION does the same and then removes duplicate rows. Three people subscribed in both years, so UNION ALL returns 10 emails and UNION returns 7.

SELECT email FROM newsletter_2023
UNION
SELECT email FROM newsletter_2024
ORDER BY email;
  • Both queries must return the same number of columns.
  • Columns are matched by position, so keep the same order and compatible types.
  • ORDER BY comes once, at the end, and sorts the combined result.
  • UNION ALL is cheaper because it skips the duplicate check.

INTERSECT returns the overlap

INTERSECT keeps rows that appear in both results. It compares entire rows, so selecting email and country requires both to match.

SELECT email FROM newsletter_2023
INTERSECT
SELECT email FROM newsletter_2024
ORDER BY email;

EXCEPT returns what only the first query has

EXCEPT returns rows from the first query that are missing from the second. The order of the two queries matters: swap them and you answer the opposite question. Some databases call this operator MINUS.

-- Subscribers who did not come back in 2024
SELECT email FROM newsletter_2023
EXCEPT
SELECT email FROM newsletter_2024
ORDER BY email;

Compare two exports of the same file

EXCEPT in both directions is a fast way to see which records disappeared or appeared between last month's export and this month's.

Stack files with UNION ALL and a label column

When the same data is split across files, such as one export per year, load each file as a table and stack them with UNION ALL. A literal column records where each row came from, and the stacked result can be grouped like any table.

WITH all_signups AS (
  SELECT 2023 AS signup_year, email, country FROM newsletter_2023
  UNION ALL
  SELECT 2024 AS signup_year, email, country FROM newsletter_2024
)
SELECT signup_year, COUNT(*) AS subscribers
FROM all_signups
GROUP BY signup_year
ORDER BY signup_year;

UNION can shrink your counts

Plain UNION removes identical rows, which silently lowers counts when two files contain the same record. Use UNION ALL before aggregating. If the files list columns in different orders, DuckDB's UNION ALL BY NAME matches them by name.

Practice set operations

  1. Open the Set Operations chapter in the SQL for Files Learn SQL course.
  2. Load the newsletter_2023 and newsletter_2024 sample tables.
  3. Solve the UNION, INTERSECT, EXCEPT, and stacking challenges.
  4. Try the same patterns on two exports of one of your own files.

Continue in the editor

Open SQL for Files to add your own CSV, JSON, or Parquet files and try these examples locally in your browser.