sql · level 1 · unit 4 · lesson 2
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
The database can do your sums, row by row.
Put a in SELECT and the database works it out for every row. Use + and -, * to multiply and / to divide. Here, each snack's price is worked out for a pack of 3.
The top line of the result shows the calculation itself, like price * 3, because that column has no name of its own yet.
Watch out with /. These prices are integers: whole numbers. Divide one whole number by another and SQL throws the leftover away. Write a , like 100.0, to keep it.
SELECT name, price * 3 FROM snacks; 7 / 2 gives 3, but 7 / 2.0 gives 3.5.
guess first
Pence into pounds. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
SELECT name, price / 100 FROM snacks WHERE name = 'chocolate';
your turn
Your turn. Buy snacks for a party.
Type how many friends are coming in the blank. The query works out what each snack costs, in pence, if everyone gets one.
result
Your answer appears here as a table.
fix it
Half price lost half a penny. Can you fix it?
result
Your answer appears here as a table.
- Half of 95 is 47.5. Where did the .5 go?
- Both 95 and 2 are whole numbers, so SQL throws the leftover away.
- Change 2 to 2.0, then press run.
nailed it
you made the database do your sums. :D
- Put calculations in
SELECTwith+,-,*and/. - The database works the sum out for every row.
- Whole numbers divide to whole numbers. Use
2.0to keep decimals.
new words in your collection
- calculation
- integer
- decimal
This lesson isn’t saved. A free account remembers every lesson you finish.