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.
select column_one, column_two
from table_name
order by sort_column desc
limit 5Open the DuckDB playground with the matching dataset and query already filled in.
Schema · Garden ShopTable · products9 columns · 24 rows
One row per product, with price, cost, and inventory levels.
Show the product name and price for the five most expensive products.
- Columns: product_name, price.
- Rows: 5 products.
- The first product should be Fiddle-Leaf Fig at 32.00.
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 COUNTRelated
Write a first SELECT and read the returned rows.
Return only the columns a report actually needs.
Clause order and the shape of a SELECT query.
SELECT, WHERE, ORDER BY, and LIMIT at a glance.