Sorting with ORDER BY
A database does not promise to return rows in any particular order. They may come back in the order they were stored today and in another order tomorrow. If the order matters to you, say so with ORDER BY.
Sorting by one column
ORDER BY goes at the end of the query, after WHERE if there is one.
SELECT name, price
FROM products
ORDER BY price;Result
| name | price |
|---|---|
| Notebook 200 pages | 60 |
| Ball Pen pack of 10 | 90 |
| LED Bulb 9 W | 110 |
| Geometry Box | 120 |
| Sunflower Oil 1 L | 145 |
| Toor Dal 1 kg | 160 |
| Tea 500 g | 280 |
| Wall Clock | 349 |
| Extension Board | 399 |
| Steel Bottle 1 L | 450 |
| Earphones | 499 |
| Basmati Rice 5 kg | 650 |
16 rows
Showing the first 12 of 16 rows.
The default is ascending order: smallest number first, A before Z, earliest date first. You may write ASC to say so, but nobody does.
Largest first
Add DESC, short for descending.
SELECT name, price
FROM products
ORDER BY price DESC;Result
| name | price |
|---|---|
| Pressure Cooker 3 L | 1450 |
| Power Bank | 1299 |
| Bedsheet Double | 899 |
| School Bag | 750 |
| Basmati Rice 5 kg | 650 |
| Earphones | 499 |
| Steel Bottle 1 L | 450 |
| Extension Board | 399 |
| Wall Clock | 349 |
| Tea 500 g | 280 |
| Toor Dal 1 kg | 160 |
| Sunflower Oil 1 L | 145 |
16 rows
Showing the first 12 of 16 rows.
Sorting text and dates
SELECT name, city
FROM customers
ORDER BY name;Result
| name | city |
|---|---|
| Arjun Mehta | Pune |
| Asha Verma | Ludhiana |
| Imran Ali | Delhi |
| Joy Thomas | Mumbai |
| Kabir Khan | Mumbai |
| Meena Gill | Amritsar |
| Neha Joshi | Jaipur |
| Priya Nair | Kochi |
| Ravi Kumar | Delhi |
| Sunita Rao | Chennai |
| Tara Singh | Ludhiana |
| Vikram Das | Kolkata |
12 rows
SELECT id, order_date, status
FROM orders
ORDER BY order_date DESC;Result
| id | order_date | status |
|---|---|---|
| 20 | 2026-10-04 | pending |
| 19 | 2026-09-17 | pending |
| 18 | 2026-09-01 | pending |
| 17 | 2026-08-11 | shipped |
| 16 | 2026-07-09 | delivered |
| 15 | 2026-06-21 | delivered |
| 14 | 2026-06-02 | delivered |
| 13 | 2026-05-19 | shipped |
| 12 | 2026-05-06 | delivered |
| 11 | 2026-04-18 | delivered |
| 10 | 2026-04-01 | delivered |
| 9 | 2026-03-22 | cancelled |
20 rows
Showing the first 12 of 20 rows.
The second query shows the newest orders first, which is how most order lists on websites are arranged.
Sorting by more than one column
List several columns. The result is sorted by the first. Rows that tie on the first are sorted by the second, and so on.
SELECT name, category, price
FROM products
ORDER BY category, price DESC;Result
| name | category | price |
|---|---|---|
| Power Bank | Electronics | 1299 |
| Earphones | Electronics | 499 |
| Extension Board | Electronics | 399 |
| LED Bulb 9 W | Electronics | 110 |
| Basmati Rice 5 kg | Grocery | 650 |
| Tea 500 g | Grocery | 280 |
| Toor Dal 1 kg | Grocery | 160 |
| Sunflower Oil 1 L | Grocery | 145 |
| Pressure Cooker 3 L | Home | 1450 |
| Bedsheet Double | Home | 899 |
| Steel Bottle 1 L | Home | 450 |
| Wall Clock | Home | 349 |
16 rows
Showing the first 12 of 16 rows.
The products are grouped by category in alphabetical order. Inside each category, the dearest comes first. Notice that DESC applies only to the column it follows. Each column in the list has its own direction.
SELECT name, department, salary
FROM employees
ORDER BY department, salary DESC, name;Result
| name | department | salary |
|---|---|---|
| Anita Kapoor | Management | 95000 |
| Rohit Bansal | Sales | 60000 |
| Deepa Iyer | Sales | 48000 |
| Manoj Yadav | Sales | 48000 |
| Harpreet Kaur | Support | 36000 |
| Nikhil Jain | Support | 33000 |
| Farah Sheikh | Warehouse | 42000 |
| Gopal Reddy | Warehouse | 30000 |
8 rows
The third column settles ties between people in the same department on the same salary.
Filtering, then sorting
WHERE comes before ORDER BY. The database first picks the rows, then sorts them.
SELECT name, price
FROM products
WHERE category = 'Home'
ORDER BY price;Result
| name | price |
|---|---|
| Wall Clock | 349 |
| Steel Bottle 1 L | 450 |
| Bedsheet Double | 899 |
| Pressure Cooker 3 L | 1450 |
4 rows
Sorting by a calculation or an alias
You can sort by something worked out. And unlike WHERE, ORDER BY can use an alias, because sorting happens after the columns have been named.
SELECT name, price * stock AS stock_value
FROM products
ORDER BY stock_value DESC;Result
| name | stock_value |
|---|---|
| Basmati Rice 5 kg | 26000 |
| Steel Bottle 1 L | 24750 |
| Power Bank | 23382 |
| Bedsheet Double | 22475 |
| LED Bulb 9 W | 22000 |
| Notebook 200 pages | 18000 |
| Pressure Cooker 3 L | 17400 |
| School Bag | 15000 |
| Extension Board | 13965 |
| Ball Pen pack of 10 | 13500 |
| Toor Dal 1 kg | 12000 |
| Wall Clock | 10470 |
16 rows
Showing the first 12 of 16 rows.
Sorting by a column you do not show
SELECT name
FROM employees
ORDER BY hired;Result
| name |
|---|
| Anita Kapoor |
| Rohit Bansal |
| Farah Sheikh |
| Harpreet Kaur |
| Deepa Iyer |
| Manoj Yadav |
| Gopal Reddy |
| Nikhil Jain |
8 rows
The names come back in order of joining date, although the date is not shown.
Sorting by position
You may give the number of a column in the SELECT list instead of its name. ORDER BY 2 means "sort by the second column".
SELECT name, salary
FROM employees
ORDER BY 2 DESC;Result
| name | salary |
|---|---|
| Anita Kapoor | 95000 |
| Rohit Bansal | 60000 |
| Deepa Iyer | 48000 |
| Manoj Yadav | 48000 |
| Farah Sheikh | 42000 |
| Harpreet Kaur | 36000 |
| Nikhil Jain | 33000 |
| Gopal Reddy | 30000 |
8 rows
It is quick to type when exploring. Avoid it in queries you keep, because adding a column to the list later silently changes what the number points to.
Where do NULLs go?
Rows with no value in the sort column are kept together at one end.
SELECT name, email
FROM customers
ORDER BY email;Result
| name | |
|---|---|
| Meena Gill | NULL |
| Arjun Mehta | NULL |
| Tara Singh | NULL |
| Asha Verma | asha@example.com |
| Imran Ali | imran@example.com |
| Joy Thomas | joy@example.com |
| Kabir Khan | kabir@example.com |
| Neha Joshi | neha@example.com |
| Priya Nair | priya@example.com |
| Ravi Kumar | ravi@example.com |
| Sunita Rao | sunita@example.com |
| Vikram Das | vikram@example.com |
12 rows
SQLite, MySQL and SQL Server put NULL first in ascending order. PostgreSQL and Oracle put it last. PostgreSQL, Oracle and SQLite let you choose with NULLS FIRST or NULLS LAST.
SELECT name, email
FROM customers
ORDER BY email NULLS LAST;Result
| name | |
|---|---|
| Asha Verma | asha@example.com |
| Imran Ali | imran@example.com |
| Joy Thomas | joy@example.com |
| Kabir Khan | kabir@example.com |
| Neha Joshi | neha@example.com |
| Priya Nair | priya@example.com |
| Ravi Kumar | ravi@example.com |
| Sunita Rao | sunita@example.com |
| Vikram Das | vikram@example.com |
| Meena Gill | NULL |
| Arjun Mehta | NULL |
| Tara Singh | NULL |
12 rows
How text is sorted
Text is compared character by character. Two things can surprise you.
- Case. In SQLite and PostgreSQL, capital letters sort before small letters by default, so "Zebra" comes before "apple". MySQL and SQL Server usually ignore case. In our data every name starts with a capital, so you will not notice.
- Numbers stored as text. If a column holds text such as '9', '10' and '100', it sorts as text: '10', '100', '9'. Numbers should be stored in number columns.
Ties have no fixed order
If two rows have the same value in every sort column, the database may return them in either order. When you need the same order every time, for example when showing results a page at a time, add a column that is unique, such as id, at the end of the ORDER BY.
SELECT name, salary
FROM employees
ORDER BY salary DESC, id;Result
| name | salary |
|---|---|
| Anita Kapoor | 95000 |
| Rohit Bansal | 60000 |
| Deepa Iyer | 48000 |
| Manoj Yadav | 48000 |
| Farah Sheikh | 42000 |
| Harpreet Kaur | 36000 |
| Nikhil Jain | 33000 |
| Gopal Reddy | 30000 |
8 rows
The order of the parts so far
SELECT, FROM, WHERE, ORDER BY. Sorting is nearly always last.
Try it yourself
Exercise 1. Show the name and salary of all employees, highest salary first.
Show one answer
SELECT name, salary FROM employees ORDER BY salary DESC;| name | salary |
|---|---|
| Anita Kapoor | 95000 |
| Rohit Bansal | 60000 |
| Deepa Iyer | 48000 |
| Manoj Yadav | 48000 |
| Farah Sheikh | 42000 |
| Harpreet Kaur | 36000 |
| Nikhil Jain | 33000 |
| Gopal Reddy | 30000 |
8 rows
Exercise 2. Show the name and city of all customers, sorted by city and then by name.
Show one answer
SELECT name, city FROM customers ORDER BY city, name;| name | city |
|---|---|
| Meena Gill | Amritsar |
| Sunita Rao | Chennai |
| Imran Ali | Delhi |
| Ravi Kumar | Delhi |
| Neha Joshi | Jaipur |
| Priya Nair | Kochi |
| Vikram Das | Kolkata |
| Asha Verma | Ludhiana |
| Tara Singh | Ludhiana |
| Joy Thomas | Mumbai |
| Kabir Khan | Mumbai |
| Arjun Mehta | Pune |
12 rows
Exercise 3. Show the name and stock of Grocery products, lowest stock first.
Show one answer
SELECT name, stock FROM products WHERE category = 'Grocery' ORDER BY stock;| name | stock |
|---|---|
| Tea 500 g | 0 |
| Basmati Rice 5 kg | 40 |
| Sunflower Oil 1 L | 60 |
| Toor Dal 1 kg | 75 |
4 rows
Examples on this page were run with SQLite 3.50.4 on 12 November 2026. The results shown are the real results.
This is an independent learning site. It is not connected with the makers of any database product. MySQL, PostgreSQL, SQL Server, Oracle and SQLite are named only so that you know where a piece of SQL works. The shop, its customers and its staff in the practice database are made up.