參考資料docs.microsoft.com:Logical Processing Order of the SELECT statement
順序如下:
- FROM
- ON
- JOIN
- WHERE
- GROUP BY
- WITH CUBE or WITH ROLLUP
- HAVING
- SELECT
- DISTINCT
- ORDER BY
- TOP
有但書
Warning
The preceding sequence is usually true. However, there are uncommon cases where the sequence may differ.
For example, suppose you have a clustered index on a view, and the view excludes some table rows, and the view’s SELECT column list uses a CONVERT that changes a data type from varchar to integer. In this situation, the CONVERT may execute before the WHERE clause executes. Uncommon indeed. Often there is a way to modify your view to avoid the different sequence, if it matters in your case.
還有一個很屌的網頁(SQL Fiddle)可以玩玩看,要先create table,執行完SQL可以顯示圖形化結果