0%

Ask Precise Questions · practice

Keep Only the First Few Rows

The search has a dependable first row, second row, and third row. Suppose the lending desk has space to show only two recommendations. Because you already defined the order, “the first two” now has one precise meaning.

Remove the semicolon after the ORDER BY line and add this final line:

LIMIT 2;

Press Run. SQLite should return the two rows at the front of the order and stop there:

Columns: asset_tag | name | category | loan_days
TL-100 | Cordless drill | tools | 7
EV-101 | Folding table | events | 7

LIMIT 2 does not decide which rows are best. The WHERE clause first decides which items qualify, and ORDER BY gives those matches their complete positions. LIMIT then keeps two rows from the beginning of that ordered result.

This sequence matters. A query with LIMIT 2 but no useful order would ask for any two matching rows SQLite happens to produce. That might look fine in one run and miss a better match in another catalog. Here, the longest allowed periods come first, names settle period ties, and IDs settle repeated-name ties before the limit is applied.

The number is a maximum. If a later catalog has only one qualifying item, SQLite returns one row. If no item qualifies, it returns no rows. The query does not invent replacements to fill the second position.

Submit includes more than two matching items and inserts the strongest candidates late. That prevents insertion order or small IDs from choosing the answer by accident. Keep the complete three-key order and add the limit after it.

If Run returns three rows, check that the semicolon moved to the end of LIMIT 2. A semicolon ends the SQL statement, so placing the new clause after the old semicolon would leave it outside the query.

Your search now returns a shortlist rather than a catalog dump: the same precise matches, arranged predictably, with only the first two kept.

Task

Add LIMIT 2 after the complete ORDER BY clause in search.sql. Keep the columns, filter, and all three sort keys unchanged.

Run the query and submit the two-row result.