Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A town library stores its readers in a table Readers. Loans is the number of books each reader borrowed this year.
ReaderID Name Type Loans
R1 Ava Adult 5
R2 Ben Child 12
R3 Cleo Adult 15
R4 Dev Child 4
R5 Eli Adult 8The librarian wants the total number of loans made by the readers who are children or who borrowed more than books. Complete the script and give its output.
SELECT ________(Loans)
FROM Readers
WHERE Type = 'Child' ________ Loans > 10;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 first gap. A total of loans: SUM.
Step 2. The second gap. A child or a reader with more than loans: OR.
Step 3. The records that pass. Ben (Child, ), Dev (Child, ) and Cleo (Adult, ).
Step 4. The sum. .
Answer: SELECT SUM(Loans) FROM Readers WHERE Type = 'Child' OR Loans > 10; outputs .
Log in to join the discussion.
Loading…