← All references

GROUP BY

GROUP BY col HAVING condition

Aggregate rows by column.

GROUP BY

Groups rows that share a column value, then applies an aggregate function per group.

Aggregates

  • COUNT(*)
  • SUM(col)
  • AVG(col)
  • MIN(col) / MAX(col)

``sql\nSELECT country, COUNT(*) AS n\nFROM users\nGROUP BY country;\n``

Use HAVING (not WHERE) to filter groups.

Example

SELECT country, COUNT(*) AS user_count\nFROM users\nGROUP BY country;\n\nSELECT status, SUM(total) AS revenue\nFROM orders\nGROUP BY status;