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

MySQL में INTERSECT और EXCEPT Alternatives

MySQL 8.4 दोनों Operators Support करता है

MySQL 8.4 direct UNION, INTERSECT और EXCEPT support करता है। INTERSECT common result rows रखता है; EXCEPT left-result की वे rows रखता है जो right में absent हैं। हर operator ALL modifier support करता है। MINUS MySQL spelling नहीं है।

Version accuracy: कई older articles केवल JOIN/EXISTS alternatives सिखाते हैं। Alternatives useful हैं, लेकिन current MySQL 8.4 में INTERSECT/EXCEPT unavailable कहना गलत है।

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 और Sana; केवल A में Meera; केवल B में Kabir। Primary keys हर branch distinct बनाती हैं, इसलिए इस data पर DISTINCT और ALL forms same rows देंगी।

INTERSECT: दोनों Results में Present

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

Default INTERSECT distinct set semantics देता है। Source query blocks में repeated rows हों तो INTERSECT ALL duplicate multiplicity preserve कर सकता है।

EXCEPT: केवल Left में Present

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

Direction matter करती है। Branches reverse करना अलग question है:

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

Portable JOIN और EXISTS Alternatives

EXISTS से INTERSECT

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

NOT EXISTS से EXCEPT

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 older deployments और extra predicates में मदद करते हैं, लेकिन हर compared column match करें और required duplicate semantics deliberately reproduce करें।

Set-Operation Rules

  • हर query block same number of columns दे।
  • Corresponding column types compatible हों; columns position से match होती हैं।
  • Output names first query block से आते हैं।
  • Default forms duplicates हटाते; ALL operator के अनुसार multiplicity रखता है।
  • Final ORDER BY combined result sort करता है।
  • Mixed operations की grouping clear रखने के लिए parentheses लें।

Multi-column rows compare करते समय हर business-key column direct branches या alternative predicates में शामिल करें। One column omit करना set definition बदलता है।

Version Check, Performance और अभ्यास

SELECT VERSION();

Actual production server version confirm करें, केवल client या hosting-panel label नहीं। Older server पर direct syntax unavailable हो तो tested EXISTS/NOT EXISTS alternatives लें।

  1. पहले DISTINCT या ALL semantics चुनें।
  2. Narrow rows project और branches safely filter करें।
  3. Alternative correlation keys index करें।
  4. Unique demo के साथ duplicates और NULL भी test करें।
  5. EXPLAIN और representative volumes लें।

अभ्यास: only-A और only-B lists; second comparison column जोड़ें; disposable tables में duplicate source rows बनाकर direct ALL और alternative compare करें।

Official संदर्भ

Syntax और behavior official MySQL 8.4 manual से जाँचे गए हैं। Production से पहले हर query disposable copy पर test करें।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

क्या MySQL 8.4 INTERSECT support करता है?
हाँ। MySQL 8.4 direct INTERSECT और ALL modifier support करता है। Unsupported कहने वाले older tutorials इस version के लिए outdated हैं।
क्या MySQL 8.4 EXCEPT support करता है?
हाँ। EXCEPT left query की वे rows देता है जो right result में absent हों; EXCEPT ALL duplicate-count differences रखता है।
क्या MySQL MINUS support करता है?
नहीं। Supported MySQL versions में EXCEPT लें।
INTERSECT को कैसे emulate करें?
सभी compared columns पर INNER JOIN या correlated EXISTS लें; set semantics में duplicate removal चाहिए तो सामान्यतः DISTINCT जोड़ें।
EXCEPT safely कैसे emulate करें?
सभी compared columns पर correlated NOT EXISTS या non-nullable key IS NULL वाला LEFT JOIN लें। Nullable NOT IN traps से बचें।
🔗

Share this topic with a friend

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

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

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

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.