Tutorials › SQL Tutorial › SQL ORDER BY

SQL ORDER BY

8 min read · Lesson 7 of 24

A database makes no promise about the order of rows in a result. If you want the cheapest products first, the newest orders at the top, or customers in alphabetical order, you must ask for it with ORDER BY.

Syntax

SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC | DESC], column2 [ASC | DESC], ...;
  • ASC sorts in ascending order (A to Z, smallest to largest, oldest to newest). It is the default, so you can leave it out.
  • DESC sorts in descending order (Z to A, largest to smallest, newest to oldest).

Sorting ascending

List products from cheapest to most expensive:

Example SQL
SELECT name, price
FROM products
ORDER BY price;
Try it Yourself »

Sorting descending

Add DESC to reverse it, most expensive first:

Example SQL
SELECT name, price
FROM products
ORDER BY price DESC;
Try it Yourself »

Text is sorted alphabetically and dates in YYYY-MM-DD form sort chronologically. The most recent orders first:

Example SQL
SELECT order_id, order_date, status
FROM orders
ORDER BY order_date DESC;
Try it Yourself »

Sorting by several columns

When two rows have the same value in the first sort column, the second column decides between them, and so on. Sort customers by country, and within each country by city:

Example SQL
SELECT name, city, country
FROM customers
ORDER BY country, city;
Try it Yourself »

Each column can have its own direction. Products grouped by category (A to Z), with the most expensive first inside each category:

Example SQL
SELECT category, name, price
FROM products
ORDER BY category ASC, price DESC;
Try it Yourself »
Note: DESC applies only to the column directly before it. In ORDER BY category, price DESC, the category is still sorted ascending.

Sorting by a calculation

You can sort by an expression, even one that is not in the SELECT list. Products with the most stock value first:

Example SQL
SELECT name, price, stock
FROM products
ORDER BY price * stock DESC;
Try it Yourself »

Where ORDER BY goes

ORDER BY comes after WHERE. The database first filters the rows, then sorts what is left:

Example SQL
SELECT name, price
FROM products
WHERE category = 'Accessories'
ORDER BY price DESC;
Try it Yourself »

The full order of clauses you have learned so far is SELECT ... FROM ... WHERE ... ORDER BY .... Later you will add GROUP BY, HAVING and LIMIT to this sequence.

How NULL values sort

Where do empty (NULL) values go? It depends on the database. SQLite and MySQL put NULLs first in ascending order; PostgreSQL and Oracle put them last. Run this and look for Nyeri Stationers:

Example SQL
SELECT name, country
FROM customers
ORDER BY country;
Try it Yourself »
Tip: Standard SQL lets you choose with NULLS FIRST or NULLS LAST, e.g. ORDER BY country NULLS LAST. PostgreSQL, Oracle and recent SQLite versions support it; MySQL does not.

Sorting by column position

You may see ORDER BY 2 in other people's code. It means "sort by the second column in the SELECT list". It works, but it breaks silently if someone later changes the column list, so prefer column names.

Exercise

List customers by the date they joined, newest first.

SELECT name, joined FROM customers ORDER BY joined ;
Exercise

Sort products alphabetically by name.

SELECT name FROM products   name;
Sign in or create a free account to track progress, earn XP, badges and a certificate.