← Back to Python Course | Chapter 15: Python & MySQL | Lesson 12 of 12

Python MySQL Join

एक JOIN, दो या ज़्यादा related tables की rows को एक single result set में जोड़ता है, उनके बीच एक matching column के आधार पर -- जैसे किसी orders table के हर order को एक अलग customers table में उसके specific customer से एक shared customer_id column के ज़रिए link करना।
Syntax
python
cursor.execute(
    "SELECT a.column, b.column FROM table_a AS a "
    "INNER JOIN table_b AS b ON a.key = b.key"
)

Basic INNER JOIN

cursor.execute("SELECT users.name, products.name FROM users INNER JOIN products ON users.fav = products.id") users और products tables की matching rows को जोड़ता है, जहाँ भी किसी user का favorite (fav) column किसी product की id से मेल खाता है -- जिन users का कोई matching product नहीं है वे result से पूरी तरह बाहर रह जाते हैं।

Note: जब आपको सिर्फ उन rows की परवाह हो जिनका दोनों related tables में genuinely कोई match है, तो default choice के रूप में INNER JOIN (अक्सर सिर्फ JOIN लिखा जाता है) इस्तेमाल करें।
Warning: INNER JOIN चुपचाप किसी भी table की उस row को exclude कर देता है जिसका दूसरी ओर कोई matching counterpart नहीं है -- अगर आपको वे unmatched rows भी चाहिए तो इसकी बजाय LEFT JOIN या RIGHT JOIN चाहिए।

उदाहरण: Basic INNER JOIN

python
from unittest.mock import MagicMock

conn = MagicMock()
cursor = conn.cursor()
cursor.execute(
    "SELECT users.name, products.name FROM users "
    "INNER JOIN products ON users.fav = products.id"  # only rows with a match in both tables are included
)
print("Only users with a matching favorite product are included")

LEFT JOIN: Unmatched Rows को शामिल करना

LEFT JOIN, left (पहले नामित) table की हर row return करता है चाहे right table में उसका कोई match हो या न हो -- जब कोई match नहीं होता, तो right table के columns बस NULL आ जाते हैं, जो उदाहरण के लिए हर customer, यहाँ तक कि जिन्होंने कभी order नहीं दिया, ढूँढने के लिए उपयोगी है।

Note: LEFT JOIN specifically तब इस्तेमाल करें जब लक्ष्य में left table की ऐसी rows ढूँढना या दिखाना शामिल हो जिनका कोई corresponding match न हो, जैसे zero orders वाले customers।
Warning: Unmatched LEFT JOIN rows में right table से आने वाले columns Python में None आते हैं -- इन results को consume करने वाले code को उस None case को explicitly handle करना चाहिए।

उदाहरण: LEFT JOIN: Including Unmatched Rows

python
from unittest.mock import MagicMock

conn = MagicMock()
cursor = conn.cursor()
cursor.execute(
    "SELECT customers.name, orders.id FROM customers "
    "LEFT JOIN orders ON customers.id = orders.customer_id"  # every customer included, even with no matching order
)
print("Customers with no orders still appear, with NULL order id")

तीन या ज़्यादा Tables Join करना

एक ही query में कई JOIN clauses को chain किया जा सकता है ताकि तीन या ज़्यादा related tables का data जोड़ा जा सके -- उदाहरण के लिए orders को customers से link करना, और अलग से हर order को उसमें मौजूद products से link करना, सब एक ही statement में।

Note: जब कोई query कई tables जोड़े, तो हर table को एक छोटे नाम (जैसे orders के लिए o, customers के लिए c) से alias दें, ताकि SQL ज़्यादा readable रहे और पूरे table names बार-बार न दोहराने पड़ें।
Warning: Chained joins अगर relationships कई levels पर one-to-many links शामिल करते हों तो surprisingly बड़ा result set दे सकते हैं -- expected row count दोबारा check करना समझदारी है।

उदाहरण: Joining Three or More Tables

python
from unittest.mock import MagicMock

conn = MagicMock()
cursor = conn.cursor()
cursor.execute(
    "SELECT c.name, o.id, p.name FROM customers c "
    "JOIN orders o ON c.id = o.customer_id "  # first join links customers to orders
    "JOIN products p ON o.product_id = p.id"  # second join links orders to products
)
print("Three tables combined with short aliases")

Ambiguous Column Errors से बचना

जब दो joined tables का column name एक जैसा हो (जैसे दोनों में एक "id" column), तो SELECT list या WHERE clause में सिर्फ उस नाम को reference करना ambiguous होता है और एक error raise करता है -- इसे table name (या alias) से qualify करना, जैसे customers.id, यह ambiguity हल कर देता है।

Note: किसी भी JOIN वाली query में column names को हमेशा उनके table (या alias) से qualify करने की आदत डालें, यहाँ तक कि उन columns के लिए भी जो अभी ambiguous नहीं हैं, consistency और future-proofing के लिए।
Warning: एक "ambiguous column" error specifically इसका मतलब है कि database यह नहीं बता सकता कि आपका मतलब joined tables में से किस से था -- इसे column को qualify करके ही हल करना होगा, किसी चीज़ का नाम बदलकर नहीं।

उदाहरण: Avoiding Ambiguous Column Errors

python
from unittest.mock import MagicMock

conn = MagicMock()
cursor = conn.cursor()
cursor.execute(
    "SELECT customers.id, orders.id FROM customers "
    "JOIN orders ON customers.id = orders.customer_id"  # table names qualify the ambiguous "id" column
)
print("Qualifying 'id' with a table name avoids ambiguity")

WHERE, ORDER BY और LIMIT के साथ Joins

एक JOIN, पहले से बताई गई हर SELECT clause के साथ naturally combine होता है -- joined result को filter करने के लिए WHERE, उसे sort करने के लिए ORDER BY, और कितनी combined rows वापस आएँ यह सीमित करने के लिए LIMIT -- और यह उसी logical order में लागू होता है: पहले join, फिर filter, फिर sort, फिर limit।

Note: Complex multi-table queries को धीरे-धीरे बनाएँ: पहले JOIN से शुरू करें और confirm करें कि वह सही दिख रहा है, फिर एक-एक करके WHERE, फिर ORDER BY, फिर LIMIT जोड़ें।
Warning: किसी joined query में WHERE conditions दोनों में से किसी भी joined table के columns पर filter कर सकती हैं -- यह भूल जाना कि filtered column किस table का है, यहाँ भी ambiguous-column error का एक आम कारण है।

उदाहरण: Joins with WHERE, ORDER BY, and LIMIT

python
from unittest.mock import MagicMock

conn = MagicMock()
cursor = conn.cursor()
cursor.execute(
    "SELECT c.name, o.total FROM customers c JOIN orders o ON c.id = o.customer_id "
    "WHERE o.total > %s ORDER BY o.total DESC LIMIT 5",  # join, then filter, then sort, then limit
    (100,),
)
print("Join, then filter, then sort, then limit")
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. #}

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.