首页 > 技术文章 > MS SQL 中 SQL 执行顺序

howtrace 2019-05-10 10:24 原文

 

     通常一个 SQL 执行 会拆开若干步骤依次进行,不同的执行顺序,其结果有差异。 整理了 MS SQL   select , top , distinct , where  , group by , order by  执行顺序。

 

 

文字描述:

--8)  SELECT (9) DISTINCT (11) <TOP_specification> <select_list> 
--(1)  FROM <left_table> 
--(3)    <join_type> JOIN <right_table> 
--(2)      ON <join_condition> 
--(4)  WHERE <where_condition> 
--(5)  GROUP BY <group_by_list> 
--(6)  WITH {CUBE | ROLLUP} 
--(7)  HAVING <having_condition> 
--(10) ORDER BY <order_by_list> 

 

通俗描述:

先联表-> 过滤(where) -> 分组(group by )-> HAVING  ->  抽取字段(select ) -> 去重(distinct) -> 排序(order by ) -> 截取前N条记录(top)

 

推荐阅读