Sorting Data Using the ORDER BY Clause

Previous
Previous
Next
Next

You can use SELECT with the ORDER BY clause to retrieve and display rows from a table ordered (sorted) by a specified column in the table. The specified column in the ORDER BY clause does not have to be in the SELECT list of columns that you want to display.

You can specify the sort order as ASC for ascending or DESC for descending. The default sort order is ascending, which means:

Null (empty) values are displayed last for ascending sequences and first for descending sequences.

Example: Selecting Data With the SQL ORDER BY Clause to Sort the Data shows how to use SELECT with the ORDER BY clause to retrieve and display rows from the employees table ordered (sorted) by specified columns.

Selecting Data With the SQL ORDER BY Clause to Sort the Data

-- the following retrieves rows with manager_id = 122 ordered by employee_id
-- the order is the default ascending order, lowest employee_id displays first
SELECT * FROM employees WHERE manager_id = 122 ORDER BY employee_id;

-- the following retrieves rows ordered by manager_id
-- the order is specified as descending, highest manager_id displays first
SELECT employee_id, last_name, first_name, manager_id FROM employees
      ORDER BY manager_id DESC;

See Example: Using SQL Aggregate Functions for the use of ORDER BY with the GROUP BY clause.


See Also:

Oracle Database SQL Reference for detailed information about using ORDER BY with SELECT