Oracle数据库-where, group by, having, order by语句的执行顺序

select xxx_name, count(*) as counter  
from table_y  
where where_condition  
group by xxx_name  
having having_condition  
order by zzz

  当我们看到一个类似上述的包含了where, group by, having, order by等关键字的SQL时,我们要首先要知道其执行顺序是怎样的,才能判断出其所表达的含义;

    下面列出其执行顺序: 先连接from后的数据源(若有join,则先执行on后条件,再连接数据源)。
    1. 根据where子句选择行;
    2. 根据group by 子句组合行;
    3. 根据having子句筛选组;
    4. 根据order by子句中的分组函数的结果对组进行排序,order by必须使用分组函数或者使用Group by子句中指定的列;


猜你喜欢

转载自blog.csdn.net/dh2442897094/article/details/78137853