Ravindra BagaleCourses & study guides Track your progress

15. INNER JOIN आणि LEFT JOIN (आधी लागणारे दोन joins)

15.3 JOIN … ON बरोबर कसं लिहायचं (Aliases, filters, ON शिवाय कधीच नाही)

का (Why). Join लिहिण्यातल्या छोट्या चुकांमुळे खूप जास्त rows येऊ शकतात, किंवा LEFT JOIN ने ठेवायचे rows गुपचूप जाऊ शकतात.

काय (What). Alias म्हणजे table चं छोटं नाव, जसं shipments साठी s, आणि त्यामुळे कुठला column कुठल्या table चा आहे ते स्पष्ट होतं. ON condition नसेल तर SQLite प्रत्येक row ची प्रत्येक row शी जोडी लावतो: 10 shipments आणि 5 hubs मिळून 50 rows येतात. LEFT JOIN मध्ये उजवीकडच्या table वरची condition ON मध्ये लिहिली तर ती फक्त काय जुळतं ते ठरवते, म्हणून डावीकडचे सगळे rows राहतात. तीच condition WHERE मध्ये join नंतर चालते आणि ज्या डावीकडच्या rows चे उजवीकडचे columns NULL आहेत ते काढून टाकते. दोन्ही tables मध्ये एकाच नावाचा column असेल तर SELECT मध्ये प्रत्येकाला वेगळं alias द्या.

कसं (How). ON शिवाय काय होतं ते बघा, मग तोच date filter ON मध्ये आणि WHERE मध्ये लिहून results ची तुलना करा.

पायऱ्या

  1. पहिलं उदाहरण run करा आणि साधारण 10 ऐवजी 50 rows बघा.
  2. Date filter ON मध्ये ठेवून दुसरं उदाहरण run करा.
  3. तोच filter WHERE मध्ये ठेवून तिसरं उदाहरण run करा.
  4. तुलना करा: ON वाल्या version मध्ये 8 rows मध्ये सगळे 6 customers राहतात, WHERE वाल्यात 6 rows मध्ये फक्त 4 customers दिसतात.

उदाहरणे

उदाहरण 1 · dataset: swiftship_logistics

SELECT COUNT(*) AS rows_without_on
FROM shipments, hubs;

Output

rows_without_on
50

Meaning. 50 rows: प्रत्येक shipment ची प्रत्येक hub शी जोडी लागली. या जोड्यांना काहीही अर्थ नाही, म्हणून join condition नेहमी लिहा.

उदाहरण 2 · dataset: swiftship_logistics

SELECT c.customer_name, s.shipment_id, s.shipped_on
FROM customers AS c
LEFT JOIN shipments AS s
  ON s.customer_id = c.customer_id
 AND s.shipped_on >= '2026-10-05'
ORDER BY c.customer_id, s.shipment_id;

Output

customer_nameshipment_idshipped_on
Kavya TradersNULLNULL
Desai Pharma10082026-10-08
Al Noor Foods10052026-10-06
Thames Books10062026-10-05
Thames Books10092026-10-09
Lotus Crafts10072026-10-07
Lotus Crafts10102026-10-10
Marina SpicesNULLNULL

Meaning. 8 rows आणि सगळे 6 customers. Kavya Traders ची 5 October किंवा नंतर एकही shipment नाही, आणि Marina Spices ची कधीच नाही, म्हणून दोघांचे shipment columns NULL दिसतात.

उदाहरण 3 · dataset: swiftship_logistics

SELECT c.customer_name, s.shipment_id, s.shipped_on
FROM customers AS c
LEFT JOIN shipments AS s
  ON s.customer_id = c.customer_id
WHERE s.shipped_on >= '2026-10-05'
ORDER BY c.customer_id, s.shipment_id;

Output

customer_nameshipment_idshipped_on
Desai Pharma10082026-10-08
Al Noor Foods10052026-10-06
Thames Books10062026-10-05
Thames Books10092026-10-09
Lotus Crafts10072026-10-07
Lotus Crafts10102026-10-10

Meaning. फक्त 6 rows. Kavya Traders आणि Marina Spices गायब होतात, कारण त्यांच्या NULL dates WHERE test मध्ये fail होतात. हा LEFT JOIN आता INNER JOIN सारखा वागतो.

Ravindra Bagale's Tip

With a LEFT JOIN, put conditions on the right-hand table in ON when you want to keep every left row. Use WHERE only when you really want to remove rows.