SELECT執行順序

來源:互聯網
上載者:User

最近準備資料庫DBA的面試,碰到考察select語句執行順序的問題,見附,回頭查詢了下王珊老師的《資料庫系統概論》,裡面給SELECT的定義格式為:
SELECT[ALL|DISTINCT]<目標列運算式>[,<目標列運算式>]…
FROM<表名或視圖名>[,<表名或視圖名>]…
[WHERE<條件運算式>]
[GROUP BY<列名1>][HAVING<條件運算式>]
[ORDER BY<列名2>][ASC|DESC]
文中其對SELECT語句的含義解釋為:根據where子句的條件運算式,從FROM子句指定的基本表或視圖中找出滿足條件的元組,再按Select子句中的目標列運算式,選出元組中的屬性值形成結果表。如果有GROUP子句,則將結果按<列名1>的值進行分組,該屬性列值相等的元組為一個組。通常會在每組中作用集合函式(sum,count,max,min等)。如果GROUP子句帶HAVING短語,則只有滿足指定條件的組才會輸出。如果有ORDER子句,則結果表還要按<列名2>的值的升序或降序排序。


我反覆體會了好幾遍,感覺這樣的敘述很容易讓人得出SELECT語句執行順序為:from,where,目標列運算式,group,使用聚集合函式計算,having,order的結論,但是這樣的執行順序總感覺特彆扭,與所體會的SELECT的用法不那麼匹配,通過網路調研,得到如下結果,特分享出來,希望對大家有所協助:
1、from子句組裝來自不同資料來源的資料;
2、where子句基於指定的條件對記錄行進行篩選;
3、group by子句將資料劃分為多個分組;
4、使用聚集合函式進行計算;
5、使用having子句篩選分組;
6、計算所有的運算式;
7、使用order by對結果集進行排序。


有了上述這樣的認識,我們來做附錄上的兩到SQL面試題,答案將很容易,分別是E,C
1)in a select statement that includes a where clause,where is the group by clause placed in the select statement?______。
A. immediately after the select clause
B. before the where clause
C. before the from clause
D. after the order by clause
E. after the where clause
2)in a select statement that includes a where clause,where is the order by clause placed in the select statement?______.
A.immediately after the select clause
B.before the where clause
C.after all clause
D.after the where clause
E.before the from clause 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.