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

Count Rows With COUNT(*)

Aggregate a result set into a single row that reports how many rows matched.

Docker-validated Not currently validation-green

Count rows that match a filter

The WHERE clause limits the rows first, then COUNT(*) collapses the remaining set to one numeric result.

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

INSERT INTO
  tasks (id, status)
VALUES
  (1, 'open'),
  (2, 'done'),
  (3, 'open');
Shared across supported engines.
SQL
SELECT
  COUNT(*) AS total_open
FROM
  tasks
WHERE
  status = 'open';
Returned rows for the shared example.
total_open
2

All engines return a single-row aggregate result for this example.

Where this command helps.

  • reporting how many records match a condition
  • checking whether a filtered dataset is empty or large enough to act on

What the command is doing.

COUNT(*) is the standard way to count rows. It is especially useful after applying filters, because it reports how many rows remain rather than returning each row individually.