Skip to content
IT glossary
DatabasesUpdated December 3, 2026

GROUP BY

This SQL clause puts rows with matching values into groups so you can return a count, total, average, or other summary for each group.

Also known as: HAVING, агрегатные функции

What GROUP BY does

Start with what you want one result row to represent. If your source has one row per support ticket but your report needs one row per team, group by the team. If the report needs one row per team and priority, group by both. SQL creates a group for each combination found in the input, and aggregate functions such as COUNT, SUM, and AVG summarize the rows in that group.

Grouping doesn't change the source table. It changes the level of detail of the query result. It also doesn't guarantee a sort order; use ORDER BY when the order matters.

Filter rows first, then groups

Suppose you want to find teams with at least five open tickets and show how many distinct customers those tickets came from:

SELECT team_id,
       COUNT(*) AS open_tickets,
       COUNT(DISTINCT customer_id) AS affected_customers
FROM tickets
WHERE status = 'open'
GROUP BY team_id
HAVING COUNT(*) >= 5
ORDER BY open_tickets DESC, team_id;

Read the query in logical order: WHERE removes tickets that aren't open, GROUP BY collects the remaining tickets by team, and HAVING keeps only groups with at least five rows. A team with six tickets, four of them open, doesn't make the cut. Five open tickets from the same known customer count as five tickets but only one affected customer. The database may execute the query using a different physical plan; the logical order explains the result.

Common mistakes

  • Selecting a value that varies within a group. Adding ticket_id to SELECT doesn't tell SQL which ticket ID should represent the team. As a rule, select grouping keys and aggregate results. Some databases also allow columns whose values are uniquely determined by the grouping key.
  • Confusing row counts with value counts. COUNT(*) counts every row in a group. COUNT(customer_id) skips rows with a NULL customer ID, and COUNT(DISTINCT customer_id) counts each distinct non-null ID once. A missing grouping key is different: tickets with a NULL team ID form a group of their own in this example.
  • Forgetting that joins can multiply rows. Joining each ticket to its comments produces multiple rows for tickets with multiple comments. Counting those rows won't count tickets. If you need comment totals alongside ticket totals, one option is to aggregate comments to one row per ticket before joining.

How it comes up in interviews

A common exercise is to find duplicate emails. If only known emails should count, filter out NULL with WHERE email IS NOT NULL, group by email, and keep groups with HAVING COUNT(*) > 1. Explain what each output row represents: one duplicated email, not one user. If the interviewer wants every matching user row, join the grouped result back to the users table or use a window function in a subquery. Unlike GROUP BY, a window function can count the rows in each partition without collapsing them.