What's the execute order of the different parts of a SQL select statement? Such as
distinct
from
order by
group by
having
multiline function(count, avg, max, min...)
top(sql server) or limit(mysql)
other parts
Does the different databases have the same execution order?
Great thanks.
Have a look at
SQL SERVER – Logical Query Processing Phases – Order of Statement Execution
- FROM
- ON
- OUTER
- WHERE
- GROUP BY
- CUBE | ROLLUP
- HAVING
- SELECT
- DISTINCT
- ORDER BY
- TOP
Also, for some good info see Logical Query Processing
The above answer addresses the question but there is one exception to the above mentioned order
when you have
select top n ............
order by
Then, order by will be executed before select. ( the entries are ordered first and then the top n entries are selected)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With