sqlcmd.net validated sql reference
beginner data-quality MySQL MariaDB SQL Server PostgreSQL SQLite

Count Missing Values Per Column

Audit a table for `NULL` values by counting how many rows have no value in each column of interest.

Docker-validated Not currently validation-green

Count how many customers are missing an email or phone number

Of the four customers, Bob and Dave have no email (2 nulls), and Carol and Dave have no phone (2 nulls). Each SUM(CASE WHEN ... IS NULL THEN 1 ELSE 0 END) counts the nulls for one column. Adding more columns to the SELECT list extends the audit without additional table scans.

Rows loaded before the example query runs.
Setup
CREATE TABLE customers (
  id INT,
  name VARCHAR(50),
  email VARCHAR(100),
  phone VARCHAR(20)
);

INSERT INTO
  customers
VALUES
  (1, 'Alice', '[email protected]', '555-1234'),
  (2, 'Bob', NULL, '555-5678'),
  (3, 'Carol', '[email protected]', NULL),
  (4, 'Dave', NULL, NULL);
Shared across supported engines.
SQL
SELECT
  COUNT(*) AS total_rows,
  SUM(
    CASE
      WHEN email IS NULL THEN 1
      ELSE 0
    END
  ) AS missing_email,
  SUM(
    CASE
      WHEN phone IS NULL THEN 1
      ELSE 0
    END
  ) AS missing_phone
FROM
  customers;
Returned rows for the shared example.
total_rowsmissing_emailmissing_phone
422

Identical syntax and result across all engines.

Where this command helps.

  • auditing a freshly loaded table to find columns with high rates of missing data
  • validating that required fields were populated before promoting data to production

What the command is doing.

A quick data quality check is to count the number of NULL values in every column you care about. The standard pattern wraps each column in a CASE WHEN col IS NULL THEN 1 ELSE 0 END expression inside SUM. A COUNT(*) alongside it gives the total row count so you can compute the percentage of missing values. This approach works across all engines and requires only a single table scan.