ORDER BY and Result Sorting in SQL
By Owen Middleton · Updated September 2026 · Examples run on PostgreSQL 17
Before this SELECT and Column Expressions, FROM and Table References
Builds toward LIMIT and OFFSET, Window Functions Introduction (OVER, PARTITION BY), DISTINCT ON, STRING_AGG and ARRAY_AGG
What is ORDER BY in SQL?
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.
How do you sort query results in SQL?
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.
How do you sort in descending order in SQL?
Add DESC to flip the direction:
SELECT name, price FROM products ORDER BY price DESC, id
Most-expensive-first. The id tiebreaker stays ascending by default, keeping the order predictable when prices match.
Can you ORDER BY a column alias in SQL?
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, nameWhere do NULLs sort in SQL?
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 ORDER BY in SQL
Brightlane's product team is preparing a catalogue sorted by price for a buyer presentation.
Write a query to return each product's name and price, sorted from the least expensive to the most expensive.
Assumptions:
- The
productstable contains every product in Brightlane's catalogue. - When two products share the same price, the product with the lower
idshould appear first.
Output:
- One row per product, with columns
nameandprice, sorted bypriceascending, then byidascending.
Schema · ecommerce5 tables? = nullable
Run previews · Check grades
Write a query, then run it to see results here.
The full breakdown walks through the shape, each clause, why this approach beats the alternatives, and the trap to avoid.
See the full worked solution9 ORDER BY 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.
Start learning to practice all 9 ORDER BY problems, with instant grading and mastery tracking.
Common questions about ORDER BY
Is row order guaranteed without an ORDER BY?
No. Without ORDER BY, PostgreSQL returns rows in whatever order is convenient for the plan it chose, and that can change as the data changes. If the order matters to whoever reads the result, say so with ORDER BY rather than trusting what you saw last time.
How do you sort by more than one column?
List the columns after ORDER BY separated by commas. PostgreSQL sorts by the first, then uses each later column only to break ties in the one before it. A second key is what makes the order of equal-priced rows stable instead of arbitrary.
Can you sort by a column you are not selecting?
Yes. ORDER BY can name any column in the table, whether or not it appears in the SELECT list, so returning product names ordered by price takes no extra column in the output.
Can ORDER BY use a column alias?
Yes, and it is one of the few places that can. Sorting is worked out after the SELECT list, so the alias exists by then. You can also sort by the position of a column, though a name survives edits to the SELECT list better than a number does.