COUNT() in MySQL — How It Works

What the MySQL COUNT() function does, with an example.

Published October 20, 2026

COUNT() counts rows. COUNT(*) counts all rows, COUNT(col) counts non-NULL values and COUNT(DISTINCT col) counts unique non-NULL values.

How it works

Pair with GROUP BY for per-group counts.

Try it in MySQL

SELECT COUNT(*) FROM users
1500
SELECT COUNT(phone) FROM users
900
SELECT COUNT(DISTINCT city) FROM users
42

Practical examples

Example 1

SELECT COUNT(*) FROM users;
-- 1500

Example 2

SELECT COUNT(phone) FROM users;
-- 900

Example 3

SELECT COUNT(DISTINCT city) FROM users;
-- 42

Use it in a real query

SELECT COUNT(*) FROM users;

Common mistakes

  • Using COUNT(col) when you want all rows (NULLs are skipped).
  • Counting huge tables frequently (cache the result).
  • Forgetting GROUP BY columns.

Key takeaways

  • COUNT() in one line: COUNT() counts rows.
  • Try queries in a test database first.
  • Check NULL handling and data types.

FAQ

COUNT(*) vs COUNT(1)?

They are equivalent in MySQL.

Does COUNT() work in other databases?

Most have an equivalent, but names and syntax vary between MySQL, PostgreSQL, SQL Server and SQLite.

More SQL articles