COUNT(*) vs COUNT(column): what 2,628 real earthquakes teach about missing data
Most SQL tutorials teach COUNT(*) in the first lesson and move on. That's fine on a tidy practice table where every cell is filled in. Real data is different. The first thing a real dataset teaches you is that "how many rows?" and "how many values?" are two separate questions, and mixing them up gives you wrong numbers.
This guide uses the quakes table on this site: every earthquake of magnitude 2.5 or larger that the U.S. Geological Survey recorded worldwide in October 2026. It's a public-domain feed, downloaded as-is, with its gaps left in.
Start with the size of the table
Before anything else, find out how big the table is.
SELECT COUNT(*) AS total_rows FROM quakes;Output (real run against the practice database):
| total_rows |
|---|
| 2628 |
COUNT(*) counts rows. It doesn't care what's inside them. A row where every column is NULL still counts. That makes it the right tool for "how many records do I have?" and the wrong tool for "how many records have a value for X?"
Now count individual columns
COUNT(column) counts only the rows where that column is not NULL. Run it on a few columns side by side and the gaps show up right away:
SELECT COUNT(*) AS total_rows,
COUNT(place) AS has_place,
COUNT(gap) AS has_gap,
COUNT(mag_error) AS has_mag_error,
COUNT(horizontal_error) AS has_horizontal_error
FROM quakes;Output (real run against the practice database):
| total_rows | has_place | has_gap | has_mag_error | has_horizontal_error |
|---|---|---|---|---|
| 2628 | 2627 | 2627 | 2623 | 2611 |
Every column here should, in theory, be filled in for every earthquake. In practice, one event has no place description, one has no azimuthal gap, five have no magnitude error estimate, and seventeen have no horizontal error. None of that is a bug in the database. It's what a live scientific feed looks like: some measurements weren't available when the record was published.
This one query is worth running on any new table. I think of it as a "profile" query. Put COUNT(*) next to COUNT(col) for the columns you care about, and you know in a few seconds where the holes are.
Find the rows with holes
Once you know a column has missing values, look at the actual rows. This is where many beginners trip up:
SELECT COUNT(*) FROM quakes WHERE place = NULL;Output (real run against the practice database):
| COUNT(*) |
|---|
| 0 |
Zero rows, even though we just saw that one place is missing. In SQL, NULL means "unknown." Any comparison with an unknown value, even = NULL, is itself unknown, so the WHERE clause never passes. You have to ask the question differently:
SELECT id, time, mag
FROM quakes
WHERE place IS NULL;Output (real run against the practice database):
| id | time | mag |
|---|---|---|
| us7000tw10 | 2026-10-25T17:50:43.930Z | 4.3 |
IS NULL and IS NOT NULL are the only reliable ways to test for missing values. If a query that "should" return rows returns nothing, check whether you wrote = NULL somewhere.
Averages quietly skip NULLs
Aggregate functions such as AVG, SUM, MIN and MAX also ignore NULLs. Usually that's what you want, but you should know it's happening. Compare these two averages of the horizontal location error:
SELECT AVG(horizontal_error) AS avg_ignoring_nulls,
AVG(COALESCE(horizontal_error, 0)) AS avg_treating_null_as_zero
FROM quakes;Output (real run against the practice database):
| avg_ignoring_nulls | avg_treating_null_as_zero |
|---|---|
| 8.038641604278475 | 7.986641259045318 |
The first number averages only the 2,611 rows that have a value. The second replaces each missing value with zero, which drags the average down. Here the difference is small because only seventeen rows are missing. On a column that's half empty, the two answers could be wildly different, and only one of them describes the data honestly.
The rule of thumb: treat a missing value as zero only when "missing" really means zero. A missing error estimate doesn't mean the error was zero. It means nobody reported it. So the first average is the honest one, and you should say how many rows it's based on.
Not every row is what the table name says
There's one more check worth making on any "events" table: are all the events the kind you think they are?
SELECT type, COUNT(*) AS n
FROM quakes
GROUP BY type
ORDER BY n DESC;Output (real run against the practice database):
| type | n |
|---|---|
| earthquake | 2626 |
| landslide | 1 |
| explosion | 1 |
The table is called quakes, but two rows aren't earthquakes: one explosion and one landslide. In a report about earthquake counts, they should probably be filtered out with WHERE type = 'earthquake'. That's a small cleaning decision, and you should write it down so whoever reads your number knows what's included.
Putting it together: a quick profile habit
Here's the routine this table suggests, and it works on almost any new dataset:
- Count the rows with
COUNT(*). - Count the values in the columns you'll use with
COUNT(column), side by side. - Look at the rows with gaps using
IS NULL. Never use= NULL. - Decide what a gap means before averaging or summing: unknown, not applicable, or truly zero.
- Check the categories with
GROUP BYon the type or status columns, so you know what you're counting.
None of these steps is advanced SQL. They're habits. What separates a number you can trust from one you can't is usually not a clever query. It's whether someone checked the boring things first.
Practice it
The exercises on this site that cover these ideas are How many earthquakes are in the table?, COUNT(*) vs COUNT(column), Find the rows with a missing place and Events per type. Each one is graded in your browser against the real data, so you'll know right away whether your answer matches.
A good next step is the guide on NULLs, blanks and placeholder values. Real data has more than one way of saying "unknown," and NULL is only the first.