Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A restaurant stores its staff in a table Staff. Hours is the hours worked this week.
StaffID Name Role Hours
S1 Kai Cook 30
S2 Lea Waiter 18
S3 Moe Cook 12
S4 Nia Cook 25
S5 Oz Waiter 35A script should output the name and hours of every cook who worked more than hours, most hours first. It has errors.
SELECT Name Hours
FROM Staffs
WHERE Role = Cook AND > 20
ORDER BY DESC Hours;Find and correct every error, then give the output of the correct script.
To give the output of a script, work in this order: the table (FROM), the records that pass (WHERE), COUNT or SUM if there is one, the sort (ORDER BY), and last the selected fields (SELECT). To correct a script, check the clause order, the commas, the names, the quotes and each condition.
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 errors.
Name, Hours.Staff, not Staffs.'Cook'.AND Hours > 20.DESC comes after the field: ORDER BY Hours DESC.Step 2. The correct script and its output.
SELECT Name, Hours
FROM Staff
WHERE Role = 'Cook' AND Hours > 20
ORDER BY Hours DESC;The output:
Name Hours
Kai 30
Nia 25Answer: errors; the output has rows, Kai and then Nia .
Log in to join the discussion.
Loading…