skip to today's puzzle

ORDER BY sorts rows by the columns you choose

ORDER BY sorts your result rows based on the columns you choose. Without an ORDER BY clause, SQL guarantees no order at all. When you use ORDER BY, it defaults to ascending (smallest numbers first, or A to Z). Add DESC after a column name to sort descending instead (biggest numbers first, or Z to A). Sorting happens last, on whatever rows passed your filters.

A tiny example

In plain English: Show me every café’s name and rating, highest rating first.

SELECT name, rating
FROM cafes
ORDER BY rating DESC;

Query result

namerating
Sunny Cup5
Corner Bean4.8
Morning Mug4.6

Same rows as a plain SELECT, just sorted so the highest rating comes first.

Common questions

What is the default sort direction for ORDER BY?
Ascending (ASC): smallest to largest, A to Z, oldest to newest. Add DESC after the column to sort the other way.
Can I sort by more than one column?
Yes. List them in order, such as ORDER BY city, total DESC. Rows are sorted by city first, then by total within each city.

Related SQL lessons

Practice ORDER BY step by step

The Foundations Track has beginner puzzles built around this idea. Tap tiles, see real results. No terminal, no setup.

practice this in the course

Ready for a break?

A fresh, bite-sized SQL puzzle waits every morning. Tap tiles, see real results. No terminal, no setup, no timers.

Try today's free SQL puzzle