sql · level 1 · unit 5 · lesson 1
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
tells you how many rows there are.
So far every query has handed back rows. COUNT(*) is different: it counts the rows and hands back one number. Ask how many pets there are and you get 6.
COUNT is an : a function that squashes many rows into one answer. The * inside the brackets means count every row.
Add WHERE and it counts only the rows that pass, like how many pets are cats. Give the answer a name with AS to keep the top line tidy.
SELECT COUNT(*) AS pets FROM pets; WHERE does its sieving before the counting starts, so only the rows that pass get counted.
guess first
Counting cats. What comes back?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
SELECT COUNT(*) AS cats FROM pets WHERE species = 'cat';
your turn
Your turn. How many films are newer?
The films came out between 2017 and 2023. Type a year in the blank to count how many films came out after it.
result
Your answer appears here as a table.
fix it
Four cats and dogs, but it says 0. Fix it!
result
Your answer appears here as a table.
- No pet can be a cat and a dog at the same time. How many rows pass this test?
- You want rows that are a cat, or rows that are a dog. Passing one test is enough.
- Change AND to OR, then press run.
nailed it
you counted a whole table in one go. :D
COUNT(*)counts rows and hands back one number.- Add
WHEREto count only some rows. - An aggregate squashes many rows into one answer.
new words in your collection
- COUNT
- aggregate
This lesson isn’t saved. A free account remembers every lesson you finish.