What is the primary purpose of the GROUP BY clause in SQL?
2024
What is the primary purpose of the GROUP BY clause in SQL?
- A.
To sort rows in ascending or descending order
- B.
To group rows with identical values in specified columns for aggregate processing
- C.
To permanently remove duplicate rows from a table
- D.
To filter rows before retrieval
- E.
To combine records from multiple tables
Attempted by 31 students.
Show answer & explanation
Correct answer: B
The GROUP BY clause groups rows that share the same values in one or more specified columns into summary groups, so that aggregate functions such as COUNT, SUM, AVG, MIN and MAX can be applied to each group separately rather than to the entire result set.
For example, the query below returns one row per department together with the number of employees in that department:
SELECT department, COUNT(*) AS employee_count FROM employees GROUP BY department;
Here the rows are collapsed into one group per distinct department value, and COUNT(*) is computed per group. This is the defining purpose of GROUP BY, which is why option B is correct.
The other choices describe different SQL features: ORDER BY sorts the result set, the DISTINCT keyword removes duplicate rows from output, the WHERE clause filters individual rows before grouping, and JOIN combines rows from multiple tables — none of these is what GROUP BY does.