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?
- COUNT(region)
- COUNT(DISTINCT region)
- DISTINCT COUNT(region)
- 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?
- SELECT rep FROM Sales WHERE SUM(amount) > 800 GROUP BY rep;
- SELECT rep FROM Sales WHERE amount > 800 GROUP BY rep;
- SELECT rep FROM Sales GROUP BY rep HAVING SUM(amount) > 800;
- 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?
- Q2 is an error, because HAVING may contain only aggregate functions
- Q1 returns North and South, but Q2 also returns West with a NULL total
- Q2 returns different totals, because HAVING changes which rows are added up
- 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
- Table Sales: sale_id | rep | region | amount 1 | Asif | North | 400 2 | Bina |…
- Table Sales: sale_id | rep | region | amount 1 | Asif | North | 400 2 | Bina |…
- A table Sales(sale_id, rep, region, amount) has no NULL in region. Which…
- Table Innings: inn_id | batter | team | runs 1 | Kunal | Tigers | 45 2 | Kunal…
- Table Sales: sale_id | rep | region | amount 1 | Asif | North | 400 2 | Bina |…
- A table Sales(sale_id, rep, region, amount) is given. Which query returns the…
- Table Innings: inn_id | batter | team | runs 1 | Kunal | Tigers | 45 2 | Kunal…
- Table Sales: sale_id | rep | region | amount 1 | Asif | North | 400 2 | Bina |…
- Table Tickets: ticket_id | agent | priority 1 | Sana | High 2 | NULL | Low 3 |…
- Table Attendance: student | subject | month_name | days Pooja | Maths | Jan |…
- Table Sales: sale_id | rep | region | amount 1 | Asif | North | 400 2 | Bina |…
- Table Innings: inn_id | batter | team | runs 1 | Kunal | Tigers | 45 2 | Kunal…
- A table Sales(sale_id, rep, region, amount) is given. Which query lists the…
- Table Sales: sale_id | rep | region | amount 1 | Asif | North | 400 2 | Bina |…
- Table Attendance: student | subject | month_name | days Pooja | Maths | Jan |…
- Table Sales: sale_id | rep | region | amount 1 | Asif | North | 400 2 | Bina |…
- Table Sales: sale_id | rep | region | amount 1 | Asif | North | 400 2 | Bina |…
- Table Innings: inn_id | batter | team | runs 1 | Kunal | Tigers | 45 2 | Kunal…
- Table Sales: sale_id | rep | region | amount 1 | Asif | North | 400 2 | Bina |…
- Table Sales(sale_id, rep, region, amount) has the regions North, South and…
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
- SELECT, WHERE & ORDER BY: Filtering, DISTINCT, IN, BETWEEN, LIKE, sorting and LIMIT.
- SQL Joins: Inner, left, right, full, cross and self joins, and row counts.
- Subqueries & CTEs: Scalar and correlated subqueries, IN, EXISTS and WITH.
- Window Functions: ROW_NUMBER, RANK, DENSE_RANK, PARTITION BY, LAG and running totals.
- NULL Handling: Three-valued logic, IS NULL, COALESCE and NULL in aggregates.
- SQL Functions & CASE: CASE WHEN, conditional aggregation, string and numeric functions.
- UNION, INTERSECT & EXCEPT: Combining results, duplicates and column rules.
- Tables, Keys & Constraints: CREATE, ALTER, primary and foreign keys, UNIQUE and CHECK.
- DML & Transactions: INSERT, UPDATE, DELETE, COMMIT, ROLLBACK and ACID.
Other subjects
- Quantitative Aptitude: Arithmetic, percentages, time and work, speed, probability and data interpretation: the numerical section of every fresher test.
- Logical Reasoning: Series, coding-decoding, arrangements, syllogisms and puzzles: the section that separates prepared candidates from the rest.
- Verbal Ability: Vocabulary, grammar, reading comprehension and sentence order: tested in nearly every IT services hiring exam.
- Technical & Programming: CS fundamentals, output prediction and pseudo-code: the technical MCQ round for IT roles.
- Python: Python quizzes by topic, from types and strings to classes and generators: output prediction and interview concepts.
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