গল্প — Departments & Employees
Manager: সব department দেখাও, আর যেখানে employee আছে তাদের info দেখাও।
| employee_id | name | department_id |
|---|---|---|
| 101 | Rahim | 1 |
| 102 | Karim | 2 |
| 103 | Hasan | 1 |
| department_id | department_name |
|---|---|
| 1 | IT |
| 2 | HR |
| 3 | Finance |
| 4 | Marketing |
Finance বাদ? Marketing বাদ? → না — সব department চাই।
Interactive Discovery
Departments → RIGHT।
Animated Frames
| employee_name | department_name |
|---|---|
| Rahim | IT |
| Hasan | IT |
| Karim | HR |
| NULL | Finance |
| NULL | Marketing |
RIGHT JOIN কী?
RIGHT JOIN right table-এর সব row রাখে; left থেকে match হলে data, না হলে left NULL।
প্রথম RIGHT JOIN Query
Expected Output
| employee_id | employee_name | department_id | department_name |
|---|---|---|---|
| 101 | Rahim | 1 | IT |
| 103 | Hasan | 1 | IT |
| 102 | Karim | 2 | HR |
| NULL | NULL | 3 | Finance |
| NULL | NULL | 4 | Marketing |
IT/HR = match · Finance/Marketing = employee columns NULL।
LEFT vs RIGHT Animation
LEFT JOIN (emp LEFT dept)
RIGHT JOIN (emp RIGHT dept)
| Question | LEFT | RIGHT |
|---|---|---|
| Preserved table | Left | Right |
| All left rows? | Yes | No |
| All right rows? | No | Yes |
| Missing side | NULL | NULL |
Memory Boxes
RIGHT JOIN + WHERE
Finance থাকে কারণ right preserve; employee নেই → NULL। WHERE join পর filter করে।
Unmatched — Departments without employees
Equivalent LEFT JOIN
RIGHT
LEFT (same idea)
Flip the Tables (৫+)
RIGHT JOIN + GROUP BY
COUNT(e.employee_id) — না COUNT(*) (unmatched-এও ১ হতে পারে)।SUM / AVG + COALESCE
HAVING
E-commerce — Products never sold
Preferred equivalent
Hospital — Doctors
সব doctor + appointment count: appointments RIGHT JOIN doctors বা doctors LEFT JOIN appointments।
Bank — Branches
সব branch + account count — RIGHT preserve branches (Sylhet = 0)। Equivalent: branches LEFT JOIN accounts।
Multi-table RIGHT
ON vs WHERE
Query 1 — WHERE Finance
Query 2 — AND in ON
Filter placement result বদলাতে পারে — row-by-row দেখে বোঝাও।
Common Mistakes (১৫)
- RIGHT = keep left ভাবা
- কোন table right ভুলে যাওয়া
- Wrong join key
- ON ভুলে
- LEFT/RIGHT গুলিয়ে
- Unmatched disappear আশা
- NULL না বোঝা
- WHERE দিয়ে unintentionally কাটা
- COUNT(*) ভুল জায়গায়
- Duplicate rows surprise
- One-to-many ignore
- Unrelated columns join
- SELECT *
- GROUP BY columns ভুলে
- যখন LEFT clearer তখনও RIGHT জোর করা
Conceptual Playground
Prediction Game (২০+)
Decision Tree
INNER vs LEFT vs RIGHT
| Requirement | JOIN |
|---|---|
| Only matching | INNER |
| Keep every left | LEFT |
| Keep every right | RIGHT |
| Missing right links from left | LEFT + IS NULL |
| Missing left links from right | RIGHT + IS NULL (or flip LEFT) |
Why Pros Often Prefer LEFT
RIGHT valid — কিন্তু যে table রাখতে চাই তাকে LEFT করে LEFT JOIN লিখলে পড়া সহজ।
Workbench Lab
- Basic RIGHT JOIN
- Specific columns
- Aliases
- WHERE
- NULL unmatched
- Find empty depts
- GROUP BY
- COUNT
- SUM
- AVG
- HAVING
- ORDER BY
- LIMIT
- 3-table note
- Convert to LEFT JOIN
Mini Project
departments / employees / projects / employee_projects:
- All depts + emp count
- Depts with zero emp
- Avg salary
- Highest salary
- Projects + assigned emp count
- Projects with no emp
- Depts > 5 emp
- Depts salary threshold
- Order by emp count
- Top 5 depts
RIGHT + LEFT equivalent দুটোই লেখো।
Performance Basics
- Index join keys / PK / FK
- Select needed columns
- Avoid useless joins
- One-to-many row growth
- Large tables costly
Challenges (২০)
MCQ (২৫+)
Viva
Interview Top 30
Homework
প্রশ্ন (১৫):
- All departments + employees
- Departments without employees
- All products + sales count
- Products never sold
- All doctors + appointments
- Doctors without appointments
- All branches + account count
- Branches with zero accounts
- RIGHT + GROUP BY
- RIGHT + HAVING
- RIGHT + ORDER BY
- RIGHT + LIMIT
- RIGHT + NULL pattern
- Convert RIGHT to LEFT
- When replace RIGHT with LEFT?
Cheat Sheet
RIGHT JOIN → RIGHT = ALL → LEFT = MATCH → no match → LEFT NULL
SELECT columns
FROM left_table l
RIGHT JOIN right_table r
ON l.key = r.key;
Unmatched left side:
FROM A RIGHT JOIN B ON A.id=B.id
WHERE A.id IS NULL;
Equivalent LEFT:
FROM B LEFT JOIN A ON B.id=A.id;
Memory: RIGHT JOIN = Right VIP all stay.
INNER = MATCH · LEFT = keep LEFT · RIGHT = keep RIGHT