sqlcmd.net validated sql reference
intermediate aggregation MySQL MariaDB SQL Server PostgreSQL

Group Rows By Multiple Columns

Use more than one column in `GROUP BY` when each combination defines a separate aggregate bucket.

Docker-validated Not currently validation-green

Count tickets by team and status

Each unique pair of team and status becomes its own result row. Billing has one open ticket and two closed tickets; Support has two open tickets and one closed ticket.

Rows loaded before the example query runs.
Setup
CREATE TABLE tickets (id INT, team VARCHAR(20), status VARCHAR(20));

INSERT INTO
  tickets (id, team, status)
VALUES
  (1, 'Support', 'open'),
  (2, 'Support', 'open'),
  (3, 'Support', 'closed'),
  (4, 'Billing', 'open'),
  (5, 'Billing', 'closed'),
  (6, 'Billing', 'closed');
Shared across supported engines.
SQL
SELECT
  team,
  status,
  COUNT(*) AS total_tickets
FROM
  tickets
GROUP BY
  team,
  status
ORDER BY
  team,
  status;
Returned rows for the shared example.
teamstatustotal_tickets
Billingclosed2
Billingopen1
Supportclosed1
Supportopen2

All engines group by the same `(team, status)` combinations and return the same counts here.

Where this command helps.

  • counting records per status within each region or team
  • building reports where the aggregate bucket depends on more than one dimension

What the command is doing.

Grouping by multiple columns creates one aggregate row per unique combination of those values. This is useful when a single grouping column is too broad, such as when you need counts by both region and status, or totals by both month and category.