Similarities and differences between WHERE and HAVING in SQL grouping

When we use SQL for grouping, we can use GROUP BY to achieve:
select product_id,sum(amt)
from a_fin_factline
group by 1

The program intent is clear: aggregate sales by product.

When we need to limit the grouping, such as excluding groups with null products, we can do it in two ways:
select product_id,sum(amt)
from a_fin_factline
group by 1
having product_id is not null;


select product_id,sum(amt)
from a_fin_factline
where product_id is not null
group by 1;


Execution time comparison in mysql:
select product_id,sum(amt)
from a_fin_factline
group by 1
having product_id is not null;
Affected rows: 0
Time: 1.086s

select product_id,sum(amt)
from a_fin_factline
where product_id is not null
group by 1;
Affected rows: 0
Time: 0.098s


It can be seen that it is more efficient to use WHERE.

The differences between the two methods are summarized as follows:
  • Syntactically: WHERE comes before GROUP BY, HAVING comes after
  • Execution plan: WHERE makes the limit happen before grouping, HAVING is after grouping
  • Efficiency: using WHERE makes the target result set smaller, and the efficiency of grouping and aggregation is high; when using HAVING, the result set remains unchanged, and WHERE is more efficient
  • Other: WHERE can use any field in the table to limit, and HAVING must be the field after SELECT

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326718799&siteId=291194637