CodeBlanks Logo Icon CodeBlanks

COUNT

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

query.sql
-- count rows
SELECT

Sample solution

query.sql
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

Question
SELECT ____(*) FROM products;
Answer
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

All lessons in Intermediate