গল্প — University Students & Departments
University চায়: সব student দেখাও এবং তাদের department দেখাও।
Students
| student_id | student_name | department_id |
|---|---|---|
| 101 | Rahim | 1 |
| 102 | Karim | 2 |
| 103 | Hasan | 3 |
| 104 | Nila | 99 |
| 105 | Sumi | NULL |
Departments
| department_id | department_name |
|---|---|
| 1 | CSE |
| 2 | EEE |
| 3 | BBA |
Hasan-এর department আছে? → হ্যাঁ (BBA)
Nila-এর 99 departments-এ আছে? → না
Sumi-এর department_id আছে? → না (NULL)
Nila ও Sumi বাদ দেব? → না — requirement বলে সব student।
Interactive Discovery
সব data রাখতে হবে → Students LEFT SIDE।
Animated Frames — Row by Row
| student_id | student_name | department_name |
|---|---|---|
| 101 | Rahim | CSE |
| 102 | Karim | EEE |
| 103 | Hasan | BBA |
| 104 | Nila | NULL |
| 105 | Sumi | NULL |
LEFT JOIN Concept
LEFT JOIN left table-এর সব row রাখে। Right থেকে match থাকলে data; না থাকলে NULL।
প্রথম LEFT JOIN Query
INNER JOIN vs LEFT JOIN Animation
INNER JOIN
LEFT JOIN
| Student | INNER | LEFT |
|---|---|---|
| Rahim | CSE | CSE |
| Karim | EEE | EEE |
| Hasan | BBA | BBA |
| Nila | ❌ | NULL |
| Sumi | ❌ | NULL |
Venn (Conceptual)
LEFT JOIN + WHERE vs ON
না — WHERE right-side NULL rows কেড়ে নিতে পারে (Nila/Sumi বাদ)।
Unmatched রাখতে চাইলে filter ON-এ
Finding Unmatched Records
যেসব student-এর valid department নেই:
E-commerce: All Customers + Order Count
| customers | orders |
|---|---|
| 1 Rahim, 2 Karim, 3 Hasan, 4 Nila, 5 Sumi | 101→1, 102→1, 103→2, 104→4 |
COUNT(o.order_id) — COUNT(*) unmatched-এও ১ গুনতে পারে (NULL row)।LEFT JOIN + SUM + COALESCE
ORDER BY + LIMIT
৩ টেবিল LEFT JOIN
Industry Use Cases
- Customers with no orders
- Employees without managers
- Students without departments
- Patients without appointments
- Accounts without transactions
- Products never sold
- Data quality / orphan detection
Pattern: Find Missing
FROM A
LEFT JOIN B ON A.key = B.key
WHERE B.key IS NULL;What Will Happen? Game
আগে predict করো, তারপর কার্ড খোলো।
Manual Row Matching (৫ datasets)
INNER vs LEFT Comparison
| Feature | INNER | LEFT |
|---|---|---|
| Left rows | Matching only | All |
| Unmatched left | Removed | Kept |
| Right unmatched | Removed | NULL |
| Find missing links | Hard | Excellent |
| Customers w/o orders | Difficult | Excellent |
LEFT vs RIGHT
Common Mistakes (১৫)
- LEFT = শুধু match ভাবা → ভুল
- ON ভুলে যাওয়া
- Wrong join columns
- WHERE দিয়ে unintentionally unmatched কাটা
- INNER-এর সাথে গুলিয়ে ফেলা
COUNT(*)যেখানেCOUNT(right.id)দরকার- NULL ignore
- One-to-many না বোঝা
- Unexpected duplicates
- Relationship না বুঝে join
- Ambiguous columns
- SELECT * সবসময়
- Right table WHERE filter ভুলে
- GROUP BY columns ভুলে
- Unmatched check না করা
JOIN Decision Tree
Execution Flow
Workbench Lab (১৫ steps)
- Basic LEFT JOIN
- Specific columns
- Aliases
- WHERE on right
- Filter in ON
- Unmatched
WHERE o.customer_id IS NULL - GROUP BY
- COUNT(o.order_id)
- SUM + COALESCE
- ORDER BY
- LIMIT
- Add products + 3-table LEFT
- Products never sold pattern
- Customers without orders
- Mini DQ report
Mini Project — Sales Dashboard Dataset
- All customers + order count
- All customers + total purchase
- Never ordered customers
- Products + qty sold
- Products never sold
- Categories + product counts
- Customers + latest order date
- Customers with no orders (confirm)
- Top customers
- Products with zero sales
ON vs WHERE Deep Dive
Query A — WHERE
Hasan/Sumi (no orders) বাদ যেতে পারে।
Query B — AND in ON
সব customer থাকতে পারে; শুধু qualifying orders attach হয়।
Performance Basics
- Index join keys (PK/FK)
- Select needed columns
- Avoid useless joins
- Watch one-to-many row growth
- Millions of rows → JOIN costly
Classroom Challenges (২০+)
MCQ (২৫+)
Viva
Interview Top 30
Homework
প্রশ্ন (১৫):
- All customers with orders
- Customers without orders
- Products with sold qty
- Products never sold
- Categories with product counts
- Customers total purchase
- Replace NULL purchase with 0
- Customers with no purchase
- LEFT + GROUP BY
- LEFT + HAVING
- LEFT + ORDER BY
- LEFT + LIMIT
- 3-table LEFT
- Explain ON vs WHERE
- Explain LEFT vs INNER
Cheat Sheet + Conceptual Playground
LEFT JOIN
LEFT TABLE → KEEP ALL → MATCH RIGHT
Match? YES→DATA NO→NULL
SELECT columns
FROM left_table l
LEFT JOIN right_table r
ON l.key = r.key;
Find unmatched:
FROM A LEFT JOIN B ON A.id=B.id
WHERE B.id IS NULL;
Aggregate:
SELECT a.name, COUNT(b.id)
FROM A a LEFT JOIN B b ON a.id=b.a_id
GROUP BY a.id, a.name;