ShareYourGroupShareYourGroupSQL

Lesson 7 of 26 · see the practice database

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

nameprice
Notebook 200 pages60
Ball Pen pack of 1090
LED Bulb 9 W110
Geometry Box120
Sunflower Oil 1 L145
Toor Dal 1 kg160
Tea 500 g280
Wall Clock349
Extension Board399
Steel Bottle 1 L450
Earphones499
Basmati Rice 5 kg650

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

nameprice
Pressure Cooker 3 L1450
Power Bank1299
Bedsheet Double899
School Bag750
Basmati Rice 5 kg650
Earphones499
Steel Bottle 1 L450
Extension Board399
Wall Clock349
Tea 500 g280
Toor Dal 1 kg160
Sunflower Oil 1 L145

16 rows

Showing the first 12 of 16 rows.

Sorting text and dates

SELECT name, city
FROM customers
ORDER BY name;

Result

namecity
Arjun MehtaPune
Asha VermaLudhiana
Imran AliDelhi
Joy ThomasMumbai
Kabir KhanMumbai
Meena GillAmritsar
Neha JoshiJaipur
Priya NairKochi
Ravi KumarDelhi
Sunita RaoChennai
Tara SinghLudhiana
Vikram DasKolkata

12 rows

SELECT id, order_date, status
FROM orders
ORDER BY order_date DESC;

Result

idorder_datestatus
202026-10-04pending
192026-09-17pending
182026-09-01pending
172026-08-11shipped
162026-07-09delivered
152026-06-21delivered
142026-06-02delivered
132026-05-19shipped
122026-05-06delivered
112026-04-18delivered
102026-04-01delivered
92026-03-22cancelled

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

namecategoryprice
Power BankElectronics1299
EarphonesElectronics499
Extension BoardElectronics399
LED Bulb 9 WElectronics110
Basmati Rice 5 kgGrocery650
Tea 500 gGrocery280
Toor Dal 1 kgGrocery160
Sunflower Oil 1 LGrocery145
Pressure Cooker 3 LHome1450
Bedsheet DoubleHome899
Steel Bottle 1 LHome450
Wall ClockHome349

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

namedepartmentsalary
Anita KapoorManagement95000
Rohit BansalSales60000
Deepa IyerSales48000
Manoj YadavSales48000
Harpreet KaurSupport36000
Nikhil JainSupport33000
Farah SheikhWarehouse42000
Gopal ReddyWarehouse30000

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

nameprice
Wall Clock349
Steel Bottle 1 L450
Bedsheet Double899
Pressure Cooker 3 L1450

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

namestock_value
Basmati Rice 5 kg26000
Steel Bottle 1 L24750
Power Bank23382
Bedsheet Double22475
LED Bulb 9 W22000
Notebook 200 pages18000
Pressure Cooker 3 L17400
School Bag15000
Extension Board13965
Ball Pen pack of 1013500
Toor Dal 1 kg12000
Wall Clock10470

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

namesalary
Anita Kapoor95000
Rohit Bansal60000
Deepa Iyer48000
Manoj Yadav48000
Farah Sheikh42000
Harpreet Kaur36000
Nikhil Jain33000
Gopal Reddy30000

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

nameemail
Meena GillNULL
Arjun MehtaNULL
Tara SinghNULL
Asha Vermaasha@example.com
Imran Aliimran@example.com
Joy Thomasjoy@example.com
Kabir Khankabir@example.com
Neha Joshineha@example.com
Priya Nairpriya@example.com
Ravi Kumarravi@example.com
Sunita Raosunita@example.com
Vikram Dasvikram@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

nameemail
Asha Vermaasha@example.com
Imran Aliimran@example.com
Joy Thomasjoy@example.com
Kabir Khankabir@example.com
Neha Joshineha@example.com
Priya Nairpriya@example.com
Ravi Kumarravi@example.com
Sunita Raosunita@example.com
Vikram Dasvikram@example.com
Meena GillNULL
Arjun MehtaNULL
Tara SinghNULL

12 rows

How text is sorted

Text is compared character by character. Two things can surprise you.

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

namesalary
Anita Kapoor95000
Rohit Bansal60000
Deepa Iyer48000
Manoj Yadav48000
Farah Sheikh42000
Harpreet Kaur36000
Nikhil Jain33000
Gopal Reddy30000

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.

Solve it in the practice box

Show one answer
SELECT name, salary FROM employees ORDER BY salary DESC;
namesalary
Anita Kapoor95000
Rohit Bansal60000
Deepa Iyer48000
Manoj Yadav48000
Farah Sheikh42000
Harpreet Kaur36000
Nikhil Jain33000
Gopal Reddy30000

8 rows

Exercise 2. Show the name and city of all customers, sorted by city and then by name.

Solve it in the practice box

Show one answer
SELECT name, city FROM customers ORDER BY city, name;
namecity
Meena GillAmritsar
Sunita RaoChennai
Imran AliDelhi
Ravi KumarDelhi
Neha JoshiJaipur
Priya NairKochi
Vikram DasKolkata
Asha VermaLudhiana
Tara SinghLudhiana
Joy ThomasMumbai
Kabir KhanMumbai
Arjun MehtaPune

12 rows

Exercise 3. Show the name and stock of Grocery products, lowest stock first.

Solve it in the practice box

Show one answer
SELECT name, stock FROM products WHERE category = 'Grocery' ORDER BY stock;
namestock
Tea 500 g0
Basmati Rice 5 kg40
Sunflower Oil 1 L60
Toor Dal 1 kg75

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.