0%

Chapter 3 · practice

Ask Precise Questions

Select More Than One Column

The lending catalog can already answer “What are the item names?” with SELECT name FROM items;. A useful search result usually needs more context. If someone sees Projector, they may also need its asset tag, category, and usual loan period before they know whether it is the right item.

Open search.sql. Keep FROM items, but replace the single name after SELECT with these four names:

SELECT asset_tag, name, category, loan_days
FROM items;

The commas separate the requested columns. Their left-to-right order matters because SQLite uses the same order in the result. Here, the tag comes first, followed by the item name, category, and loan period.

Press Run. The first lines should look like this:

== search.sql ==
Columns: asset_tag | name | category | loan_days
EV-101 | Folding table | events | 7

The project also contains run_queries.py. You do not need to edit it. Each time you press Run, it creates a fresh six-item catalog, runs the exercise files that are present, and prints SQLite’s answers. The database disappears after the run, while your SQL files remain in the project.

This particular catalog prints all six items in ID order, but your query has not requested an order yet. For now, inspect the headings and values rather than treating the visible row order as a promise.

When you press Submit, your query runs against another catalog. It should read every stored row instead of spelling out the six answers you can see here. It must also ask for the four named columns rather than using *, because choosing the result deliberately is the skill you are practicing.

If you want to return to the starting files, use Reset project. That restores run_queries.py; returning here adds the starting search.sql when it is missing.

You began with one bare name. Your new result keeps each name beside the details that make it useful, and you decided exactly how those details line up.

Task

In search.sql, return asset_tag, name, category, and loan_days from items, in that order. Keep every stored item in the result.

Run the query and check the four headings, then submit it against another catalog.