2 Day Developer > Using SQL > Retrieving Data With Queries > Sorting Data Using the ORDE...
Sorting Data Using the ORDER BY Clause |
Previous |
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:
Numeric values are displayed with the lowest values first, such as 1 to 999.
Character values are displayed in alphabetical order, such as A first and Z last.
Date values are displayed with the earliest value first, such as 01-JUN-93 before 01-JUN-95.
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.