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

Group Rows And Count

Aggregate rows by category with `GROUP BY` and count how many rows fall into each group.

Docker-validated Not currently validation-green

Count rows per status

Every distinct status value becomes one group, and COUNT(*) reports how many source rows fell into that group.

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

INSERT INTO
  tickets (id, status)
VALUES
  (1, 'open'),
  (2, 'closed'),
  (3, 'open');
Shared across supported engines.
SQL
SELECT
  status,
  COUNT(*) AS total
FROM
  tickets
GROUP BY
  status
ORDER BY
  status;
Returned rows for the shared example.
statustotal
closed1
open2

Sorting the grouped output keeps the example deterministic across engines.

Where this command helps.

  • building summary reports by category or status
  • turning row-level activity into grouped totals

What the command is doing.

GROUP BY collects rows that share the same value in one or more columns. Combined with COUNT(*), it produces category totals that are useful in reporting queries.