Sorting with ORDER BY and LIMIT

Stage 1: Basics · Lesson 3 of 17 · 5 min read · free, interactive

Without ORDER BY, a database may return rows in any order. To get a guaranteed order, sort explicitly:

SELECT name, level FROM players ORDER BY level DESC;

ASC (smallest first) is the default; DESC reverses it. When two rows tie, add more sort columns to break the tie:

SELECT name, level FROM players ORDER BY level DESC, name ASC;

LIMIT keeps only the first rows of the sorted result — perfect for "top 3" questions. OFFSET skips rows first.

SELECT name, level FROM players ORDER BY level DESC, name LIMIT 3;

Tip: Always pair LIMIT with ORDER BY. "The first 3 rows" means nothing without a sort order.

Practice

Each answer runs in your browser and is checked on hidden data.

Previous: Filtering rows with WHERE · Next: Removing duplicates with DISTINCT · All lessons