CodeBlanks Logo Icon CodeBlanks

NULL and aggregates

See how NULL affects aggregates.

NULL and aggregates

Aggregate functions skip NULL values. Only COUNT(*) counts all rows.

SELECT COUNT(*), COUNT(email) FROM customers;

This query gives total customers and those with an email.

COUNT(*), COUNT(email) is selected from customers.

NULL and aggregates — details

COUNT(column) does not count NULLs. SUM, AVG, MIN, MAX also ignore NULLs.

That's why you decide whether NULL means zero or unknown in averages and totals, and use COALESCE when needed.

Practice NULL and aggregates

Find how many customers have an email with COUNT(email).

Starter code

query.sql
-- count emails
SELECT

Sample solution

query.sql
SELECT COUNT(email) FROM customers;

Expected output

count
12

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Add COUNT

Question
SELECT ____(email) FROM customers;
Answer
SELECT COUNT(email) FROM customers;

Explanation: COUNT(email) counts only rows with an email; NULLs are not counted.

2 NULL and aggregates: match the terms

Term Meaning
COUNT(*) all rows
COUNT(column) non-NULL
NULL skip
SUM sum

3 Does COUNT(email) count NULLs?

  • Raises an error
  • Counts
  • No, it does not count them Correct answer
  • Some of them

Explanation: COUNT(column) does not count NULLs.

All lessons in Intermediate