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

Multiple Table Joins

कई tables join करना एक card से अगले तक clues के एक trail follow करने जैसा है: customers से orders से products, हर link में hop करते हुए।
Syntax
sql
SELECT columns
FROM table1
JOIN table2 ON table1.column = table2.column
JOIN table3 ON table2.column = table3.column;

तीन Tables Join करना

जब आपका data कई related tables में फैला हो तो आप एक single query में तीन या ज़्यादा tables connect कर सकते हैं। इसके लिए, बस कई JOIN clauses एक के बाद एक chain करें, हर एक पहले join की गई किसी table से linking करते हुए।

उदाहरण: Joining Three Tables

sql
CREATE TABLE customers (customer_id INT, name TEXT);
CREATE TABLE orders (order_id INT, customer_id INT, product_id INT);
CREATE TABLE products (product_id INT, product_name TEXT);
INSERT INTO customers VALUES (1, 'Amit');
INSERT INTO products VALUES (10, 'Keyboard');
INSERT INTO orders VALUES (101, 1, 10);
SELECT customers.name, products.product_name
FROM orders
JOIN customers ON orders.customer_id = customers.customer_id
JOIN products ON orders.product_id = products.product_id;

INNER और LEFT JOINs Mix करना

आप किसी query भर में सिर्फ एक join type इस्तेमाल करने तक limited नहीं हैं। आप same query के अंदर INNER JOIN और LEFT JOIN को freely mix कर सकते हैं, जैसे एक relationship पर match माँगते हुए दूसरे को optional रहने देना।

उदाहरण: Mixing INNER and LEFT JOINs

sql
CREATE TABLE customers (customer_id INT, name TEXT);
CREATE TABLE orders (order_id INT, customer_id INT);
CREATE TABLE loyalty (customer_id INT, tier TEXT);
INSERT INTO customers VALUES (1, 'Amit');
INSERT INTO orders VALUES (101, 1);
SELECT customers.name, orders.order_id, loyalty.tier
FROM customers
INNER JOIN orders ON customers.customer_id = orders.customer_id
LEFT JOIN loyalty ON customers.customer_id = loyalty.customer_id;

Table Aliases इस्तेमाल करना

कई tables join करते समय, column names confusing हो सकते हैं, खासकर अगर कई tables id या name जैसा एक column share करती हों। अपने multi-join queries को clean और readable रखने के लिए हमेशा short table aliases इस्तेमाल करें, और यह disambiguate करने के लिए कि हर column किस table से आता है।

उदाहरण: Using Table Aliases

sql
CREATE TABLE customers (id INT, name TEXT);
CREATE TABLE orders (id INT, customer_id INT);
INSERT INTO customers VALUES (1, 'Amit');
INSERT INTO orders VALUES (101, 1);
SELECT c.name, o.id AS order_id
FROM orders o
JOIN customers c ON o.customer_id = c.id;

Foreign Keys के through Join करना

Multi-table joins आमतौर पर आपकी schema द्वारा defined आपके database relationships के path follow करते हैं। आप आमतौर पर foreign keys इस्तेमाल करके parent tables को child tables से join करते हैं, relationship graph में एक बार में एक hop चलते हुए।

उदाहरण: Joining via Foreign Keys

sql
CREATE TABLE customers (customer_id INT, name TEXT);
CREATE TABLE orders (order_id INT, customer_id INT);
CREATE TABLE order_items (order_id INT, product_name TEXT);
INSERT INTO customers VALUES (1, 'Amit');
INSERT INTO orders VALUES (101, 1);
INSERT INTO order_items VALUES (101, 'Keyboard');
SELECT customers.name, order_items.product_name
FROM customers
JOIN orders ON customers.customer_id = orders.customer_id
JOIN order_items ON orders.order_id = order_items.order_id;

Multiple Table Joins Filter करना

combined result set filter करने के लिए आप अपनी join chain के अंत में एक WHERE clause add कर सकते हैं। यह आपको सारे joins resolve हो जाने के बाद, joined सभी tables की combined rows को साथ filter करने देता है।

उदाहरण: Filtering Multiple Table Joins

sql
CREATE TABLE customers (customer_id INT, name TEXT);
CREATE TABLE orders (order_id INT, customer_id INT, total INT);
INSERT INTO customers VALUES (1, 'Amit');
INSERT INTO orders VALUES (101, 1, 500);
SELECT customers.name, orders.total
FROM customers
JOIN orders ON customers.customer_id = orders.customer_id
WHERE orders.total > 100;
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. गलत column पर एक तीसरी table join करना, ताकि rows duplicate या lost हो जाएँ।
  2. एक LEFT JOIN के बाद INNER JOIN mix करना, जो चुपचाप unmatched rows हटा सकता है।
  3. कई tables एक column name share करते समय table aliases इस्तेमाल न करना, ambiguous column errors का कारण बनते हुए।
🔒

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.