SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
The ORDER BY
clause in SQL is used to sort the result set of a query by one or more columns. It allows you to present your results in a more meaningful order, especially when the inherent order of rows in a database table is not guaranteed by most RDBMS systems.
SELECT column1, column2, ... FROM table_name ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;
ASC
: Ascending order (default if not specified).DESC
: Descending order.Order by a Single Column:
If you want to order a list of employees by their last names:
SELECT first_name, last_name FROM employees ORDER BY last_name;
By default, the order will be ascending. If you want the order to be descending:
SELECT first_name, last_name FROM employees ORDER BY last_name DESC;
Order by Multiple Columns:
To order by the department first and then by last name within each department:
SELECT department, first_name, last_name FROM employees ORDER BY department, last_name;
You can also mix the order directions:
SELECT department, first_name, last_name FROM employees ORDER BY department ASC, last_name DESC;
Order by Using Column Position:
Sometimes, it might be convenient to order by the position of the column in the SELECT
list rather than the column name:
SELECT first_name, last_name FROM employees ORDER BY 2; -- This orders by the second column (last_name) in ascending order
Order by Aggregate Function Results:
If you're using aggregate functions, you can order by the result of these functions:
SELECT department, COUNT(*) as num_employees FROM employees GROUP BY department ORDER BY num_employees DESC;
This query retrieves the number of employees in each department, ordering the departments by their employee counts in descending order.
The ORDER BY
clause always comes after the WHERE
and GROUP BY
clauses in a SQL statement.
In databases that support the LIMIT
or OFFSET-FETCH
clauses for pagination, the ORDER BY
clause typically comes before these pagination clauses.
It's always a good idea to provide an explicit order if the sequence of the results is important, as most RDBMS systems don't guarantee a specific order without an ORDER BY
clause.
Be careful when using the column position approach, as changes to the SELECT
list can inadvertently change the ordering of results if you're not careful. It's generally more readable and safer to use explicit column names.
How to use ORDER BY in SQL:
SELECT column1, column2 FROM example_table ORDER BY column1;
Sorting results in ascending and descending order:
ASC
for ascending order (default) and DESC
for descending order.SELECT column1, column2 FROM example_table ORDER BY column1 DESC;
ORDER BY with multiple columns in SQL:
SELECT column1, column2, column3 FROM example_table ORDER BY column1, column2;
Sorting NULL values in ORDER BY:
NULLS FIRST
or NULLS LAST
.SELECT column1, column2 FROM example_table ORDER BY column1 NULLS LAST;
ORDER BY and column aliases in SQL:
ORDER BY
clause.SELECT column1 AS alias1, column2 AS alias2 FROM example_table ORDER BY alias1;
ORDER BY with expressions and functions:
SELECT column1, column2 FROM example_table ORDER BY LEN(column2), column1 DESC;
ORDER BY and LIMIT for pagination in SQL:
ORDER BY
in combination with LIMIT
for result set pagination.SELECT column1, column2 FROM example_table ORDER BY column1 LIMIT 10;
ORDER BY and FETCH FIRST in SQL:
ORDER BY
with FETCH FIRST
for result set limitation.SELECT column1, column2 FROM example_table ORDER BY column1 FETCH FIRST 10 ROWS ONLY;
Sorting results by specific criteria in SQL:
SELECT column1, column2 FROM example_table ORDER BY CASE WHEN column1 < 5 THEN 1 WHEN column1 >= 5 AND column1 < 10 THEN 2 ELSE 3 END, column1 DESC;
Sorting alphanumeric data in SQL:
CAST
or CONVERT
.SELECT column1, column2 FROM example_table ORDER BY CAST(column1 AS INT), column2;
ORDER BY and GROUP BY in SQL:
GROUP BY
.SELECT column1, COUNT(*) FROM example_table GROUP BY column1 ORDER BY column1;
ORDER BY and HAVING clause in SQL:
HAVING
clause.SELECT column1, COUNT(*) FROM example_table GROUP BY column1 HAVING COUNT(*) > 1 ORDER BY COUNT(*) DESC;
ORDER BY and subqueries in SQL:
SELECT column1, column2 FROM example_table ORDER BY (SELECT MAX(column3) FROM another_table WHERE column1 = example_table.column1);