Aggregates & GROUP BY questions and answers

COUNT, SUM, AVG, grouping and HAVING versus WHERE. Practise aggregates & group by questions from the sql section of placement tests, with a worked explanation after every answer. Practice is free and needs no sign-up.

Sample aggregates & group by questions with worked answers

Question 1

A table Sales(sale_id, rep, region, amount) has no NULL in region. Which expression gives the number of different regions in the table?

  1. COUNT(region)
  2. COUNT(DISTINCT region)
  3. DISTINCT COUNT(region)
  4. SUM(DISTINCT region)

Answer: B. COUNT(DISTINCT region)

Explanation: COUNT(region) counts every row with a region, repeats included. Putting DISTINCT inside the brackets counts each different value once.

Question 2

A table Sales(sale_id, rep, region, amount) is given. Which query lists the reps whose total sales are more than 800?

  1. SELECT rep FROM Sales WHERE SUM(amount) > 800 GROUP BY rep;
  2. SELECT rep FROM Sales WHERE amount > 800 GROUP BY rep;
  3. SELECT rep FROM Sales GROUP BY rep HAVING SUM(amount) > 800;
  4. SELECT rep, SUM(amount) FROM Sales HAVING amount > 800;

Answer: C. SELECT rep FROM Sales GROUP BY rep HAVING SUM(amount) > 800;

Explanation: A condition on a group total must go in HAVING, which runs after GROUP BY. WHERE cannot contain SUM, and WHERE amount > 800 tests single sales, not the total per rep.

Question 3

Table Sales(sale_id, rep, region, amount) has the regions North, South and West. Compare:
Q1: SELECT region, SUM(amount) FROM Sales WHERE region <> 'West' GROUP BY region;
Q2: SELECT region, SUM(amount) FROM Sales GROUP BY region HAVING region <> 'West';
Which statement is correct?

  1. Q2 is an error, because HAVING may contain only aggregate functions
  2. Q1 returns North and South, but Q2 also returns West with a NULL total
  3. Q2 returns different totals, because HAVING changes which rows are added up
  4. Both return the same rows; Q1 discards West rows before grouping, Q2 discards the West group after it

Answer: D. Both return the same rows; Q1 discards West rows before grouping, Q2 discards the West group after it

Explanation: HAVING may test a grouping column, so Q2 is valid. Removing the West rows before grouping or the West group after grouping leaves the same North and South rows with the same totals; Q1 simply does less work.

All 20 aggregates & group by questions with answers

Aggregates & GROUP BY: common questions

What are aggregates & group by questions?

COUNT, SUM, AVG, grouping and HAVING versus WHERE. They appear in the sql section of many placement tests.

Where can I practise aggregates & group by questions with answers for free?

Right here on LogicWiz Exams. Every aggregates & group by question has a worked explanation after you answer. Practice is free and needs no sign-up.

How should I practise aggregates & group by?

Start with easy questions, then move to medium and hard. Read the explanation after each answer, and come back to the ones you missed.

Start practising

On this page you can start a fresh set of aggregates & group by questions, choose easy, medium or hard, and time yourself if you want to. Or practise the whole subject: SQL practice.

More sql topics

Other subjects

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge · Test from a job description · LogicWiz Open: free team quiz · Free interview prep