KwickAcademy Databases and SQL · 6 min · free
GROUP BY and HAVING
GROUP BY puts rows with the same value together so an aggregate gives one answer per group. HAVING then filters those groups. WHERE filters rows before grouping; HAVING filters groups after.
Follows the syllabus of: CBSE Class 12 Computer Science (083), CBSE Class 12 Informatics Practices (065), CBSE Class 12 Information Technology (802)
On screen in this lesson
Why group rows?
| Aggregate alone: one answer for the whole table |
| Often we want one answer for each group |
| GROUP BY makes groups, then aggregates each one |
The GROUP BY rule
| Columns in SELECT: the grouped column or aggregates |
| SELECT name ... GROUP BY sec is wrong |
| A group has many names but only one sec |
HAVING vs WHERE
| WHERE | HAVING | |
|---|---|---|
| Filters | rows | groups |
| Runs | before grouping | after grouping |
| Aggregates? | not allowed | allowed |
Clause order
| SELECT ... FROM ... |
| WHERE: filter rows |
| GROUP BY: make groups |
| HAVING: filter groups |
| ORDER BY: sort, always last |
Answer 1
| city | MAX(marks) |
|---|---|
| Surat | 88 |
| Pune | 91 |
| Delhi | NULL |
Quick recap
| GROUP BY puts rows with the same value together |
| Aggregates then give one answer per group |
| SELECT only the grouped column or aggregates |
| WHERE filters rows; HAVING filters groups |
| Order: WHERE, GROUP BY, HAVING, ORDER BY |
Quick answers
Why does WHERE COUNT(*) > 1 give an error?
Aggregates are not allowed in WHERE. Use HAVING.
What is the clause order?
SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY.
KwickClips from this lesson
Short clips, one idea each. Good for revision the night before.
What does GROUP BY do first?36 sec
How many rows does GROUP BY return?36 sec
Where do aggregates belong?36 sec
How many rows remain after HAVING COUNT(*) > 1?36 secThe full lesson, in text
Hello students, welcome to Kwickprep. Can one query give the average marks of every section at once? And why does where avg of marks give an error? Today we will learn group by and having, and solve output questions the way board exams ask them.
All our examples use one table called student. It has five columns: roll, name, section, marks and city. Kabir was absent for the test, so his marks are NULL. NULL means no value at all. It is not zero, and it is not a blank space.
An aggregate like avg gives one answer for the whole table. But a teacher often wants one answer for each section. Group by first puts rows with the same value into one group. Then the aggregate works on each group separately.
Here is the process as a flowchart. First, the rows are collected by section. Section A gets Riya, Neha and Meera. Section B gets Aman and Kabir. Then count star runs on each group. So A gives three and B gives two. The result has one row for each group.
Here is the query. We select the section and count star, and group by section. Section A has three students and section B has two. The groups may come out in any order, so add order by sec if order matters.
Every aggregate works per group. Section A has eighty eight, sixty five and ninety one. The average is two hundred forty four divided by three, which is eighty one point three three. In section B, Kabir's NULL is skipped, so the average is just seventy two.
Remember one important rule. In select, write only the grouped column or aggregate functions. Selecting name while grouping by section is wrong. A group has many names but only one section, so SQL cannot pick one name. Strict MySQL gives an error for it.
To filter groups, we use having, not where. Where filters single rows, while having filters whole groups. Where runs before the rows are grouped, and having runs after grouping. So where cannot use an aggregate, but having can.
Let us find cities with more than one student. Group by city makes three groups. Surat has two, Pune has two, and Delhi has one. Having count star greater than one removes Delhi. Writing where count star greater than one here would give an error.
Both can appear in one query. First, where keeps rows with marks above seventy. That leaves Riya and Meera in A, and Aman in B. Then group by makes the groups, A with two and B with one. Having keeps groups with at least two rows, so only A remains.
Write the clauses in this fixed order. Select and from come first. Where filters the rows. Group by makes the groups. Having filters the groups. Order by sorts the final result, and it always comes last.
Now try questions like the ones in board exams. Pause and predict the output of this query before the next slide. Think group by group. Remember what happens to Kabir's NULL.
Here is the answer, one row per city. Surat has eighty eight and sixty five, so the maximum is eighty eight. Pune has seventy two and ninety one, so ninety one. Delhi has only Kabir, whose marks are NULL. Max of only NULL values is NULL, not zero.
Here is a second question with having. Section A has three marks, adding up to two hundred forty four. Section B has count of marks one, because the NULL is skipped, and the sum is seventy two. Seventy two is not more than one hundred, so section B is removed. Only A appears.
Let us revise what we learned today. Group by puts rows with the same value into one group. The aggregates then give one answer for each group. In select, use only the grouped column or aggregates. Where filters rows before grouping, and having filters groups after grouping. The order is where, group by, having, then order by. Solve output questions group by group, and always check for NULL values.
Courses that teach this
| Course | Unit |
|---|---|
| CBSE Class 12 Computer Science (083) | Database Management |
| CBSE Class 12 Informatics Practices (065) | Database Query using SQL |
| CBSE Class 12 Information Technology (802) | Database Concepts - RDBMS Tool |
Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.
Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

