Brightlane's front-end team is rebuilding the site navigation and needs to populate a category dropdown from the database.
Write a query to return every product category's ID and name.
Assumptions:
- The
categoriestable contains every product category in Brightlane's catalog. - Each category has an
idand aname.
Output:
- One row per category, with columns
idandname.
Schema · ecommerce 5 tables
Run previews · Check grades
Write a query, then run it to see results here.
Worked solution Try it yourself first
SELECT
id,
name
FROM
categories The shape
FROM categories reads the catalog's category list, and the two columns in the SELECT list produce exactly the value-and-label pair a UI dropdown needs to render.
Clause by clause
SELECT id, namereturns two columns per row: the identifier the dropdown sends back when a user picks an option, and the display name the user sees. That's the standard shape for any UI-driven control — an internal ID that the rest of the system uses, paired with a human-readable label.FROM categoriesreads every category the catalog defines. With noWHEREclause, the dropdown gets the full list — which is what the front-end needs, because hiding a category from the dropdown would silently break navigation to that part of the site.
Why this and not SELECT *
The dropdown only needs two columns. If the categories table carries more — a slug, a sort order, a parent reference — none of those go to the front-end; they'd just inflate the payload over the network without serving the UI. Returning the minimum the consumer needs is the right default any time a query feeds another system. The receiving code is built against a specific column contract, and tight column lists are how that contract stays explicit. A SELECT * query couples the front-end to whatever shape the table happens to have today, which makes both sides harder to change later.
You practiced returning a small two-column slice of a table — exactly what a UI dropdown or label-value pair needs. Picking the minimum columns the consumer needs is good query hygiene whenever a query feeds another system.