RG's E-Learning Centre · Book a free demo class · WhatsApp +91-9379416374

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. Read the method, follow the three worked examples, then try the six questions.

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

  1. Split rows into groups by the column.
  2. Compute the aggregate per group.
  3. 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;

  1. Group totals: Mysuru 2800, Pune 2900, Delhi 1600
  2. 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;

  1. Group totals: Mysuru 3, Pune 1, Kochi 4, Delhi 1
  2. 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;

  1. Group totals: Mysuru 2400, Delhi 2100, Pune 4700, Kochi 4000
  2. Keep groups over 2300
  3. 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

  1. (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;
  2. (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;
  3. (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;
  4. (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;
  5. (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;
  6. (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

Syllabus note

This skill is listed under "Database Query using SQL" for Class 12 in the NCERT/CBSE books. ICSE, IGCSE, IB and state boards may teach it in a different chapter or year.

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.

Related practice and notes

Frequently asked questions

Which chapter is sql group by and having in?

"Database Query using SQL", Class 12 Computer Science (NCERT/CBSE). Other boards may place it differently.

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.

Book a free demo class