COUNT
- SQL course
- Intermediate
- Lesson 36/115
- 4 min
Count rows with COUNT.
COUNT
COUNT returns the number of rows. COUNT(*) counts all rows, COUNT(column) counts non-NULL values.
SELECT COUNT(*) FROM products;
SELECT COUNT(price) FROM products;This query gives total rows and non-NULL price count.
COUNT(*) is selected from products.
COUNT — details
COUNT(*) counts NULL rows too. COUNT(column) counts only non-NULL values. The difference matters.
For example, if 3 of 10 customers have no email, COUNT(*) returns 10 and COUNT(email) returns 7.
Practice COUNT
Find the total row count of products with COUNT(*).
Starter code
-- count rows
SELECTSample solution
SELECT COUNT(*) FROM products;Expected output
count 15
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does COUNT(*) count?
- Column names
- Only NULLs
- All rows Correct answer
- Only non-empty values
Explanation: COUNT(*) counts all rows.
2 Add COUNT
SELECT ____(*) FROM products;
SELECT COUNT(*) FROM products;
Explanation: COUNT(*) counts every row, including NULLs.
3 COUNT: match the terms
| Term | Meaning |
|---|---|
COUNT(*) |
all rows |
COUNT(column) |
non-NULL |
SUM |
sum |
AVG |
average |