SQL GROUP BY and HAVING: Class 12 practice
GROUP BY makes one result row per distinct value; aggregate functions (SUM, COUNT, AVG) run inside each group. The quick answer and an example come first; then the method, worked examples and six graded questions.
SQL GROUP BY and HAVING: quick answer
GROUP BY makes one result row per distinct value; aggregate functions (SUM, COUNT, AVG) run inside each group. HAVING filters groups after aggregation, while WHERE filters rows before.
Example: Table sales(city, amount): (Mysuru, 1300), (Pune, 1100), (Delhi, 500), (Pune, 1800), (Mysuru, 800), (Mysuru, 700), (Delhi, 1100). Give the output of SELECT city, SUM(amount) FROM sales GROUP BY city ORDER BY city; Answer: Delhi 1600; Mysuru 2800; Pune 2900.
- Where it is taught: "Database Query using SQL", Class 12 Computer Science.
- On this page: the method, 3 worked examples and 6 graded questions with a full answer key.
How to use this page (about 30 minutes)
- Read the method once, then cover the worked examples and try them yourself before reading the steps.
- Do the two Basic questions without notes. If either is wrong, re-read the matching worked example.
- Do the Standard and Challenge questions, then check every line of working against the answer key — not only the final answer.
- Write down the mistake behind each wrong answer and redo that question the next day.
What you should know first
- SQL aggregate functions
The idea behind sql group by and having
GROUP BY makes one result row per distinct value; aggregate functions (SUM, COUNT, AVG) run inside each group. HAVING filters groups after aggregation, while WHERE filters rows before.
Step-by-step method
- Split rows into groups by the column.
- Compute the aggregate per group.
- Apply HAVING to the group results.
Worked example 1 (Basic)
Table sales(city, amount): (Mysuru, 1300), (Pune, 1100), (Delhi, 500), (Pune, 1800), (Mysuru, 800), (Mysuru, 700), (Delhi, 1100). Give the output of SELECT city, SUM(amount) FROM sales GROUP BY city ORDER BY city;
- Group totals: Mysuru 2800, Pune 2900, Delhi 1600
- Answer: Delhi 1600; Mysuru 2800; Pune 2900
Worked example 2 (Standard)
Table sales(city, amount): (Mysuru, 1700), (Pune, 1300), (Kochi, 700), (Delhi, 400), (Kochi, 1000), (Kochi, 1000), (Mysuru, 1800), (Kochi, 1600), (Mysuru, 100). Give the output of SELECT city, COUNT(*) FROM sales GROUP BY city ORDER BY city;
- Group totals: Mysuru 3, Pune 1, Kochi 4, Delhi 1
- Answer: Delhi 1; Kochi 4; Mysuru 3; Pune 1
Worked example 3 (Challenge)
Table sales(city, amount): (Mysuru, 500), (Delhi, 1700), (Pune, 1200), (Mysuru, 1900), (Pune, 1300), (Kochi, 1900), (Pune, 700), (Kochi, 1800), (Delhi, 400), (Pune, 1500), (Kochi, 300). Give the output of SELECT city, SUM(amount) FROM sales GROUP BY city HAVING SUM(amount) > 2300 ORDER BY city;
- Group totals: Mysuru 2400, Delhi 2100, Pune 4700, Kochi 4000
- Keep groups over 2300
- Answer: Kochi 4000; Mysuru 2400; Pune 4700
Common mistakes and misconceptions
- Using WHERE for an aggregate condition
- Forgetting a group with one row
Practice questions
- (Basic) Table sales(city, amount): (Mysuru, 600), (Pune, 1700), (Delhi, 1300), (Mysuru, 700), (Mysuru, 1400), (Mysuru, 200), (Pune, 1200). Give the output of SELECT city, SUM(amount) FROM sales GROUP BY city ORDER BY city;
- (Basic) Table sales(city, amount): (Pune, 300), (Mysuru, 1200), (Mysuru, 1300), (Pune, 1400), (Delhi, 100), (Delhi, 100), (Pune, 1700). Give the output of SELECT city, SUM(amount) FROM sales GROUP BY city ORDER BY city;
- (Standard) Table sales(city, amount): (Delhi, 700), (Delhi, 1300), (Kochi, 1100), (Kochi, 1200), (Pune, 200), (Delhi, 900), (Delhi, 1300), (Delhi, 2000), (Kochi, 700). Give the output of SELECT city, COUNT(*) FROM sales GROUP BY city ORDER BY city;
- (Standard) Table sales(city, amount): (Delhi, 400), (Pune, 1300), (Pune, 500), (Pune, 900), (Delhi, 1400), (Mysuru, 1600), (Kochi, 1400), (Mysuru, 1400), (Mysuru, 500). Give the output of SELECT city, COUNT(*) FROM sales GROUP BY city ORDER BY city;
- (Challenge) Table sales(city, amount): (Pune, 1000), (Delhi, 500), (Mysuru, 1900), (Mysuru, 500), (Delhi, 1800), (Mysuru, 1600), (Pune, 900), (Delhi, 700), (Delhi, 1300), (Pune, 600), (Pune, 1700). Give the output of SELECT city, SUM(amount) FROM sales GROUP BY city HAVING SUM(amount) > 2600 ORDER BY city;
- (Challenge) Table sales(city, amount): (Pune, 900), (Mysuru, 1600), (Mysuru, 1100), (Kochi, 1300), (Pune, 900), (Delhi, 1500), (Delhi, 700), (Pune, 1600), (Pune, 2000), (Kochi, 1800), (Kochi, 1400). Give the output of SELECT city, SUM(amount) FROM sales GROUP BY city HAVING SUM(amount) > 1700 ORDER BY city;
Answer key with full working
- Q1: Group totals: Mysuru 2900, Pune 2900, Delhi 1300 → Answer: Delhi 1300; Mysuru 2900; Pune 2900
- Q2: Group totals: Pune 3400, Mysuru 2500, Delhi 200 → Answer: Delhi 200; Mysuru 2500; Pune 3400
- Q3: Group totals: Delhi 5, Kochi 3, Pune 1 → Answer: Delhi 5; Kochi 3; Pune 1
- Q4: Group totals: Delhi 2, Pune 3, Mysuru 3, Kochi 1 → Answer: Delhi 2; Kochi 1; Mysuru 3; Pune 3
- Q5: Group totals: Pune 4200, Delhi 4300, Mysuru 4000 → Keep groups over 2600 → Answer: Delhi 4300; Mysuru 4000; Pune 4200
- Q6: Group totals: Pune 5400, Mysuru 2700, Kochi 4500, Delhi 2200 → Keep groups over 1700 → Answer: Delhi 2200; Kochi 4500; Mysuru 2700; Pune 5400
Which textbook this practice follows
This skill is taught under "Database Query using SQL" in Class 12 Computer Science. Check your own textbook's contents page — CBSE, ICSE/ISC and state boards order chapters differently.
Answers on this page are generated and automatically re-checked by a second, independent calculation. They have not been reviewed by a teacher — if you spot a problem, tell us on WhatsApp.
Still stuck after the answer key?
If the same step in sql group by and having keeps going wrong, a short live 1 on 1 class can find the exact gap. The first demo class is free; send the class, board and chapter on WhatsApp (+91 9379416374).
Related practice and notes
More Class 12 Computer Science practice
Continue with this syllabus
Frequently asked questions
Which chapter is sql group by and having in?
This skill is taught under "Database Query using SQL" in Class 12 Computer Science. Check your own textbook's contents page — CBSE, ICSE/ISC and state boards order chapters differently.
Which question should I start with?
Start with the Basic questions. Move to Standard only when both Basic answers match the key with the same working; the Challenge questions combine this skill with earlier ones.
How were the answers checked?
Answers on this page are generated and automatically re-checked by a second, independent calculation. They have not been reviewed by a teacher — if you spot a problem, tell us on WhatsApp.
Is this only for CBSE?
The skill names follow NCERT/CBSE, but the method is the same for ICSE, state boards, IGCSE and IB. Only the chapter name and order differ.
Can I get live help with this topic?
Yes. RG's E-Learning teaches live 1 on 1 online classes; the first demo class is free and fees are shared after it.