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

Understand COUNT(column) Versus COUNT(*)

`COUNT(column)` skips `NULL` values, while `COUNT(*)` counts every row that made it into the result set.

Docker-validated Not currently validation-green

Compare total rows with only non-NULL timestamps

There are three invoice rows total, so COUNT() returns 3. Only two rows have a non-NULL paid_at, so COUNT(paid_at) returns 2. Use COUNT() when you want all rows, and COUNT(column) only when you intentionally want to ignore missing values.

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

INSERT INTO
  invoices (id, paid_at)
VALUES
  (1, '2026-04-01'),
  (2, NULL),
  (3, '2026-04-03');
Shared across supported engines.
SQL
SELECT
  COUNT(*) AS total_rows,
  COUNT(paid_at) AS rows_with_paid_at
FROM
  invoices;
Returned rows for the shared example.
total_rowsrows_with_paid_at
32

All engines treat `COUNT(column)` as a count of non-NULL values only.

Where this command helps.

  • checking how many rows have a value in an optional column
  • avoiding undercounting when nullable columns are present

What the command is doing.

A common counting mistake is assuming COUNT(column) behaves the same as COUNT(). It does not. COUNT(column) only counts rows where that specific column is not NULL, while COUNT() counts every row after filtering. This matters when optional fields, missing timestamps, or nullable foreign keys are involved.