Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A school kitchen records each lunch order in a table Lunches. Hot says whether the meal is served hot.
LunchID Meal Hot Portions
L1 Pasta TRUE 3
L2 Salad FALSE 2
L3 Curry TRUE 4
L4 Wrap FALSE 1
L5 Stew TRUE 2The cook asks two questions: "How many orders were for hot meals?" and "How many portions of hot food must I make?" Write a script for each and give its output.
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 hot orders. L1, L3 and L5.
Step 2. The number of orders.
SELECT COUNT(*)
FROM Lunches
WHERE Hot = TRUE;The output:
COUNT(*)
3Step 3. The number of portions.
SELECT SUM(Portions)
FROM Lunches
WHERE Hot = TRUE;The output:
SUM(Portions)
9Check. portions.
Answer: hot orders and portions.
Log in to join the discussion.
Loading…