Sub topic 8
Where analytic functions are allowed and when they run
This chapter assumes you can write an analytic function with an OVER clause, including PARTITION BY and ORDER BY. It also assumes you know the logical order of WHERE, GROUP BY, and HAVING, and that you can write an inline view. Here you learn two things. The first is which clauses may contain an analytic function. The second is when Oracle computes it, which decides what rows the function sees.
The two places analytic functions can appear
Oracle accepts an analytic function in only two places in a query block:
- The
SELECTlist. - The final
ORDER BYclause.
It rejects them everywhere else. The table summarizes the common cases.
| Clause | Analytic function allowed? | Typical outcome |
|---|---|---|
| `SELECT` list | Yes | Computed once per row |
| `ORDER BY` | Yes | Sorts by the computed value |
| `WHERE` | No | ORA-30483: window functions are not allowed here |
| `GROUP BY` | No | Error |
| `HAVING` | No | Error |
| `JOIN ... ON` | No | Error |
The query below is a common first attempt at "the top three earners per department". It fails.
SELECT department_id, employee_id, salary
FROM employees
WHERE ROW_NUMBER() OVER (
PARTITION BY department_id
ORDER BY salary DESC
) <= 3;Oracle returns ORA-30483: window functions are not allowed here. The restriction follows from the order of evaluation, which the next section describes.
The order of evaluation
The WHERE clause decides which rows are in the result. An analytic function needs to see a whole set of rows, such as every row in a partition. If it ran inside WHERE, the filter would depend on rows the filter has not yet chosen. Oracle avoids this circularity by running analytic functions after the row-filtering steps.
Within a single query block, the logical order is:
FROMand joins.WHERE.GROUP BY.HAVING.- Analytic functions in the
SELECTlist. DISTINCT.- The final
ORDER BY.
Oracle may run the steps physically in another order, but the result must match this logical order. Two practical consequences follow.
- The analytic function sees only rows that survived
WHERE,GROUP BY, andHAVING. - Nothing that runs before step 5 can refer to the analytic result.
Filtering with an inline view
To filter on an analytic result, compute it in an inner query and filter in the outer query. The outer WHERE runs after the inner query has produced its numbers.
SELECT department_id, employee_id, salary, rn
FROM (
SELECT department_id,
employee_id,
salary,
ROW_NUMBER() OVER (
PARTITION BY department_id
ORDER BY salary DESC, employee_id
) AS rn
FROM employees
)
WHERE rn <= 3
ORDER BY department_id, rn;The inner block computes rn for every employee. The outer block treats rn as an ordinary column and filters on it. The same pattern works for RANK, DENSE_RANK, LAG, and LEAD. For example, you could keep only the rows where salary is above the LAG value.
A WITH clause (subquery factoring) does the same job and is often easier to read when the query has several steps.
WITH numbered AS (
SELECT department_id,
employee_id,
salary,
ROW_NUMBER() OVER (
PARTITION BY department_id
ORDER BY salary DESC, employee_id
) AS rn
FROM employees
)
SELECT department_id, employee_id, salary, rn
FROM numbered
WHERE rn <= 3
ORDER BY department_id, rn;A WHERE filter changes what the function sees
Because WHERE runs first, adding a condition to the same query block changes the analytic results. It does not just hide rows after the fact. Compare these two queries.
-- Filter applied after ranking
SELECT department_id, employee_id, salary, rnk
FROM (
SELECT department_id,
employee_id,
salary,
RANK() OVER (
PARTITION BY department_id
ORDER BY salary DESC
) AS rnk
FROM employees
)
WHERE salary > 5000;
-- Filter applied before ranking
SELECT department_id,
employee_id,
salary,
RANK() OVER (
PARTITION BY department_id
ORDER BY salary DESC
) AS rnk
FROM employees
WHERE salary > 5000;In the first query, ranks are computed over all employees, and employees under 5000 are removed afterwards. The ranks of the remaining employees are unchanged. In the second query, the low earners are removed first, so ranking happens only among employees above 5000. For RANK over a descending salary order the visible ranks usually come out the same, because the removed rows rank below the kept ones. The difference shows up with ROW_NUMBER over an ascending order, or with LAG and LEAD. In those cases the neighbour of a row can be a row the filter would have removed.
The LAG case is the clearest one. Suppose monthly_sales has rows for January through June, and you add WHERE sales_month >= DATE '2026-03-01' to the same block as LAG(amount). The March row now has no previous row, so its LAG value is NULL, even though February exists in the table. If you want the March change from February, compute LAG in an inner query and filter the months in the outer query.
Analytic functions on top of GROUP BY
Analytic functions run after GROUP BY and HAVING, so they can use aggregate results. Inside the OVER clause and in the function arguments, you can refer to grouped columns and to aggregate expressions.
SELECT department_id,
SUM(salary) AS dept_total,
RANK() OVER (ORDER BY SUM(salary) DESC) AS dept_rank
FROM employees
GROUP BY department_id
ORDER BY dept_rank;The grouping step produces one row per department. RANK then ranks those department rows by their totals. The same rule limits what you can write. You can use SUM(salary) or department_id in the OVER clause, but not a bare salary column, because after grouping a single row no longer has one salary.
Analytic functions in ORDER BY
The final ORDER BY is the one clause that runs after analytic functions, so it may contain one directly or through a select-list alias.
SELECT employee_id, department_id, salary
FROM employees
ORDER BY ROW_NUMBER() OVER (
PARTITION BY department_id
ORDER BY salary DESC, employee_id
),
department_id;This lists each department's top earner first, then each department's second earner, and so on. It is useful for round-robin style listings. The window ORDER BY inside OVER and the final ORDER BY are separate. The first controls how the function numbers rows. The second controls how the finished rows are returned.
Other restrictions
- Analytic functions cannot be nested directly inside another analytic function's
OVERclause. To use one result as input to another, compute the first in an inline view. DISTINCTruns after the analytic step. A column such asROW_NUMBER() OVER (...)can make otherwise identical rows distinct, soSELECT DISTINCTwill not remove them.FETCH FIRST n ROWS ONLYlimits the whole result, not each partition. For top-N per group you still need the inline view pattern.
Exercise
A query needs the department with the second-highest total salary. A colleague wrote SELECT department_id FROM employees GROUP BY department_id HAVING RANK() OVER (ORDER BY SUM(salary) DESC) = 2;. Why does it fail, and how do you fix it?
Show answer
HAVING is evaluated before analytic functions, so Oracle rejects RANK() OVER (...) there. Compute the rank in an inline view and filter in the outer query: SELECT department_id FROM (SELECT department_id, RANK() OVER (ORDER BY SUM(salary) DESC) AS dept_rank FROM employees GROUP BY department_id) WHERE dept_rank = 2;
Exercise
A table monthly_sales(sales_month, amount) holds twelve months. You want the change from the previous month, but only for the last three months. Where do you put the sales_month filter, and why?
Show answer
Put it in an outer query. Compute amount - LAG(amount) OVER (ORDER BY sales_month) in an inline view over all twelve months, then filter sales_month in the outer WHERE. A filter in the same block as LAG would run first and leave the earliest remaining month with a NULL previous value.