Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
MySQL + SQL · Lesson 77

INTERSECT and EXCEPT Alternatives in MySQL

MySQL 8.4 Supports Both Operators

MySQL 8.4 directly supports UNION, INTERSECT and EXCEPT. INTERSECT keeps rows common to query results; EXCEPT keeps left-result rows absent from the right result. Each operator supports an ALL modifier. MINUS is not the MySQL spelling.

Version accuracy: Many older articles teach only JOIN/EXISTS alternatives. Those alternatives remain useful, but stating that current MySQL 8.4 lacks INTERSECT or EXCEPT is incorrect.

Verified Club Data

CREATE TABLE club_a (
  student_name VARCHAR(60) PRIMARY KEY
);
CREATE TABLE club_b (
  student_name VARCHAR(60) PRIMARY KEY
);

INSERT INTO club_a VALUES
('Aarav'), ('Meera'), ('Sana');

INSERT INTO club_b VALUES
('Aarav'), ('Kabir'), ('Sana');

Common names: Aarav and Sana. Only in A: Meera. Only in B: Kabir. Primary keys make each branch distinct, so DISTINCT and ALL forms produce the same rows for this small dataset.

INTERSECT: Present in Both Results

SELECT student_name FROM club_a
INTERSECT
SELECT student_name FROM club_b
ORDER BY student_name;
Aarav Sana

Default INTERSECT has distinct set semantics. INTERSECT ALL can preserve duplicate multiplicity when source query blocks contain repeated rows.

EXCEPT: Present Only on the Left

SELECT student_name FROM club_a
EXCEPT
SELECT student_name FROM club_b
ORDER BY student_name;
Meera

Direction matters. Reversing the branches asks a different question:

SELECT student_name FROM club_b
EXCEPT
SELECT student_name FROM club_a
ORDER BY student_name;
Kabir

Portable JOIN and EXISTS Alternatives

INTERSECT using EXISTS

SELECT DISTINCT a.student_name
FROM club_a AS a
WHERE EXISTS (
  SELECT 1
  FROM club_b AS b
  WHERE b.student_name = a.student_name
)
ORDER BY a.student_name;
Aarav Sana

EXCEPT using NOT EXISTS

SELECT a.student_name
FROM club_a AS a
WHERE NOT EXISTS (
  SELECT 1
  FROM club_b AS b
  WHERE b.student_name = a.student_name
)
ORDER BY a.student_name;
Meera

Alternatives help with older deployments and allow extra predicates, but match every compared column and reproduce required duplicate semantics deliberately.

Set-Operation Rules

  • Each query block must return the same number of columns.
  • Corresponding column types must be compatible; columns match by position.
  • Output names come from the first query block.
  • Default forms remove duplicates; ALL retains multiplicity according to the operator.
  • Final ORDER BY sorts the combined result.
  • Parenthesize mixed operations when grouping must be unmistakable.

When comparing multi-column rows, include every business-key column in direct branches or alternative predicates. Omitting one column changes the set definition.

Version Check, Performance and Practice

SELECT VERSION();

Confirm the actual production server version, not only the client or hosting-panel label. If direct syntax is unavailable on an older server, use tested EXISTS/NOT EXISTS alternatives.

  1. Choose DISTINCT or ALL semantics first.
  2. Project narrow rows and filter branches safely.
  3. Index keys used by alternative correlation.
  4. Test duplicates and NULL values, not only unique demo data.
  5. Use EXPLAIN and representative volumes.

Practice: return only-A and only-B lists; add a second comparison column; then create duplicate source rows in disposable tables and compare direct ALL with your alternative.

Official References

Syntax and behavior were checked against the official MySQL 8.4 manual. Test every query on a disposable copy before production use.

Frequently Asked Questions

Does MySQL 8.4 support INTERSECT?
Yes. MySQL 8.4 directly supports INTERSECT and the ALL modifier. Older tutorials saying it is unsupported are outdated for this version.
Does MySQL 8.4 support EXCEPT?
Yes. EXCEPT returns rows from the left query result that are absent from the right; EXCEPT ALL preserves duplicate-count differences.
Does MySQL support MINUS?
No. Use EXCEPT in supported MySQL versions.
How can INTERSECT be emulated?
Use an INNER JOIN or correlated EXISTS on all compared columns, usually with DISTINCT when set semantics require duplicate removal.
How can EXCEPT be emulated safely?
Use NOT EXISTS correlated on all compared columns, or a LEFT JOIN with a non-nullable key IS NULL. Avoid nullable NOT IN traps.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.