Brightlane's promotions team is featuring entry-level products in the weekly newsletter and wants the three lowest-priced items.
Write a query to return the ID, name, and price of the 3 cheapest products.
Assumptions:
- The
productstable contains every product in Brightlane's catalogue. - When two products share the same
price, the product with the loweridtakes priority.
Output:
- One row per product, with columns
id,name, andprice, sorted bypriceascending (andidascending for ties), capped at 3 rows.
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,
price
FROM
products
ORDER BY
price
LIMIT
3 The shape
Sorting price ascending puts the cheapest products at the top of the result, and LIMIT 3 cuts the output at the three rows the promotions team needs for the newsletter.
Clause by clause
SELECT id, name, pricepicks the three columns the newsletter slot displays.FROM productsreads every product in the catalogue. The bottom-three filter happens via sort plus cap, not via aWHEREclause.ORDER BY pricesorts ascending by default — smallest first. Picture Frame 8x10 and HDMI Cable 2m both sit at12.99; the tie resolves withid60 ahead ofid49, matching the prompt's note that loweridtakes priority.LIMIT 3caps the output at three rows. PostgreSQL completes the sort, hands back the first three, and stops.
Why this and not ORDER BY price DESC LIMIT 3
DESC would put the most expensive products at the top. That's the right shape for a "top three" query — the wrong shape here. "Cheapest" means smallest price first, which is what ascending order produces. The cap is the same; what flips between top-N and bottom-N is the direction of the sort, not the LIMIT.
You practiced combining ORDER BY ASC with LIMIT to fetch the bottom-N rows. The recurring shape: sort ascending so the smallest values surface first, then cap with LIMIT — the mirror image of "top N" via DESC + LIMIT.