Skip to the content

COUNT

SQL · Unit 5: Aggregates

You've spent four units counting rows by hand: six orders with a quantity of 1, three customers from Denver. The database knows how to count, and until today nobody had asked it to.

How many orders has the store received? COUNT(*) counts the rows and returns the number. In SELECT *, the * asked for every column. Inside COUNT it means "the whole row, whatever it holds": it counts rows and doesn't look at any value.

SELECT COUNT(*)
FROM orders;

Returns

COUNT(*)
12

The rest of the explanation is in the lesson, which is part of the full plan.

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Multiple choice

    The customers table has 8 rows and 4 columns. What shape is this query's result?

  2. 2. Put the lines in order

    How many products does the store sell? Put in order the query that answers it in a column named how_many. Two lines don't belong.

  3. 3. Predict the output

    Hannah's age is missing, and Luke and Dylan are both 27. What does this query return?

  4. 4. Find the bug

    The owner asks how many customers the store has, and it's 8. Someone thought a column had to go inside the parentheses, and the query says 7. Which line has the problem?

  5. 5. Complete the code

    How many customers does the store have to call to ask for their city? It's the total minus the ones who do have it on file. Fill in the blank to finish the subtraction.

  6. 6. Complete the code

    How many sections does the store have? In unit 3 you listed them with DISTINCT, one per row. Fill in the blank so that the database says how many there are.

  7. 7. Find the bug

    How many different products have been sold? The answer should be 7, all of them except the Speaker. The query runs without an error and says 12. Which line has the problem?

  8. 8. Put the lines in order

    How many customers have a city on file, and how many different cities do they live in? Put in order the query that gives the two numbers, in that order. Two lines don't belong.

  9. 9. Predict the output

    This query still has a LIMIT 5 left over from an earlier version, one that listed orders. The table has 12 rows. What does it return?

  10. 10. Multiple choice

    Tomorrow Ann, who already has three orders, places her fourth. What happens to the two numbers in this query, which today are 12 and 6?

  11. 11. Write the code

    The owner wants one row with three numbers: first how many orders the store has received, then how many different customers placed them, and then how many different products were ordered.

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →