>_ IzzyLab

Hands-on task

This task is step 4 of the course “SQL with SQLite”

SQLite: top 3 by price (ORDER BY + LIMIT)

easy~3 min

The database /root/shop.db has a table products(id, name, price). Output the 3 most expensive products — only their names, in descending order of price — and write the result to /root/out.txt (one name per line).

What you'll practise:
  • sqlite3
  • order by
  • limit
  • sorting
  • top-n

Topic breakdown

A "top-N" query is built from two clauses that work strictly in sequence: ORDER BY sorts all rows by a column, and LIMIT cuts off the required number from the top. Without sorting, LIMIT returns arbitrary rows — physical storage order is undefined. The direction is set by a modifier (ASC ascending, DESC descending); when values at the boundary are equal, the order between them is not guaranteed.

What's checked

  1. the result has exactly 3 lines
  2. the three most expensive products are selected
  3. products are in descending order of price

What it looks like