What's the execute order of the different parts of a SQL select statement?

前端 未结 3 1900
猫巷女王i
猫巷女王i 2020-12-01 11:38

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,

相关标签:
3条回答
  • 2020-12-01 11:50

    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)

    0 讨论(0)
  • 2020-12-01 11:53

    Have a look at

    SQL SERVER – Logical Query Processing Phases – Order of Statement Execution

    1. FROM
    2. ON
    3. OUTER
    4. WHERE
    5. GROUP BY
    6. CUBE | ROLLUP
    7. HAVING
    8. SELECT
    9. DISTINCT
    10. ORDER BY
    11. TOP

    Also, for some good info see Logical Query Processing

    0 讨论(0)
  • 2020-12-01 12:02

    Visit https://msdn.microsoft.com/en-us/library/ms189499.aspx for a better explanation.

    The following steps show the logical processing order, or binding order, for a SELECT statement. This order determines when the objects defined in one step are made available to the clauses in subsequent steps. For example, if the query processor can bind to (access) the tables or views defined in the FROM clause, these objects and their columns are made available to all subsequent steps. Conversely, because the SELECT clause is step 8, any column aliases or derived columns defined in that clause cannot be referenced by preceding clauses. However, they can be referenced by subsequent clauses such as the ORDER BY clause. Note that the actual physical execution of the statement is determined by the query processor and the order may vary from this list.

    FROM

    ON

    JOIN

    WHERE

    GROUP BY

    WITH CUBE or WITH ROLLUP

    HAVING

    SELECT

    DISTINCT

    ORDER BY

    TOP

    0 讨论(0)
提交回复
热议问题