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, idThe 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, nameWhen 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 9 ORDER BY and Result Sorting practice problems
Write a query to return each product's name and price, sorted from the least expensive to the most expensive.
Write a query to return each department's name and office location, listed alphabetically by department name.
Write a query to return each employee's name and hire date, ordered from most recently hired to earliest.
Write a query to return each customer's name, email, and country, grouped visually by country and then alphabetised within each country.
Write a query to return each product's name and stock value, sorted from highest stock value to lowest.
Write a query to return each product's name, category ID, and price.
Write a query to return each order's ID and total amount, ranked from largest order to smallest.
Write a query to return each customer's name and city.
Write a query to return each employee's name and job title, ordered from the most recently hired to the earliest.
These problems are part of the ORDER BY and Result Sorting lesson in SQLMaxx, with instant grading and a worked solution on each.
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.
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.
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.
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.
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 freeNo account, no credit card. Start solving in under a minute.