先执行from关键字后面的语句,明确数据的来源,它是从哪张表取来的。 再进行on的过滤。 之后join, 这样就避免了两个大表产生全部数据的笛卡尔积的庞大数据。 接着执行where关键字后面的语句,对数据进行筛选。 再接着执行group by后面的语句,对数据进行分组分类。 然后执行select后面的语句,也就是对处理好的数据,具体要取哪一部分。 最后执行order by后面的语句,对最终的结果进行排序。 最后limit限制数据条数。
from-where-groupby-having-select-orderby-limit 这就是一条基本sql的执行顺序。
一、查询的逻辑执行顺序 (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 (8) SELECT (9) DISTINCT (11) top_specification select_list (9) ORDER BY order_by_list
标准的 SQL 的解析顺序为: (1) FROM 子句 组装来自不同数据源的数据 (2) WHERE 子句 基于指定的条件对记录进行筛选 (3) GROUP BY 子句 将数据划分为多个分组 (4) 使用聚合函数进行计算 (5) 使用HAVING子句筛选分组 (6) 计算所有的表达式 (7) 使用ORDER BY对结果集进行排序
from(包括join 和left join 以及right join ) where group by having select order by
1.from 2.join on 或 lateral view explode(需炸裂的列) tbl as 炸裂后的列名 3.where 4.group by (开始使用select中的别名,从group 开始往后都可用) 5.聚合函数 如Sum() avg() count(1)等 6.having 7.select 中若包含over()开窗函数,执行完非开窗函数后select等待执行完开窗函数,然后执行select完,开窗函数通过表数据进行分区和排序,跟select查询中的字段是平行关系,不依赖查询字段。 8.distinct 9.order by
8.select 9.distinct<select_list> 1.from<left_table> 3.<join_type>join<right_table> 2.on<jon_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> 11.limit<limit_number>
1.from 2.on 3.join 4.where 5.group by 6.sum、count、max、avg 7.having 8.select 9.distinct 10.order by 11.limit