← Back to MySQL Course | Chapter 9: Joins | Lesson 7 of 10

CROSS JOIN

एक CROSS JOIN सभी possible outfits देखने के लिए हर shirt को हर pants से pair करने जैसा है। एक table की हर row दूसरे की हर row से मिलती है।
Syntax
sql
SELECT columns
FROM table1
CROSS JOIN table2;

एक CROSS JOIN क्या है?

एक CROSS JOIN दो tables के बीच एक Cartesian product produce करता है। यह पहली table की हर single row को दूसरी table की हर single row के साथ pair करता है। यह तब helpful है जब आपको items के सभी possible combinations देखने हों, जैसे हर size को हर color के साथ pair करना।

उदाहरण: What is a CROSS JOIN?

sql
CREATE TABLE sizes (size TEXT);
CREATE TABLE colors (color TEXT);
INSERT INTO sizes VALUES ('S'), ('M');
INSERT INTO colors VALUES ('Red'), ('Blue');
SELECT sizes.size, colors.color FROM sizes CROSS JOIN colors;

Explicit CROSS JOIN Syntax

आप अपनी query में बिना किसी ON condition के explicitly CROSS JOIN keywords इस्तेमाल कर सकते हैं। यह दूसरे developers को clearly दिखाता है कि आप जानबूझकर दोनों tables का एक Cartesian product बनाने का intend रखते हैं, गलती से नहीं।

उदाहरण: Explicit CROSS JOIN Syntax

sql
CREATE TABLE sizes (size TEXT);
CREATE TABLE colors (color TEXT);
INSERT INTO sizes VALUES ('S'), ('M');
INSERT INTO colors VALUES ('Red'), ('Blue');
-- Explicit CROSS JOIN: intent is clear to other developers
SELECT sizes.size, colors.color FROM sizes CROSS JOIN colors;

Implicit CROSS JOIN

आप FROM clause में बिना किसी join condition के tables को comma से separated list करके एक cross join implicitly लिख सकते हैं। यह exactly एक explicit CROSS JOIN जैसा behave करता है, हालाँकि अगर आप उन्हें link करने वाली एक WHERE condition भूल जाएँ तो इसे गलती से लिखना आसान है।

उदाहरण: Implicit CROSS JOIN

sql
CREATE TABLE sizes (size TEXT);
CREATE TABLE colors (color TEXT);
INSERT INTO sizes VALUES ('S'), ('M');
INSERT INTO colors VALUES ('Red'), ('Blue');
-- Implicit cross join: comma-separated, no ON condition
SELECT sizes.size, colors.color FROM sizes, colors;

WHERE Filter के साथ CROSS JOIN

आप CROSS JOIN द्वारा produced combinations को एक useful subset तक filter कर सकते हैं। अपनी requirements के आधार पर final results limit करने के लिए बस एक standard WHERE clause add करें, effectively इसे एक manual join condition में बदलते हुए।

उदाहरण: CROSS JOIN with a WHERE Filter

sql
CREATE TABLE sizes (size TEXT);
CREATE TABLE colors (color TEXT);
INSERT INTO sizes VALUES ('S'), ('M');
INSERT INTO colors VALUES ('Red'), ('Blue');
SELECT sizes.size, colors.color FROM sizes CROSS JOIN colors WHERE colors.color = 'Red';

CROSS JOIN के Practical Use Cases

CROSS JOIN matrix structures generate करने के लिए बढ़िया है जहाँ हर combination genuinely मायने रखता है। इसमें card suits को ranks से match करना, या एक complete combo list बनाने के लिए menu food items को drink choices से pair करना शामिल है।

उदाहरण: Practical Use Cases for CROSS JOIN

sql
CREATE TABLE suits (suit TEXT);
CREATE TABLE ranks (rank_name TEXT);
INSERT INTO suits VALUES ('Hearts'), ('Spades');
INSERT INTO ranks VALUES ('Ace'), ('King');
SELECT ranks.rank_name, suits.suit FROM ranks CROSS JOIN suits;
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. दो बड़ी tables पर एक cross join चलाना, जो दो row counts को multiply करने के बराबर rows return करता है और server को overwhelm कर सकता है।
  2. एक ordinary join में join condition भूल जाना और गलती से एक cross join पाना।
  3. CROSS JOIN में ON add करना और एक filter की उम्मीद करना, जबकि combinations को WHERE से filter किया जाना चाहिए।
🔒

Chapter Quiz — Complete all 10 topics to unlock

0/10 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.