N007 Tier 1 · Foundations

ORDER BY and Result Sorting in SQL

ORDER BY controls the sequence of rows in a query's output. Without it, PostgreSQL makes no guarantee about the order in which rows are returned.

ORDER BY controls the sequence rows come back in.

Without it, SQL returns rows in whatever order it finds convenient. The database doesn't guarantee any particular sequence unless you ask for one, and that order can change between runs as data changes. The moment you need results in a specific order, you add ORDER BY.

This comes up constantly in analyst work. A sales report needs deals from largest to smallest. A customer list needs to be alphabetical. A log needs to be newest first. The underlying data doesn't care about any of that. ORDER BY is you imposing the sequence that matters for the purpose.

Write ORDER BY after FROM and name the column to sort by. SQL defaults to ascending order — smallest first for numbers, A to Z for text:

SELECT name, price
FROM products
ORDER BY price, id

The id after the comma is a tiebreaker. When two products share the same price, SQL uses id to break the tie and produce a consistent order. Multiple sort keys work left to right: sort by the first, use each additional column to resolve ties.

Add DESC to flip the direction:

Most-expensive-first. The id tiebreaker stays ascending by default, keeping the order predictable when prices match.

You can sort by a computed column. Give the expression an alias in SELECT and reference it by name in ORDER BY — this is one of the few places in a query where a SELECT alias can be used:

SELECT name, price * stock_qty AS stock_value
FROM products
ORDER BY stock_value DESC, name

When a sort column contains NULL values, SQL puts them at the end in ascending order and at the top in descending order by default. If you need NULLs in a specific position regardless of direction, NULLS LAST or NULLS FIRST makes it explicit:

SELECT name, city
FROM customers
ORDER BY city DESC NULLS LAST, id
Practice

9 ORDER BY and Result Sorting practice problems

These problems are part of the ORDER BY and Result Sorting lesson in SQLMaxx, with instant grading and a worked solution on each.

How you actually get good at SQL

Reading explains SQL. Writing it, over and over with instant feedback, is what makes you fluent.

That's the whole SQLMaxx loop: 600+ real problems, instant AI feedback, mastery you can actually see, and spaced review that won't let you forget.

A stack of SQL practice problem cards, the top card showing an employees table.
615 problems · 66 concepts

Real problems. Not toy examples.

615 hand-built problems spanning all 66 concepts, from basic SELECTs to window functions, built on real schemas and real business questions, the kind you'll actually get asked on the job. Enough reps to make SQL automatic.

A retro computer showing a SQL query marked correct with a green checkmark.
Instant AI feedback

Write a query. Know if it's right in one second.

No copying an answer and hoping it clicked. The AI grader checks your real query against real data, catches exactly what's wrong, and explains the fix in plain English, like a senior analyst reading over your shoulder on every problem.

A circular mastery progress dial filling from blue to green, the SQLMaxx diamond at its center.
Mastery tracking

Stop guessing whether you actually know it.

SQLMaxx tracks every concept and shows you what you've mastered and what's still shaky. Your skills fill in one concept at a time, so 'I think I get joins' becomes something you can prove.

A SQL query editor circled by a blue return arrow with a clock, scheduled to come back for review.
Spaced review

Learn it once. Keep it for good.

Most of what you learn this week fades by next week. So when a concept comes due for review, SQLMaxx hands you a fresh problem to solve from a blank editor, not a flashcard to re-read. A research-backed spaced-repetition algorithm (FSRS) times each return for right before you'd forget, so your SQL is still there months later, when the interview or the job actually needs it.

Practice, feedback, mastery, review. That's the loop that turns reading into real skill.

Start free

No account, no credit card. Start solving in under a minute.