LQ
SQL Reference
Practice →
SQL ReferenceExpertSet Operations

Set Operations

Combine or compare results from multiple queries.

Questions this answers
  • How do I combine results from two queries into one list?
  • How do I find rows that appear in both queries?
  • How do I find rows in one query but not another?

How it works

UNION, INTERSECT, and EXCEPT operate on the results of two SELECT statements. They require the same number of columns with compatible types. UNION combines results; INTERSECT finds overlap; EXCEPT finds rows in the first result not in the second.

UNION — combine results

SELECT name FROM artists WHERE genre = 'Pop'
UNION
SELECT name FROM artists WHERE monthly_listeners > 10000000;
💡 UNION removes duplicates. UNION ALL keeps them (and is faster).

INTERSECT — find overlap

SELECT customer_id FROM orders WHERE year = 2023
INTERSECT
SELECT customer_id FROM orders WHERE year = 2024;
💡 Customers who ordered in both 2023 and 2024.

EXCEPT — find difference

SELECT id FROM customers
EXCEPT
SELECT customer_id FROM orders;
💡 Customers who have never placed an order.

Ready to practise Set Operations?

Solve real exercises with AI feedback — free to start.

Try it free →