Ask Precise Questions · practice
Put Matching Items in Descending Order
The search now finds three suitable items, but the longest allowed loan period is not guaranteed to appear first. If Riley wants the most flexible option at the top, row order becomes part of the question rather than a detail to leave to chance.
Remove the semicolon after the WHERE search.sql, then add this line:
ORDER BY loan_days DESC;
The complete ending should read:
WHERE (category = 'tools' OR category = 'events')
AND loan_days <= 7
ORDER BY loan_days DESC;
Press Run. The seven-day rows should appear before the three-day step ladder:
TL-100 | Cordless drill | tools | 7
EV-101 | Folding table | events | 7
TL-220 | Step ladder | tools | 3
ORDER BY asks SQLite to arrange the completed result by a DESC means descending, so larger loan_days values come before smaller ones. Writing ASC, or leaving the direction out, would put the shortest period first because ascending is the default.
You can read the visible periods straight down to check the direction: 7, 7, 3. Every next value is equal to or smaller than the one above it. That is the evidence to look for after Run.
The cordless drill and folding table both have a seven-day period. Either may appear first here. Sorting by one value cannot decide between rows where that value is equal, and SQLite has not promised to preserve the order you happened to see earlier.
That tie is useful evidence. Your query has done exactly what you asked, but your question is not yet detailed enough to choose a single order for every row. We will settle ties in the next exercise. Here, only two things are required: the qualifying rows stay the same, and their loan periods never increase as you read downward.
Submit uses another catalog inserted in a different order. Put DESC directly after loan_days so your SQL states the direction instead of relying on IDs, insertion order, or arithmetic that happens to reverse a particular set of numbers.
You have turned row order into an explicit part of the answer. The longest qualifying period now rises to the top because the query says it should.
Task
Order the matching rows in search.sql by loan_days DESC. Keep the existing columns and filter unchanged.
Run the query, check that longer periods appear first, and submit it.