AnalystPracticeGuides
DashboardSign up

SQL COUNT: COUNT(*), COUNT(column) and COUNT(DISTINCT)

COUNT(*) counts rows. COUNT(column) counts rows where that column is not NULL. COUNT(DISTINCT column) counts different non-NULL values. Interview questions often hinge on exactly this difference.

Watch it solved. Q3: count unique values (COUNT DISTINCT), from 1:48, in our video “SQL Interview Questions and Answers for Data Analysts (Easy to Hard)”. Pressing play loads the video from YouTube (privacy). Open on YouTube

How do the three differ?

For an orders table with 10 rows, 2 of them without a coupon, and 7 different customers:

QueryResult
COUNT(*)10
COUNT(coupon)8
COUNT(DISTINCT customer_id)7

How do you count with a condition?

Filter in WHERE:

SELECT COUNT(*) FROM orders WHERE status = 'paid';

Or count several conditions in one query:

SELECT SUM(CASE WHEN status = 'paid' THEN 1 ELSE 0 END) AS paid,
SUM(CASE WHEN status = 'open' THEN 1 ELSE 0 END) AS open
FROM orders;

How do you count per group?

SELECT country, COUNT(*) AS customers
FROM customers
GROUP BY country;

The LEFT JOIN trap

After a LEFT JOIN, COUNT(*) counts a customer without orders as 1. Count a column from the right table instead, COUNT(o.id), to get 0.

Try it yourself. Practise this in a real database in your browser, with instant feedback.
Practise COUNT

Updated November 2026.

← All free guides