Skip to the content

Summarizing a part

SQL · Unit 5: Aggregates

How many customers does the store have in Denver? Until now, every aggregate summarized a whole table. An aggregate summarizes the rows that reach it, and which ones reach it is up to the WHERE.

SQL filters first and summarizes after. COUNT(*) no longer counts the table. It counts what the WHERE let through.

SELECT COUNT(*) AS in_denver
FROM customers
WHERE city = 'Denver';

Returns

in_denver
3

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. Predict the output

    There are 8 customers, and 3 are from Denver. Nobody knows Chloë's city. What does this query return?

  2. 2. Complete the code

    How many units has Luke, customer 2, ordered across all his orders? Fill in the blank to finish the condition.

  3. 3. Find the case that fails

    This query should give the average age of the Denver customers, counting only the ones with an age on file. Whoever wrote it didn't know about AVG and divided the sum by COUNT(*). Which of these changes makes it give the wrong average?

  4. 4. Put the lines in order

    How much is the warehouse stock in the Devices section worth? Put the query in order. One line doesn't belong.

  5. 5. Find the bug

    The store's average price is 201.57, and someone wants the list of what costs more than that. Here SQL answers misuse of aggregate function AVG(), which means AVG is being used where it doesn't belong. Which line has the problem?

  6. 6. Multiple choice

    Here this query returns a single row, with Headphones and a 2. What does that row tell you?

  7. 7. Find the bug

    Someone had the list of the three accessories with their prices, and wanted to add a column saying how many there are. Here the query runs, but the list now has a single product. Which line has the problem?

  8. 8. Put the lines in order

    Which is the most expensive product in Audio, with its name and its price? Put in order the query that returns its real row, without leaving a bare column next to an aggregate. One line doesn't belong.

  9. 9. Predict the output

    Brian is customer 8 and has never ordered anything. What does this query return?

  10. 10. Complete the code

    The sales report has one line per product with the units sold, and the Speaker's can't be blank. It has to say 0. Fill in the blank.

  11. 11. Find the case that fails

    The sign for the Audio section says how many units there are across all its products, and it has to say 0 when there aren't any. Which of these changes makes this query leave the sign blank?

  12. 12. Write the code

    Order 12 is dated January 20, 2026, and today is the 15th. Write a query that summarizes what's been sold up to today, today included, with three columns: how many orders there are so far, then how many units they add up to, and then the date of the last one.

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 →