Sub topic 9
Sorting Results with `ORDER BY`
This chapter assumes you understand tables, rows, columns, and basic SELECT statements. You should also be able to connect to an Oracle Database session and recognize numbers, strings, and dates.
A query does not promise a particular row order unless it contains an ORDER BY clause. Oracle may return rows in a different order after an index change, a statistics update, or a different execution plan.
The basic sorting model
ORDER BY sorts the rows produced by a query. It is normally the last query clause, after WHERE, GROUP BY, and HAVING.
SELECT employee_id, first_name, last_name, salary
FROM employees
WHERE department_id = 50
ORDER BY salary DESC, last_name ASC;This query first keeps employees in department 50. It then sorts those employees by salary from highest to lowest. If two employees have the same salary, Oracle sorts those tied rows by last name from A to Z.
ASC means ascending order and is the default. DESC means descending order.
| Expression | Meaning |
|---|---|
| `ORDER BY salary` | Salary from low to high |
| `ORDER BY salary DESC` | Salary from high to low |
| `ORDER BY last_name ASC` | Last name from A to Z |
| `ORDER BY salary DESC, employee_id` | Salary descending, then employee ID ascending |
You can sort by a selected column, an expression, or a column alias.
SELECT first_name || ' ' || last_name AS full_name,
salary * 12 AS annual_salary
FROM employees
ORDER BY annual_salary DESC, full_name;The alias annual_salary names the calculated expression. Oracle can use that alias in ORDER BY, which often makes the statement easier to read.
Multiple sort keys and stable results
Oracle applies sort expressions from left to right. The second expression matters only when rows have equal values for the first expression.
Add a unique column as the final sort key when the order must be repeatable:
SELECT employee_id, last_name, salary
FROM employees
ORDER BY salary DESC, employee_id;The employee ID breaks ties between employees with the same salary. This matters when an application displays pages of results or compares two query results.
NULL values
NULL means that a value is missing or unknown. Oracle places NULL values last by default when sorting ascending and first by default when sorting descending. You can state the desired behavior explicitly with NULLS FIRST or NULLS LAST.
SELECT first_name, last_name, commission_pct
FROM employees
ORDER BY commission_pct DESC NULLS LAST;This asks for the highest commission percentages first and places employees without a commission at the end.
Common errors
- Omitting
ORDER BYand assuming the current display order is permanent. - Sorting by a column that is not unique and expecting tied rows to have a fixed order.
- Writing
ORDER BYbeforeWHERE. The correct order isFROM,WHERE, thenORDER BY. - Sorting a number stored as text and expecting numeric order. Text values such as
'100'and'20'sort alphabetically, not numerically. - Using a column alias that is not defined in the
SELECTlist. - Relying on ordinal positions such as
ORDER BY 2. This works, but changing the selected columns can silently change the meaning.
For a quick test, DUAL supplies one row, so sorting does not change the result:
SELECT 'Oracle' AS product, 2 + 3 AS total
FROM dual
ORDER BY total DESC;The ORDER BY clause controls presentation. It does not change the stored rows.
Exercise
Write a query that returns employee_id, last_name, and salary from employees, showing the highest salaries first. For equal salaries, show the lower employee ID first, and put any NULL salaries last.
Show answer
SELECT employeeid, lastname, salary FROM employees ORDER BY salary DESC NULLS LAST, employee_id ASC;