Skip to content
/Chapter 3 · Select, Filter, and Sort
Lesson 3.7·garden_shop
Lesson 3.7

SQL ORDER BY and LIMIT

Sorting and limiting turns a long result into a focused top-N list.

SQL tables do not promise a useful row order by themselves. Use ORDER BY when the order matters.

Add DESC for largest-to-smallest or newest-to-oldest sorting. Then use LIMIT when you only need the first few rows.

Pattern
select column_one, column_two
from table_name
order by sort_column desc
limit 5
Ready to run it?

Open the DuckDB playground with the matching dataset and query already filled in.

Schema · Garden ShopTable · products9 columns · 24 rows
Table · products

One row per product, with price, cost, and inventory levels.

9 columns · 24 rows
product_id intproduct_name textcategory_id intsupplier_id intprice decimalcost decimalquantity_on_hand intreorder_level intdiscontinued bool
Your task

Show the product name and price for the five most expensive products.

SQL Workbench
query.sqlgarden_shop · SQL engine loading
⌘↵ to run
·
Expected answer
  • Columns: product_name, price.
  • Rows: 5 products.
  • The first product should be Fiddle-Leaf Fig at 32.00.
← Previous · 3.6 LIKE & pattern matching
✓ Chapter 3 complete

You finished “Select, Filter, and Sort.”

Nice work. Ready to start the next one?

Start Chapter 4: Aggregations and Grouping →Begins with 4.1 Counting rows with COUNT