The top few with LIMIT
LIMIT keeps only the first few rows of the result. Combined with ORDER BY, it answers "top
5" and "bottom 10" questions:
SELECT name, list_price
FROM products
ORDER BY list_price DESC
LIMIT 5This is the five most expensive products. LIMIT always comes last.
Always sort before you limit
LIMIT on its own just takes whichever rows come first, which isn't a meaningful "top 5". Use
ORDER BY with it every time.
In other databases
SELECT name, list_price FROM products ORDER BY list_price DESC LIMIT 5SELECT TOP 5 name, list_price FROM products ORDER BY list_price DESCTOP goes straight after SELECT. OFFSET 0 ROWS FETCH NEXT 5 ROWS ONLY after the ORDER BY works too.
SELECT name, list_price FROM products ORDER BY list_price DESC LIMIT 5SELECT name, list_price FROM products ORDER BY list_price DESC LIMIT 5The standard spelling, FETCH FIRST 5 ROWS ONLY, works here and in DuckDB too.
SELECT name, list_price FROM products ORDER BY list_price DESC LIMIT 5