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

CROSS JOIN और SELF JOIN

CROSS JOIN का अर्थ हर Combination

CROSS JOIN में matching predicate नहीं होता—one input की हर row दूसरे input की हर row से pair होती है। Exact row formula M × N है। Designed grid के लिए useful, पर large या accidental inputs पर risky है।

CREATE TABLE exam_shifts (
  shift_name VARCHAR(20) PRIMARY KEY
);
INSERT INTO exam_shifts VALUES ('Morning'), ('Afternoon');
तीन classes × दो shifts से छह rows आनी चाहिए। Execute करने से पहले predict करें।

Verified Class–Shift Schedule

SELECT c.class_name, e.shift_name
FROM classes AS c
CROSS JOIN exam_shifts AS e
ORDER BY c.class_id, e.shift_name;
X-A | Afternoon X-A | Morning X-B | Afternoon X-B | Morning XI-A | Afternoon XI-A | Morning

Alphabetical shift order में Afternoon पहले है। Business order Morning फिर Afternoon चाहिए तो numeric shift_order column store करके उससे sort करें।

Accidental Cartesian Product रोकें

-- Intent unclear है और row count multiply होता है:
SELECT s.student_name, c.class_name
FROM join_students AS s
CROSS JOIN classes AS c;

पाँच students × तीन classes से 15 rows मिलती हैं, जिनमें अधिकतर real relation नहीं बताते। Connecting predicate के बिना comma-separated FROM list भी यही problem बना सकती है। Explicit JOIN syntax लें, ON review करें और output size estimate करें।

Production guard: 100,000 और 50,000 rows का CROSS JOIN later filter से पहले पाँच billion combinations बनाता है। Inputs पहले restrict करें और grid की requirement confirm करें।

SELF JOIN: Same Table के दो Roles

SELF JOIN pattern है, special keyword नहीं। Table दो aliases से आती है। हर alias एक role दिखाता है—जैसे first student और second student।

SELECT s1.student_name AS student_1,
       s2.student_name AS student_2,
       s1.class_id
FROM join_students AS s1
JOIN join_students AS s2
  ON s2.class_id = s1.class_id
 AND s1.student_id < s2.student_id
ORDER BY s1.student_id, s2.student_id;

Verified Same-Class Pairs

Aarav | Meera | 10 Kabir | Sana | 20

Equality same non-NULL class माँगती है। Strict less-than condition Aarav–Aarav self-pair हटाती और Aarav–Meera तथा Meera–Aarav में केवल one direction रखती है। Vihaan की class NULL है, इसलिए equality match नहीं करती।

ConditionEffect
s1.id = s2.idकेवल same row
s1.id != s2.idदोनों directions
s1.id < s2.idOne direction, no self-pair

दूसरे Useful SELF JOIN Patterns

Hierarchy

SELECT e.employee_name,
       m.employee_name AS manager_name
FROM employees AS e
LEFT JOIN employees AS m
  ON m.employee_id = e.manager_id;

LEFT JOIN top-level employees preserve करता है जिनका manager_id NULL है। Other patterns में consecutive events, versions compare करना, overlapping ranges और shared attribute वाली rows match करना शामिल हैं। हर case में meaningless pairs रोकने वाली condition चाहिए।

Performance और अभ्यास

  • manager_id जैसी self-referencing keys और class_id जैसे equality attributes index करें।
  • Pair count estimate करें; N rows का group N(N−1)/2 unique unordered pairs बना सकता है।
  • Requirement safely allow करे तो दोनों roles को early filter करें।
  • EXPLAIN देखें, पर पहले pair-generation logic correct verify करें।

अभ्यास: Evening shift जोड़कर nine combinations predict करें; one class में three students देकर three unique pairs predict करें; manager SELF JOIN लिखें जो no-manager principal भी दिखाए।

Official संदर्भ

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

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

CROSS JOIN क्या लौटाता है?
यह first input की हर row को second input की हर row से combine करता है। M और N rows से M × N result rows बनती हैं।
क्या CROSS JOIN हमेशा mistake है?
नहीं। हर class–exam shift combination जैसे intentional cases में सही है। Missing relationship predicate से बने तो dangerous है।
SELF JOIN क्या है?
यह normal join है जिसमें same table को दो aliases से लिखकर अलग rows या roles compare किए जाते हैं।
Mirrored self-join pairs कैसे रोकें?
s1.student_id < s2.student_id जैसी strict ordering condition लें। इससे self-pairs हटते और हर pair की केवल one direction बचती है।
क्या SELF JOIN का special keyword है?
नहीं। Same table को distinct aliases से दो बार use करें और requirement के अनुसार INNER या OUTER semantics चुनें।
🔗

Share this topic with a friend

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

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

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

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

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