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

UNION से MySQL में FULL OUTER JOIN

MySQL में FULL OUTER JOIN Keyword नहीं

Full outer result में matched pairs, left-only rows और right-only rows होती हैं। MySQL INNER, LEFT और RIGHT joins देता है, direct FULL OUTER JOIN clause नहीं। Keyword लिखने पर syntax error होगा; set-operation pattern चाहिए।

Requirement first: यह pattern तभी लें जब report को दोनों populations की unmatched rows सच में चाहिए। कई reports के लिए simpler LEFT JOIN पर्याप्त है।

Expected Full Result Define करें

School lab में पाँच students और तीन classes हैं:

  • चार students class 10 या 20 से match होते हैं।
  • Vihaan left-only है क्योंकि class_id NULL है।
  • XI-A right-only है क्योंकि कोई student class_id 30 use नहीं करता।

Full result में छह rows होनी चाहिए: चार matched + one student-only + one class-only। यह prediction emulation को testable बनाती है।

Correct LEFT JOIN + UNION ALL Pattern

SELECT s.student_id, s.student_name,
       c.class_id, c.class_name
FROM join_students AS s
LEFT JOIN classes AS c
  ON c.class_id = s.class_id

UNION ALL

SELECT s.student_id, s.student_name,
       c.class_id, c.class_name
FROM classes AS c
LEFT JOIN join_students AS s
  ON s.class_id = c.class_id
WHERE s.student_id IS NULL;

First branch हर student और उसका class match देती है। Second preservation reverse करती है, पर WHERE केवल वे class rows रखता है जिनका student नहीं मिला। Matched pairs दूसरी बार append नहीं होते।

Verified Six-Row Output

student_id | student_name | class_id | class_name 1 | Aarav | 10 | X-A 2 | Meera | 10 | X-A 3 | Kabir | 20 | X-B 4 | Sana | 20 | X-B 5 | Vihaan | NULL | NULL NULL | NULL | 30 | XI-A

Display order चाहिए तो set operation को derived table में wrap करके final result sort करें:

SELECT *
FROM (
  SELECT s.student_id, s.student_name,
         c.class_id, c.class_name
  FROM join_students AS s
  LEFT JOIN classes AS c
    ON c.class_id = s.class_id
  UNION ALL
  SELECT s.student_id, s.student_name,
         c.class_id, c.class_name
  FROM classes AS c
  LEFT JOIN join_students AS s
    ON s.class_id = c.class_id
  WHERE s.student_id IS NULL
) AS full_result
ORDER BY COALESCE(class_id, 999999), student_id;

Naive UNION क्यों नहीं?

-- Common but weaker pattern:
left_side_with_matches
UNION
right_side_with_matches

UNION पूरे set में duplicate projected rows हटाता है। Selected columns keys न हों तो legitimate duplicates छिप सकते हैं और distinct processing work बढ़ता है। Anti-match filter + UNION ALL actual logic स्पष्ट करता है—पहले all left rows, फिर केवल missing right rows।

दो unrestricted outer branches को UNION ALL न करें; हर matched pair twice आएगा। Anti-match test के लिए nullable descriptive column न लें।

Useful Counts और Variants

Row origin label करें

SELECT CASE
         WHEN student_id IS NULL THEN 'class_only'
         WHEN class_id IS NULL THEN 'student_only'
         ELSE 'matched'
       END AS row_status,
       COUNT(*) AS rows_count
FROM (
  SELECT s.student_id, c.class_id
  FROM join_students AS s
  LEFT JOIN classes AS c
    ON c.class_id = s.class_id
  UNION ALL
  SELECT s.student_id, c.class_id
  FROM classes AS c
  LEFT JOIN join_students AS s
    ON s.class_id = c.class_id
  WHERE s.student_id IS NULL
) AS x
GROUP BY row_status;
matched | 4 student_only | 1 class_only | 1

Different match rule या composite key हो तो complete predicate दोनों branches में repeat करें। Left-only, right-only और multiple-match data से test करें।

Safety Checklist और अभ्यास

  1. Confirm करें कि MySQL में direct FULL syntax नहीं, emulation चाहिए।
  2. Absence test के लिए non-nullable key चुनें।
  3. Exact relationship दोनों branches में repeat करें।
  4. UNION ALL लें और branch two में only unmatched rows रखें।
  5. Matched, left-only और right-only counts separately verify करें।
  6. ORDER BY final combined result पर लगाएँ।

अभ्यास: no-student class 40 जोड़कर seven rows predict करें; फिर class 30 में assigned student जोड़ें और origin counts का change समझाएँ।

Official संदर्भ

Syntax और optimizer notes official MySQL 8.4 manual से जाँचे गए हैं। अपने schema और data पर plan तथा row count अवश्य test करें।

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

क्या MySQL FULL OUTER JOIN support करता है?
MySQL 8.4 में FULL OUTER JOIN keyword नहीं है। दोनों sides की unmatched rows चाहिए तो two outer-join branches combine करें।
Safest common emulation क्या है?
All left rows के लिए LEFT JOIN लें, reverse LEFT JOIN को UNION ALL करें और second branch में original-left non-nullable key IS NULL से right-only rows रखें।
UNION की जगह UNION ALL क्यों?
Anti-match filter matched pairs repeat होने से रोकता है। UNION ALL legitimate duplicate-looking results बचाता और distinct elimination work टालता है।
Matched rows twice count होने से कैसे रोकें?
Reverse branch में original left table की non-nullable primary key IS NULL लगाएँ, ताकि केवल right-only rows append हों।
क्या nullable join keys पर emulation चलती है?
हाँ, लेकिन absence detect करने के लिए missing side की non-nullable primary key test करें, nullable join key नहीं।
🔗

Share this topic with a friend

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

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

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

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

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