Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A café keeps its orders in a table Orders. Cost is in dollars.
OrderID Item Size Cups Cost
C1 Tea Small 2 6
C2 Soup Large 1 7
C3 Tea Large 3 12
C4 Juice Small 4 10
C5 Soup Small 2 8
C6 Tea Small 1 3Give the output of each script.
SELECT COUNT(*) FROM Orders WHERE Item = 'Tea';
SELECT SUM(Cups) FROM Orders WHERE Item = 'Tea';COUNT(*) counts the records that pass the WHERE condition. SUM(Field) adds up a numeric field of the records that pass. Each gives one value. "How many orders?" is a count; "how many cups?" is a sum of the Cups field.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Step 1. The tea orders. C1, C3 and C6.
Step 2. COUNT. There are of them, so the first script outputs .
Step 3. SUM. Their cups are , and , so the second script outputs .
Answer: and .
Log in to join the discussion.
Loading…