Skip to content
Question 86 of 95

Q.Explain the usage of HAVING clause in GROUP BY command in RDBMS with the help of an example.

Puducherry TnboardCBSE Class XII Board 2023Subjective· 2mImportance★★★★★
91% · 86/95 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

The HAVING clause filters groups created by GROUP BY based on aggregate conditions, whereas WHERE filters individual rows before grouping occurs.

When you work with relational databases, you often need to summarize data — count how many customers placed orders, find the average salary by department, or total sales by region. The GROUP BY clause lets you collapse rows into groups and apply aggregate functions like COUNT, SUM, AVG, MAX, or MIN to each group. But what if you want to filter those groups themselves? That's where HAVING steps in.

The WHERE clause cannot do this job because it operates on individual rows before any grouping happens. It examines each record one by one and decides whether to include it in the result set. Once GROUP BY has done its work and created summary groups, WHERE is already finished. You need a tool that works after grouping, on the aggregated results themselves. HAVING is precisely that tool.

Think of the sequence this way: the database first applies WHERE to filter rows, then GROUP BY to organize the survivors into groups, then HAVING to filter those groups based on aggregate conditions, and finally SELECT to display the result. HAVING always appears after GROUP BY in the SQL statement and uses aggregate functions in its condition.

A Concrete Example

Suppose you manage a bookstore and have a table called Sales with columns: BookID, BookTitle, Quantity, and Price. You want to find which books have sold more than 50 copies in total.

SELECT BookTitle, SUM(Quantity) AS TotalSold
FROM Sales
GROUP BY BookTitle
HAVING SUM(Quantity) > 50;

Here's what happens step by step. The GROUP BY clause groups all sales records by BookTitle, so all rows for "The Great Gatsby" collapse into one group, all rows for "1984" into another, and so on. For each group, SUM(Quantity) calculates the total copies sold. Now HAVING examines each group's total: if it exceeds 50, that group appears in the final result; otherwise, it's excluded.

Note

You cannot write WHERE SUM(Quantity) > 50 because WHERE doesn't recognize aggregate functions — it works on raw rows before any summing occurs. The database would throw an error.

Consider another scenario: you have an Employees table with columns EmployeeID, Department, and Salary. You want departments where the average salary exceeds 60,000.

SELECT Department, AVG(Salary) AS AvgSalary
FROM Employees
GROUP BY Department
HAVING AVG(Salary) > 60000;
``` …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.