কেন JOIN দরকার?
Employee information এক table-এ, department অন্য table-এ। নামের সাথে department নাম একসাথে কীভাবে দেখাব?
employees
| employee_id | name | department_id |
|---|---|---|
| 101 | Rahim | 1 |
| 102 | Karim | 2 |
| 103 | Hasan | 1 |
| 104 | Nila | 3 |
departments
| department_id | department_name |
|---|---|
| 1 | IT |
| 2 | HR |
| 3 | Finance |
Employee table-এ department নাম নেই; department table-এ employee নাম নেই। কীভাবে একসাথে?
Bad Design — এক বিশাল table
| emp_id | name | dept_id | dept_name | manager |
|---|---|---|---|---|
| 101 | Rahim | 1 | IT | Mr. A |
| 102 | Karim | 2 | HR | Mr. B |
| 103 | Hasan | 1 | IT | Mr. A |
একই IT information বারবার লিখতে হচ্ছে — update কঠিন, ভুল সহজ।
Real-Life Analogies
- School: Student ID দিয়ে student + department connect
- Hospital: doctor_id দিয়ে patient + doctor
- E-commerce: customer_id দিয়ে orders + customers
- Bank: branch_id দিয়ে accounts + branches
INNER JOIN কী?
প্রথম INNER JOIN Query
Expected Output
| employee_id | name | department_id | department_name |
|---|---|---|---|
| 101 | Rahim | 1 | IT |
| 102 | Karim | 2 | HR |
| 103 | Hasan | 1 | IT |
| 104 | Nila | 3 | Finance |
Step-by-Step Visual
Match না থাকলে কী হয়?
Hasan-এর department_id = 99, কিন্তু departments-এ ৯৯ নেই।
INNER JOIN vs WHERE
Selecting Specific Columns
Professional SQL-এ দরকারি column নির্বাচন ভালো — SELECT * সবসময় নয়।
Table Aliases
INNER JOIN + WHERE Filters
শুধু IT
IT এবং HR
IT + salary > 60000
INNER JOIN + ORDER BY
INNER JOIN + GROUP BY
প্রতিটি department-এ কতজন employee?
INNER JOIN + Aggregates
INNER JOIN + HAVING
যেসব department-এ ৫ জনের বেশি employee:
তিন টেবিল INNER JOIN
| customers | orders | products |
|---|---|---|
| 1 Rahim | 101 → cust 1, prod 501 | 501 Laptop 80000 |
| 2 Karim | 102 → cust 2, prod 502 | 502 Mobile 30000 |
E-commerce Schema
Lab / project tables:
- customers — customer_id, customer_name, city, country
- products — product_id, product_name, category_id, price
- categories — category_id, category_name
- orders — order_id, customer_id, order_date, payment_method
- order_items — order_item_id, order_id, product_id, quantity, unit_price
Sample scale: ~১৫ customers, ১০ products, ৫ categories, ২০ orders, ৩০ order items (Workbench-এ INSERT)।
Business Questions (১০টি)
- Customer + order:
customers c INNER JOIN orders o ON c.customer_id=o.customer_id - Order + product: via
order_items - Customer কোন product কিনেছে: ৩-table JOIN
- Category বিক্রি: JOIN + SUM(quantity) GROUP BY category
- Customer total purchase: SUM(qty*unit_price) GROUP BY customer
- City-wise orders: JOIN + GROUP BY city
- Category AVG price: products JOIN categories + AVG
- Purchase > 50000: GROUP BY + HAVING SUM(...) > 50000
- Category sold qty > 10: HAVING SUM(quantity) > 10
- Top 10 customers: ORDER BY total DESC LIMIT 10
INNER vs অন্য JOINs (সংক্ষেপ)
আজ গভীর LEFT/RIGHT নয় — শুধু ধারণা।
INNER JOIN vs LEFT JOIN
Hasan department_id=99 (match নেই):
INNER JOIN
LEFT JOIN (preview)
PK → FK → JOIN
Common Beginner Mistakes (১৫)
- ON ভুলে যাওয়া
- Wrong columns join
- employee_id দিয়ে department join করা
- Ambiguous
id— alias লাগে - সবসময় SELECT *
- Alias ভুলে যাওয়া
- Wrong ON condition
- INNER-এ unmatched আশা করা
- LEFT-এর সাথে গুলিয়ে ফেলা
- Unexpected duplicates (one-to-many)
- Relationship না বুঝে join
- WHERE ভুল table-এ
- JOIN পর GROUP BY ভুল
- Row multiplication বুঝতে না পারা
- NULL behavior ignore
One-to-Many Duplicates
Execution Flow (simplified)
MySQL Workbench Lab
Steps: view → first JOIN → columns → aliases → WHERE → ORDER → GROUP → aggregates → HAVING → 3-table → mini report
Interactive Activities (২০+)
কার্ডে ক্লিক করে উত্তর দেখো।
Mini Project — Sales Analytics
customers / products / categories / orders / order_items তৈরি করে:
- Customer + Order report
- Customer + Product report
- Product + Category
- Total sales by customer
- Total sales by category
- Orders count by customer
- Avg order value by customer
- Top 10 customers
- Categories above sales threshold
- Products quantity sold above threshold
Industry Use Cases
- Analyst: customer + sales
- Data Scientist: multi-table features
- Data Engineer: ETL joins
- BI: dashboard datasets
- Finance / HR / E-com / Healthcare: related entities
Performance Basics
- Index join keys (PK/FK)
- Select needed columns
- Avoid unnecessary joins
- Understand one-to-many row growth
- Large tables → JOIN cost matters
Optimizer গভীর নয় — আজ clear & correct JOIN।
Cheat Sheet
SELECT columns
FROM table1 t1
INNER JOIN table2 t2
ON t1.key = t2.key;
INNER JOIN → Match → Keep matching → Ignore unmatched
SELECT e.employee_name, d.department_name
FROM employees e
INNER JOIN departments d
ON e.department_id = d.department_id;Comparison Tables
| Topic | Simple takeaway |
|---|---|
| INNER vs LEFT | match only vs keep left unmatched |
| JOIN vs WHERE | relate vs filter |
| PK vs FK | identity vs reference |
| INNER vs UNION | relate columns vs stack rows |
| INNER vs CROSS | match condition vs every×every |
| ON vs WHERE | how related vs which rows keep after |
| SELECT * vs columns | all vs intentional |
MCQ (২৫+)
Viva
Interview Top 30
Homework
প্রশ্ন (১৫):
- Customer + Order JOIN
- Product + Category JOIN
- Order + Customer JOIN
- Three-table INNER JOIN
- JOIN + WHERE
- JOIN + IN
- JOIN + ORDER BY
- JOIN + GROUP BY
- JOIN + HAVING
- JOIN + COUNT
- JOIN + SUM
- JOIN + AVG
- Top customers
- Top categories
- Multi-JOIN business report