Exam-ready answer with definitions, differences, examples, outputs, and quick revision points.
SQL এ where এবং having এর পার্থক্য উদাহরণ সহ বর্ণনা কর। Union এবং union all এর পার্থক্য উদাহরণ সহযোগে গণনা কর।
SQL-এ data filter এবং combine করার জন্য বিভিন্ন clause এবং operator ব্যবহার করা হয়। WHERE এবং HAVING clause data filter করতে ব্যবহৃত হয়, কিন্তু এদের কাজ করার সময় এবং উদ্দেশ্য আলাদা।
অন্যদিকে UNION এবং UNION ALL দুটি SELECT query-এর result একসঙ্গে combine করতে ব্যবহৃত হয়। কিন্তু UNION duplicate row remove করে, আর UNION ALL duplicate row সহ সব row দেখায়।
সহজ ভাষায়: WHERE row filter করে, HAVING group filter করে। UNION duplicate বাদ দেয়, UNION ALL duplicate সহ সব data দেখায়।
WHERE clause SQL-এ table থেকে নির্দিষ্ট condition অনুযায়ী rows filter করার জন্য ব্যবহৃত হয়। এটি grouping বা aggregation হওয়ার আগে কাজ করে।
Definition: WHERE clause is used to filter individual rows before grouping or aggregation.
SELECT column1, column2
FROM table_name
WHERE condition;
HAVING clause SQL-এ grouped data বা aggregate result filter করার জন্য ব্যবহৃত হয়। এটি সাধারণত GROUP BY clause-এর সঙ্গে ব্যবহৃত হয়।
WHERE aggregate function-এর উপর condition apply করতে পারে না, কিন্তু HAVING aggregate function-এর result-এর উপর condition apply করতে পারে।
Definition: HAVING clause is used to filter groups after GROUP BY and aggregation.
SELECT column_name, aggregate_function(column_name)
FROM table_name
GROUP BY column_name
HAVING aggregate_condition;
নিচের Student table ব্যবহার করে WHERE এবং HAVING বোঝানো হলো:
| Roll_No | Name | Department | Marks |
|---|---|---|---|
| 101 | Rahul | Computer Science | 85 |
| 102 | Riya | Commerce | 72 |
| 103 | Karim | Computer Science | 90 |
| 104 | Anita | Arts | 68 |
| 105 | Suman | Commerce | 80 |
যেসব student-এর marks 75-এর বেশি, তাদের record বের করতে:
SELECT Roll_No, Name, Department, Marks
FROM Student
WHERE Marks > 75;
| Roll_No | Name | Department | Marks |
|---|---|---|---|
| 101 | Rahul | Computer Science | 85 |
| 103 | Karim | Computer Science | 90 |
| 105 | Suman | Commerce | 80 |
এখানে WHERE clause প্রতিটি row check করেছে এবং Marks 75-এর বেশি এমন rows result-এ দেখিয়েছে।
যেসব Department-এর average marks 75-এর বেশি, সেই Department গুলি বের করতে:
SELECT Department, AVG(Marks) AS Average_Marks
FROM Student
GROUP BY Department
HAVING AVG(Marks) > 75;
| Department | Average_Marks |
|---|---|
| Computer Science | 87.5 |
| Commerce | 76 |
এখানে GROUP BY Department অনুযায়ী students-দের group করেছে এবং HAVING average marks 75-এর বেশি এমন groups filter করেছে।
| বিষয় | WHERE | HAVING |
|---|---|---|
| কাজ | Individual rows filter করে। | Groups বা aggregate result filter করে। |
| কখন কাজ করে | GROUP BY-এর আগে কাজ করে। | GROUP BY-এর পরে কাজ করে। |
| Aggregate Function | Aggregate function-এর সঙ্গে সাধারণত ব্যবহার করা যায় না। | Aggregate function-এর সঙ্গে ব্যবহার করা যায়। |
| GROUP BY দরকার? | GROUP BY ছাড়াও ব্যবহার করা যায়। | সাধারণত GROUP BY-এর সঙ্গে ব্যবহার করা হয়। |
| Example | WHERE Marks > 75 | HAVING AVG(Marks) > 75 |
| Filter Level | Row level filtering | Group level filtering |
কখনও কখনও WHERE এবং HAVING একই query-তে ব্যবহার করা যায়। WHERE আগে rows filter করে, তারপর GROUP BY group তৈরি করে, তারপর HAVING group filter করে।
SELECT Department, AVG(Marks) AS Average_Marks
FROM Student
WHERE Marks >= 70
GROUP BY Department
HAVING AVG(Marks) > 75;
এখানে প্রথমে Marks 70 বা তার বেশি এমন rows নেওয়া হয়েছে। তারপর Department অনুযায়ী group করা হয়েছে। শেষে average marks 75-এর বেশি এমন groups দেখানো হয়েছে।
UNION operator দুই বা ততোধিক SELECT statement-এর result একত্র করে এবং duplicate rows remove করে। অর্থাৎ result-এ শুধুমাত্র unique rows দেখানো হয়।
Definition: UNION combines result sets of two or more SELECT statements and removes duplicate rows.
SELECT column1, column2
FROM table1
UNION
SELECT column1, column2
FROM table2;
UNION ALL operator দুই বা ততোধিক SELECT statement-এর result একত্র করে কিন্তু duplicate rows remove করে না। অর্থাৎ সব rows, duplicate সহ, result-এ দেখায়।
Definition: UNION ALL combines result sets of two or more SELECT statements and keeps duplicate rows.
SELECT column1, column2
FROM table1
UNION ALL
SELECT column1, column2
FROM table2;
| Student_Name |
|---|
| Rahul |
| Riya |
| Karim |
| Student_Name |
|---|
| Riya |
| Karim |
| Anita |
Course_A এবং Course_B table-এর students একত্রে বের করতে, কিন্তু duplicate বাদ দিয়ে:
SELECT Student_Name
FROM Course_A
UNION
SELECT Student_Name
FROM Course_B;
| Student_Name |
|---|
| Rahul |
| Riya |
| Karim |
| Anita |
এখানে Riya এবং Karim দুই table-এ ছিল, কিন্তু UNION duplicate remove করে একবার করে দেখিয়েছে।
গণনা: Course_A-তে 3 rows + Course_B-তে 3 rows = 6 rows। Duplicate Riya এবং Karim বাদ দিলে final UNION result = 4 rows।
Course_A এবং Course_B table-এর students একত্রে বের করতে, duplicate সহ:
SELECT Student_Name
FROM Course_A
UNION ALL
SELECT Student_Name
FROM Course_B;
| Student_Name |
|---|
| Rahul |
| Riya |
| Karim |
| Riya |
| Karim |
| Anita |
এখানে UNION ALL duplicate remove করেনি। তাই Riya এবং Karim দুইবার করে এসেছে।
গণনা: Course_A-তে 3 rows + Course_B-তে 3 rows = 6 rows। UNION ALL সব rows রাখে, তাই final result = 6 rows।
| বিষয় | UNION | UNION ALL |
|---|---|---|
| Duplicate Rows | Duplicate rows remove করে। | Duplicate rows রাখে। |
| Result | Only unique rows দেখায়। | All rows দেখায়। |
| Performance | Duplicate remove করার কারণে তুলনামূলক slow হতে পারে। | Duplicate check না করার কারণে তুলনামূলক faster। |
| Use Case | যখন unique result দরকার। | যখন duplicate সহ সব result দরকার। |
| Example Row Count | 3 + 3 rows থেকে duplicate বাদ দিয়ে 4 rows। | 3 + 3 rows = 6 rows। |
SELECT Student_Name FROM Course_A
UNION
SELECT Student_Name FROM Course_B
ORDER BY Student_Name;
| Topic | Main Point | Example |
|---|---|---|
| WHERE | Rows filter করে | WHERE Marks > 75 |
| HAVING | Groups filter করে | HAVING AVG(Marks) > 75 |
| UNION | Duplicate বাদ দিয়ে result combine করে | Result = 4 rows |
| UNION ALL | Duplicate সহ result combine করে | Result = 6 rows |
সুতরাং, WHERE এবং HAVING দুটিই filtering-এর জন্য ব্যবহৃত হলেও WHERE individual rows filter করে এবং HAVING grouped বা aggregated result filter করে। WHERE GROUP BY-এর আগে কাজ করে, কিন্তু HAVING GROUP BY-এর পরে কাজ করে।
একইভাবে UNION এবং UNION ALL দুটিই multiple SELECT query-এর result combine করে। কিন্তু UNION duplicate rows remove করে unique result দেয়, আর UNION ALL duplicate সহ সব rows দেখায়। তাই requirement অনুযায়ী UNION বা UNION ALL ব্যবহার করা উচিত।