Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. SQL Dictionary
  3. ORDER BY

ORDER BY

Since: SQL-92(1992)

A clause that sorts the retrieved results by the values of a specified column. You can sort in ascending or descending order, and sort by multiple columns.

Syntax

Sorts by the specified column in ascending order (ASC is the default and can be omitted).

SELECT column_name FROM table_name ORDER BY column_name ASC;

Sorts by the specified column in descending order.

SELECT column_name FROM table_name ORDER BY column_name DESC;

Sorts by multiple columns (if column1 has the same value, sorts by column2).

SELECT column_name FROM table_name ORDER BY column1 ASC, column2 DESC;

Syntax Reference

SyntaxDescription
ORDER BY column_nameSorts rows by the specified column. The default order is ascending (ASC).
ASCSorts in ascending order (small to large, A to Z, oldest to newest). This is the default when omitted.
DESCSorts in descending order (large to small, Z to A, newest to oldest).
ORDER BY col1, col2When multiple columns are specified, rows with the same value in col1 are further sorted by col2.
NULLS FIRSTPlaces NULL values at the beginning of the results (supported in PostgreSQL and others).
NULLS LASTPlaces NULL values at the end of the results (supported in PostgreSQL and others).

Sample Code

The following 'employees' table is used in the examples below.

employees id name department salary hire_date 1 Okabe Rintaro Future Gadget Lab 320000 2019-06-01 2 Makise Kurisu Future Gadget Lab 350000 2020-09-15 3 Shiina Mayuri Future Gadget Lab 260000 2021-04-01 4 Hashida Itaru Future Gadget Lab 280000 2022-07-10 4 rows in set

Sorts employees by salary in ascending order.

sample_order_by.sql
SELECT name, salary FROM employees ORDER BY salary ASC;
+---------------+--------+
| name          | salary |
+---------------+--------+
| Shiina Mayuri | 260000 |
| Hashida Itaru | 280000 |
| Okabe Rintaro | 320000 |
| Makise Kurisu | 350000 |
+---------------+--------+
4 rows in set

Sorts employees by salary in descending order (highest first).

sample_order_by.sql
SELECT name, salary FROM employees ORDER BY salary DESC;
+---------------+--------+
| name          | salary |
+---------------+--------+
| Makise Kurisu | 350000 |
| Okabe Rintaro | 320000 |
| Hashida Itaru | 280000 |
| Shiina Mayuri | 260000 |
+---------------+--------+
4 rows in set

Sorts by department name ascending, then by salary descending within the same department.

sample_order_by.sql
SELECT name, department, salary
FROM employees
ORDER BY department ASC, salary DESC;
+---------------+------------------+--------+
| name          | department       | salary |
+---------------+------------------+--------+
| Makise Kurisu | Future Gadget Lab | 350000 |
| Okabe Rintaro | Future Gadget Lab | 320000 |
| Hashida Itaru | Future Gadget Lab | 280000 |
| Shiina Mayuri | Future Gadget Lab | 260000 |
+---------------+------------------+--------+
4 rows in set

Sorts by hire date in descending order (most recently hired first).

sample_order_by.sql
SELECT name, hire_date FROM employees ORDER BY hire_date DESC;
+---------------+------------+
| name          | hire_date  |
+---------------+------------+
| Hashida Itaru | 2022-07-10 |
| Shiina Mayuri | 2021-04-01 |
| Makise Kurisu | 2020-09-15 |
| Okabe Rintaro | 2019-06-01 |
+---------------+------------+
4 rows in set

You can also sort by column position (index) in the SELECT clause. 3 refers to the third column in SELECT (salary in this example).

sample_order_by.sql
SELECT name, department, salary FROM employees ORDER BY 3 DESC;
+---------------+------------------+--------+
| name          | department       | salary |
+---------------+------------------+--------+
| Makise Kurisu | Future Gadget Lab | 350000 |
| Okabe Rintaro | Future Gadget Lab | 320000 |
| Hashida Itaru | Future Gadget Lab | 280000 |
| Shiina Mayuri | Future Gadget Lab | 260000 |
+---------------+------------------+--------+
4 rows in set

Database-Specific Syntax

The basic syntax of ORDER BY (ASC, DESC, and multiple column specification) works the same across major databases.

SELECT name, department, salary
FROM employees
ORDER BY department ASC, salary DESC;

How NULL values are sorted differs by database. In PostgreSQL and Oracle, you can explicitly control NULL placement using NULLS FIRST and NULLS LAST.

-- PostgreSQL / Oracle
SELECT name, salary FROM employees ORDER BY salary ASC NULLS LAST;

MySQL and SQL Server do not support NULLS FIRST / LAST. In MySQL, you can use the ISNULL() function or IS NULL to control NULL placement.

-- MySQL (place NULLs at the end)
SELECT name, salary FROM employees ORDER BY salary IS NULL ASC, salary ASC;

Notes

ORDER BY is placed at the end of a SELECT statement, after WHERE, GROUP BY, and HAVING. You can specify multiple sort columns, and they are applied in left-to-right priority.

When sorting a column that contains NULL values, MySQL treats NULL as the smallest value — placing it first in ascending order and last in descending order. In PostgreSQL, you can explicitly control this behavior with NULLS FIRST and NULLS LAST.

To limit the number of rows returned along with sorting, see 'LIMIT / OFFSET'.

If you find any errors or copyright issues, please .