✏️ Explanatory Question

SQL এ where এবং having এর পার্থক্য উদাহরণ সহ বর্ণনা কর। Union এবং union all এর পার্থক্য উদাহরণ সহযোগে গণনা কর।

👁 3 Views
📘 Detailed Answer
🟢 Easy
💡

Answer with Explanation

Exam-ready answer with definitions, differences, examples, outputs, and quick revision points.

Question 16

প্রশ্ন

SQL এ where এবং having এর পার্থক্য উদাহরণ সহ বর্ণনা কর। Union এবং union all এর পার্থক্য উদাহরণ সহযোগে গণনা কর।

Answer: WHERE vs HAVING and UNION vs 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 দেখায়।

Part 1: WHERE Clause কী?

WHERE clause SQL-এ table থেকে নির্দিষ্ট condition অনুযায়ী rows filter করার জন্য ব্যবহৃত হয়। এটি grouping বা aggregation হওয়ার আগে কাজ করে।

Definition: WHERE clause is used to filter individual rows before grouping or aggregation.

Syntax


SELECT column1, column2
FROM table_name
WHERE condition;
            

HAVING Clause কী?

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.

Syntax


SELECT column_name, aggregate_function(column_name)
FROM table_name
GROUP BY column_name
HAVING aggregate_condition;
            

Sample Table: Student

নিচের 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

WHERE Clause-এর উদাহরণ

যেসব student-এর marks 75-এর বেশি, তাদের record বের করতে:


SELECT Roll_No, Name, Department, Marks
FROM Student
WHERE Marks > 75;
            

Output

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-এ দেখিয়েছে।

HAVING Clause-এর উদাহরণ

যেসব Department-এর average marks 75-এর বেশি, সেই Department গুলি বের করতে:


SELECT Department, AVG(Marks) AS Average_Marks
FROM Student
GROUP BY Department
HAVING AVG(Marks) > 75;
            

Output

Department Average_Marks
Computer Science 87.5
Commerce 76

এখানে GROUP BY Department অনুযায়ী students-দের group করেছে এবং HAVING average marks 75-এর বেশি এমন groups filter করেছে।

WHERE এবং HAVING-এর পার্থক্য

বিষয় 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 একসঙ্গে ব্যবহার

কখনও কখনও 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 দেখানো হয়েছে।

Part 2: UNION কী?

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.

Syntax


SELECT column1, column2
FROM table1
UNION
SELECT column1, column2
FROM table2;
            

UNION ALL কী?

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.

Syntax


SELECT column1, column2
FROM table1
UNION ALL
SELECT column1, column2
FROM table2;
            

Sample Tables for UNION and UNION ALL

Table 1: Course_A

Student_Name
Rahul
Riya
Karim

Table 2: Course_B

Student_Name
Riya
Karim
Anita

UNION Example

Course_A এবং Course_B table-এর students একত্রে বের করতে, কিন্তু duplicate বাদ দিয়ে:


SELECT Student_Name
FROM Course_A
UNION
SELECT Student_Name
FROM Course_B;
            

Output

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।

UNION ALL Example

Course_A এবং Course_B table-এর students একত্রে বের করতে, duplicate সহ:


SELECT Student_Name
FROM Course_A
UNION ALL
SELECT Student_Name
FROM Course_B;
            

Output

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-এর পার্থক্য

বিষয় 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।

UNION / UNION ALL ব্যবহারের নিয়ম

  • দুটি SELECT query-তে column সংখ্যা সমান হতে হবে।
  • Corresponding columns-এর data type compatible হতে হবে।
  • Column order একই ধরনের হওয়া উচিত।
  • ORDER BY ব্যবহার করতে হলে সাধারণত final result-এর শেষে ব্যবহার করা হয়।

SELECT Student_Name FROM Course_A
UNION
SELECT Student_Name FROM Course_B
ORDER BY Student_Name;
            

Combined Summary

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 clause row-level filtering-এর জন্য গুরুত্বপূর্ণ।
  • HAVING clause aggregate result filter করার জন্য গুরুত্বপূর্ণ।
  • UNION multiple query result combine করে unique output দেয়।
  • UNION ALL duplicate সহ data analysis করতে সাহায্য করে।
  • Reporting, filtering এবং data merging-এর ক্ষেত্রে এগুলি খুব useful।

Exam Writing Tips

  • WHERE এবং HAVING-এর definition আলাদা করে লিখবে।
  • WHERE rows filter করে এবং HAVING groups filter করে — এই line অবশ্যই লিখবে।
  • WHERE এবং HAVING-এর example query ও output দেবে।
  • UNION এবং UNION ALL-এর difference table দেবে।
  • UNION-এর ক্ষেত্রে duplicate remove হয় এবং UNION ALL-এ duplicate থাকে — এই point লিখবে।
  • গণনা দেখালে answer বেশি scoring হবে।

উপসংহার

সুতরাং, 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 ব্যবহার করা উচিত।

Quick Revision Points

  • WHERE row filter করে।
  • HAVING group filter করে।
  • WHERE GROUP BY-এর আগে কাজ করে।
  • HAVING GROUP BY-এর পরে কাজ করে।
  • UNION duplicate remove করে।
  • UNION ALL duplicate remove করে না।
  • UNION result ছোট হতে পারে।
  • UNION ALL result বড় হতে পারে কারণ duplicate থাকে।