The default sort order, for the data returned by a SQL
query, is ascending. In this sort order:
·
numeric values are displayed with the lowest values first; for
example, 1 to 999
·
date values are displayed with the earliest value first; for
example, 01-JAN-92 before 01-JAN-95
·
character values are displayed in the alphabetical order; for
example, "A" first and "Z" last
·
null values are displayed last for ascending sequences and first
for descending sequences
·
you can also sort by a column that is not in the SELECT list
Here are some examples that depict sorting:
- To reverse the order in which the rows are displayed, specify the DESC keyword after the
column name in the ORDER
BY clause. This sample
code sorts the result by the most recently hired employee.
SELECT last_name, job_id, department_id, hire_date
FROM employees
ORDER BY hire_date DESC ; - You can also use a column alias in the ORDER BY clause. This sample
code sorts the data by annual salary.
SELECT employee_id, last_name, salary*12 annual
FROM employees
ORDER BY annual ; - You can sort query results by specifying the numeric position of
the column in the SELECT clause. This sample
code sorts the result by department_id, as this column is at
the third position in the SELECT clause.
SELECT last_name, job_id, department_id, hire_date
FROM employees
ORDER BY 3; - You can sort query results by more than one column. The sort limit
is the number of columns in the given table. In the ORDER BY clause, you specify the
columns and separate the column names using commas. If you want to reverse the
order of a column, specify DESC after its name, as in
this sample code.
SELECT last_name, department_id, salary
FROM employees
ORDER BY department_id, salary DESC;
No comments:
Post a Comment