Search notes:

SQL: Order of select operations

The order is:
from to generate the data set
on / join
where to filter interesting records from the data set
Add pseudo column rownum Oracle only (See Select first n rows …)
group by To specify which rows need to be combined for aggregations
with cube / with rollup
aggregation functions to actually perform the calculations on the combined rows
having To exclude (aggregated) records
Window (analytic) functions to combine values from different records of the result set
select Specify what is actually returned.
distinct eliminate duplicates
Set operatiors (union, intersect, except etc.) to combine results from multiple select statements
Model clause Oracle only?
order by to sort the result set
offset to specify the first record that should be returned
limit, fetch, top to specify how many records should be returned
Because aggregation functions are performed after group by, they cannot appear in group by expressions.
Because window functions are performed after aggregation functions, they can use the result of aggregation functions.
Because column aliases are defined in the select step, they cannot be used in preceding steps (such as the where clause etc.)
An example that combines order by and union all is here.
ORA-03048: SQL reserved word … is not syntactically valid following …

Links

jooq: A Beginner’s Guide to the True Order of SQL Operations
This Stackoverflow answer

Index