Chapter 7: SQL Joins & Subqueries for Data Science

Introduction: Data Science में SQL क्यों जरूरीData Science में 80% टाइम data extract और clean करने में जाता है। Python से पहले SQL से ही data निकालते हैं। Real Example:
customers JOIN orders JOIN products → “18-25 age group में कौन सा product सबसे ज्यादा बिक रहा है”बिना Join और Subquery के कोई insight नहीं निकलेगी।

1 Table से काम नहीं चलता –

Example:

customers table में नाम है

orders table में पैसे हैं

products table में सामान है अब सवाल: “Rohit ने कौन सा iPhone खरीदा और कितने का?”

इसका जवाब 1 table नहीं दे सकता। 3 table को जोड़ना पड़ेगा।

यही काम JOINS करते हैं।और कभी-कभी हमें “जिनका salary average से ज्यादा है” निकालना है। पहले average निकालो फिर compare करो। ये SUBQUERY है।

Data Scientist की 60% query में JOIN + SUBQUERY होता है। चलो सीखते हैं।

Part 1: SQL JOINS – Data को जोड़ना

सोचो 2 Excel शीट को VLOOKUP से जोड़ना। SQL में उसे JOIN बोलते हैं।मान लो हमारे पास 2 table हैं:

Table 1: players

player_idnameteam
1AmitT1
2RahulS8
3PriyaT1

Table 2: stats

player_idkillsmatches
15010
28010
4305

1.INNER JOIN – Common Data

सिर्फ वही player जो दोनों table में हैं।

SELECT players.name, stats.kills
FROM players
INNER JOIN stats ON players.player_id = stats.player_id;

Output: Amit, Rahul
Priya और player_id 4 नहीं आएगा क्योंकि वो सिर्फ 1 table में है।
Use Case: “जिन customers ने order किया है उनकी list”

2.LEFT JOIN – सबसे जरूरी Join

Left वाली table का सारा data + Right का common data।
Data Science में 70% यही use होता है।

SELECT players.name, stats.kills
FROM players
LEFT JOIN stats ON players.player_id = stats.player_id;

Output: Amit, Rahul, Priya
Priya के सामने kills = NULL आएगा क्योंकि उसका data stats में नहीं है।
Use Case: “सारे customers की list, चाहे order किया हो या ना किया हो”

3.RIGHT JOIN

LEFT JOIN का उल्टा। Right table का सारा data।
कम use होता है।

4. FULL OUTER JOIN

दोनों table का सारा data। Common + Unique सब।

SELECT * FROM players
FULL OUTER JOIN stats ON players.player_id = stats.player_id;

5.CROSS JOIN

हर row को हर row से जोड़ दो। Cartesian Product।
कम use होता है।

6.SELF JOIN

खुद की table को खुद से join करना।
Example: “Employee और उसका Manager एक ही table में हैं”

SELECT e.name AS Employee, m.name AS Manager
FROM employees e
JOIN employees m ON e.manager_id = m.employee_id;

JOIN का Golden Rule:
ON में हमेशा वही column डालो जो दोनों table में common है – player_id, customer_id, order_id

Part 2: SUBQUERIES – Query के अंदर Query –

Subquery = Query के अंदर Query। ब्रैकेट में लिखते हैं।1. WHERE में Subquery – सबसे ज्यादा useProblem: “Average Kills से ज्यादा kill करने वाले player बताओ”Step 1: पहले average निकालो
Step 2: फिर compare करो

SELECT name, kills
FROM stats
WHERE kills > (SELECT AVG(kills) FROM stats);

ब्रैकेट वाली query पहले चलेगी। Result = 53 आएगा। फिर बाहर वाली चलेगी।

2.FROM में Subquery – Temporary Table

SELECT team, avg_kills
FROM (
SELECT team, AVG(kills) as avg_kills
FROM players JOIN stats ON players.id = stats.player_id
GROUP BY team
) AS team_stats
WHERE avg_kills > 40;

पहले team-wise average बना, फिर उसमें से filter किया।

3.SELECT में Subquery

SELECT name,
(SELECT COUNT(*) FROM orders WHERE orders.customer_id = customers.id) AS total_orders
FROM customers;

हर customer के सामने उसके total orders आ जाएंगे।

4.IN, ANY, ALL के साथ Subquery

“जिन team में Amit है, उस team के सारे player”

SELECT name FROM players
WHERE team IN (SELECT team FROM players WHERE name = ‘Amit’);

Part 3: JOIN vs SUBQUERY – कब क्या use करें?

ये Interview में जरूर पूछते हैं।

JOINSUBQUERY
SpeedFastSlow
Readability2+ table जोड़नी हो1 table में calculation
ExampleCustomer + Order dataAverage से compare करना
ReturnMultiple Columns1 Column/1 Value

Data Science Rule:
पहले JOIN करके सारा data 1 table में ले आओ। फिर Subquery से filter करो।

Part 4: Real Data Science Examples –

Example 1: E-commerce
“हर Category की Top Selling Product”

SELECT p.category, p.name, sales.total
FROM products p
JOIN (
SELECT product_id, SUM(amount) as total
FROM orders GROUP BY product_id
) sales ON p.id = sales.product_id;

यहां JOIN + Subquery दोनों हैं।

Example 2: Gaming
“जिन Player की K/D Ratio 2 से ज्यादा है”

SELECT name FROM players
WHERE player_id IN (
SELECT player_id FROM stats WHERE kills/deaths > 2

);

Example 3: HR Analytics
“Department wise सबसे ज्यादा Salary वाला”


SELECT e.name, e.dept, e.salary
FROM employees e
JOIN (
SELECT dept, MAX(salary) as max_sal FROM employees GROUP BY dept
) m ON e.dept = m.dept AND e.salary = m.max_sal;

Part 5: 15 Practice Questions –

1.Customers जिन्होंने कभी order नहीं किया – LEFT JOIN + IS NULL

2.दूसरा सबसे ज्यादा Salary वाला

3.हर Product की Total Sale

4.जिन Student के Marks Class Average से ज्यादा हैं

5.Duplicate Records हटाओ

6.Employees और उनके Manager का नाम

7.Month wise Revenue

8.3 Table Join: Customers + Orders + Products

9.EXISTS vs IN का फर्क

10.Top 3 Products per Category

11.Running Total निकालो – WINDOW FUNCTION

12.Nth Highest Salary

13.Customers जिन्होंने सभी Product खरीदे

14.Delete Duplicate Rows

15.Correlated Subquery क्या है?

Conclusion + Key Takeaways –

क्या सीखा?

1.JOIN = कई Table को जोड़ना। LEFT JOIN सबसे जरूरी।

2.SUBQUERY = Query के अंदर Query। Calculation के लिए।

3.Combo = Real Data Science = JOIN + GROUP BY + SUBQUERY

Pro Tip: Query लिखते समय पहले English में सोचो। “मुझे Customer का नाम और उसका Total Order चाहिए” → फिर SQL में convert करो।

Practice: Mode Analytics SQL Tutorial और StrataScratch पर रोज 2 Question करो।

अगला Chapter: Chapter 8 – “Window Functions & CTEs in SQL”
जहां हम RANK(), LAG(), LEAD() सीखेंगे।

Leave a Comment

Your email address will not be published. Required fields are marked *