In SQL, aggregate functions cannot appear in the WHERE clause:
SELECT *
FROM product
WHERE SUM(slae_price) > 1000
GROUP BY product_type;
Copy the code
This will cause an error:
SELECT * FROM product WHERE SUM(slae_price) > 1000 GROUP BY product_type > 1111-invalid use of GROUP function > 0.001 sCopy the code
Why is that?
SELECT * FROM -> WHERE -> GROUP BY -> SELECT
First of all, we know that aggregate functions such as SUM(), AVG(), MAX() and so on are used to filter the results. After FROM, we only get the table product. If this table is aggregated and filtered, what about the following GROUP BY? Select * from table WHERE (GROUP BY); select * from table WHERE (GROUP BY);
Aggregation functions, also known as column functions, evaluate the entire column of data, whereas the WHERE clause filters the rows, and relying on “calculations based on filtered data” is a paradox that won’t work. To put it more simply, because the aggregate function evaluates rows for all column data, it can be used only if the result set is already determined!