Results.

SCEPTRA.TECH

Why SQLite COUNT(column) Is Lower Than COUNT(*): Audit NULLs Before Changing Data

SQLite count comparisons: all rows, non-NULL values and missing-value checks

A SQLite report says there are 200 records, but a second count returns 173. Before looking for lost rows, check what each query counts. COUNT(*) counts rows. COUNT(column) counts non-NULL values in that column. Both results can be correct while answering different questions.

I would put the two counts in one query before changing the data. That keeps the comparison on the same set of input rows and makes the missing-value question explicit.

Count rows and populated values side by side

Suppose a table named contacts has an optional text column named email. Adapt this read-only example to your own table and column names:

SELECT
  COUNT(*) AS total_rows,
  COUNT(email) AS nonnull_email_rows,
  COUNT(*) - COUNT(email) AS null_email_rows
FROM contacts;

SQLite's aggregate-function documentation defines COUNT(X) as the number of times X is not NULL in a group. The star form returns the total number of rows. Subtracting those two counts therefore gives the number with NULL email values in this input set.

The opening numbers are an illustration, not measured results from a real contact list. If the first two outputs were 200 and 173, their difference would be 27 NULL values. It would not mean 27 records disappeared from the table.

Do not treat an empty string as NULL

A text value containing no characters is still a value. It is different from SQL NULL, so COUNT(email) includes an empty string. It also includes text that contains only spaces. That makes "non-NULL email" a narrower technical label than "usable email address."

To inspect NULLs and empty strings separately, use separate counts:

SELECT COUNT(*) AS null_email_rows
FROM contacts
WHERE email IS NULL;

SELECT COUNT(*) AS empty_email_rows
FROM contacts
WHERE email = '';

The first query uses IS NULL, not = NULL. SQLite documents special NULL behavior for IS and IS NOT. Ordinary equality with NULL does not work like comparing two ordinary text values.

These checks still do not validate an address. A string containing spaces, a placeholder such as "unknown" or malformed text needs a separate rule based on your application's requirements. Do not silently merge those categories into a single "valid" count.

Watch out for a tempting but wrong shortcut

COUNT(email IS NULL) does not count just the missing emails. In SQLite, the expression email IS NULL evaluates to true or false, represented as 1 or 0. Neither result is NULL, so COUNT counts both. Use the WHERE form above when the question is how many rows satisfy that condition.

Likewise, COUNT(DISTINCT email) answers another question: how many distinct non-NULL values appear. Repeated values are filtered before counting. It is not the number of contact rows and is not proof that the values belong to distinct people.

Keep the input set the same

If your real report includes a WHERE condition, include the same condition in each comparison. A count for active records should not be reconciled against the entire table without saying so. For a grouped report, interpret each count within its group rather than comparing one group's populated values with a table-wide total.

Joins deserve their own check too. When a query produces several rows for one contact, COUNT(*) counts those query rows, not automatically unique contacts. Establish which entity the report is meant to count before adding DISTINCT as a patch.

My stopping point would be a report with honest names: total rows, non-NULL values, NULL values and, if needed, empty strings under a documented rule. Keep the audit read-only until you know what each category means. Replacing every NULL with an empty string merely to make two numbers match changes the data without fixing the question.

Sources

Comments