Sub topic 12
How Oracle Logically Processes a Query
This chapter assumes you understand tables, rows, columns, basic data types, and the normal order of clauses in a SELECT statement. The SQL text is written in one order, but Oracle reasons about its result in a different logical order. Knowing that order explains why some aliases work in ORDER BY but not in WHERE, and why filtering happens before sorting.
Written Order and Logical Order
A query normally appears in this order:
SELECT [DISTINCT] columns
FROM table
WHERE condition
ORDER BY expression
FETCH FIRST number ROWS ONLY;Oracle conceptually processes the clauses in this order:
| Logical step | Clause | Purpose |
|---|---|---|
| 1 | `FROM` | Choose the row source |
| 2 | `WHERE` | Remove rows that do not match |
| 3 | `SELECT` | Calculate and return selected expressions |
| 4 | `DISTINCT` | Remove duplicate result rows |
| 5 | `ORDER BY` | Sort the remaining rows |
| 6 | `FETCH` | Keep only the requested number of rows |
This is a model for understanding the result. The optimizer can use a different physical execution plan to improve performance. The logical result must still follow the meaning of the SQL statement.
Following a Query Step by Step
Consider this query:
SELECT DISTINCT department_id
FROM employees
WHERE department_id IS NOT NULL
ORDER BY department_id
FETCH FIRST 3 ROWS ONLY;Oracle can reason about it as follows:
FROM employeesprovides rows from theemployeestable.WHERE department_id IS NOT NULLremoves rows without a department.SELECT department_idkeeps only the department number.DISTINCTremoves repeated department numbers.ORDER BY department_idsorts the unique values.FETCH FIRST 3 ROWS ONLYreturns the first three sorted values.
The order matters. If the database limited rows before sorting, it could return three arbitrary departments instead of the three lowest department numbers.
Why Aliases Work in Some Clauses
A column alias is a name assigned by the SELECT list. Because WHERE is logically processed before SELECT, the alias does not exist when WHERE is evaluated.
This query uses the expression again in WHERE and uses the alias in ORDER BY:
SELECT employee_id,
salary * 12 AS annual_salary
FROM employees
WHERE salary * 12 > 100000
ORDER BY annual_salary DESC;annual_salary works in ORDER BY because sorting happens after the SELECT list has produced that name. Using the alias in WHERE would produce an invalid identifier error in this query.
The same idea applies to expressions evaluated against DUAL:
SELECT 2 + 3 AS total
FROM dual;DUAL supplies one row. Oracle evaluates the expression for that row, then returns one result with the heading TOTAL.
Common Errors
- Expecting
WHEREto see aSELECTalias. - Assuming
FETCH FIRSTchooses rows beforeORDER BY. - Applying
DISTINCTto one column while expecting duplicate combinations involving other unselected columns to matter. - Treating logical processing order as the execution plan. Use
EXPLAIN PLANwhen you need to study physical execution.
Exercise
In what logical order does Oracle process this query: SELECT DISTINCT lastname FROM employees WHERE departmentid = 50 ORDER BY last_name FETCH FIRST 5 ROWS ONLY?
Show answer
FROM, WHERE, SELECT, DISTINCT, ORDER BY, then FETCH.