[SQL Basics] The order of GROUP BY and WHERE HAVING ORDER BY in SQL execution order

Group query

SELECT 字段列表 FROM 表名 [ WHERE 条件 ] GROUP BY 分组字段名 [ HAVING 分组后过滤条件 ];

Query the employees whose age is less than 45, and group them according to the work address to obtain the work addresses with the number of employees greater than or equal to 3

select workaddress, count(*) address_count from emp where age < 45 group by workaddress having address_count >= 3;

The order of GROUP BY and WHERE HAVING ORDER BY

SQL execution order

Writing order: select–from–where–group by–having–order by --limit

Execution order: from—where—group by—having—select—order by—limit

insert image description here

from:需要从哪个数据表检索数据 
where:过滤表中数据的条件 
group by:如何将上面过滤出的数据分组 
having:对上面已经分组的数据进行过滤的条件 
select:查看结果集中的哪个列,或列的计算结果 
order by :按照什么样的顺序来查看返回的数据 

where is executed first, then groupby is grouped

The GROUP BY clause must appear after the WHERE clause and before the ORDER BY clause
insert image description here

group by is grouped first, and having is executing

1. The column name in the SELECT clause must be a grouping column or column function, because SELECT has the columns contained in group by

2. having must be used together with group by and after group by

3. The order of use of group by, having, order by: group by, having, order by

SELECT *|字段列表 [as 别名] FROM 表名 [WHERE 子句] [GROUP BY 子句][HAVING 子句][ORDER BY 子句][LIMIT 子句]

Except for aggregate calculation statements, each column in the SELECT statement must be in the GROUP BY sub

insert image description here

The difference between having and where

(1) having must be run after the group by operation is executed, having must be used together with group by, and after group by

(2) The execution order of where is before group by, and must be before having.

(3) Aggregation functions can be added after having, but functions such as count(), sum(), avg(), max(), min() and other functions cannot be added after where.

(4) The timing of execution is different: where is to filter before grouping, if the where condition is not met, it will not participate in grouping; while having is to filter the results after grouping.

(5) The judgment conditions are different: where cannot judge the aggregation function, but having can.

(6) Execution order: where > aggregate function > having

Common aggregate functions:

insert image description here

order by sorting instance

order by A,B        # A 升序排列 B也升序排列
order by A desc,B   # A 降序(优先),B 升序排列
order by A ,B desc  # A 升序(优先),B 降序排列

Case combat

SQL100 Another way to determine the best customer (2)

Return the customer name and total amount (order_num in the OrderItems table) whose order total price is not less than 1000

Hint: A sum needs to be calculated (item_price multiplied by quantity). To sort results by total, use INNER JOIN syntax

SQL writing order: group by–having–order by

select
    c.cust_name,
    sum(os.item_price * os.quantity) total_price
from
    OrderItems os
    INNER JOIN Orders o on o.order_num = os.order_num
    INNER JOIN Customers c on c.cust_id = o.cust_id
GROUP BY
    cust_name
HAVING
    total_price >= 1000
ORDER BY
    total_price

Guess you like

Origin blog.csdn.net/weixin_42694422/article/details/129624454