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

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

-- Sorts employees by salary in ascending order.
SELECT name, salary FROM employees ORDER BY salary ASC;

-- Sorts employees by salary in descending order (highest first).
SELECT name, salary FROM employees ORDER BY salary DESC;

-- Sorts by department name ascending, then by salary descending within the same department.
SELECT name, department, salary
FROM employees
ORDER BY department ASC, salary DESC;

-- Retrieves the top 5 employees with the most recent hire dates.
SELECT name, hire_date FROM employees ORDER BY hire_date DESC LIMIT 5;

-- You can also sort by column position (index) in the SELECT clause.
SELECT name, department, salary FROM employees ORDER BY 3 DESC;

Execution Result

-- Example result of: SELECT name, department, salary FROM employees ORDER BY department ASC, salary DESC;
-- +----------+------------+--------+
-- | name     | department | salary |
-- +----------+------------+--------+
-- | Jiro     | Sales      | 320000 |
-- | Taro     | Sales      | 300000 |
-- | Hanako   | Dev        | 350000 |
-- | Misaki   | Dev        | 280000 |
-- +----------+------------+--------+

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;

In SQL Server, you can achieve the same result using a CASE expression.

-- SQL Server (place NULLs at the end)
SELECT name, salary FROM employees
ORDER BY CASE WHEN salary IS NULL THEN 1 ELSE 0 END, 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 .