Common aggregate functions
COUNT() Find how many rows there are
SUM() Summing
AVG() average value
MIN() finds the minimum value
MAX() Find the maximum value
give a chestnut
SELECT AVG(price) FROM products WHERE price_id > 10;
This line of code is to find the average price of id greater than 10
AVG(price) means to find the average value of the price column
The execution logic is to first delete the rows with price_id>10 from the WHERE statement, and then output the average price of these rows
Example of aliasing
SELECT AVG(price) AS avg_price FROM products WHERE price_id > 10;
Group query
GROUP BY
create group
Look directly at the example
SELECT vend_id,COUNT(*) AS num_prods FROM products GROUP BY vend_id;
The execution logic is: first group all the data according to the vendor_id, and then perform the grouping operation
If NULL appears in the group, NULL appears as a separate group
The GROUP BY statement must appear after the WHERE and before the ORDER BY
The parameter of GROUP BY cannot be an expression or an alias
HAVING
filter group
All WHERE options are applicable to HAVING. The difference between HAVING and WHERE is that one is a filter group and the other is a filter row.
Or it can be understood that they are all filtering functions, but WHERE is filtering before grouping and HAVING is filtering after grouping
The function of HAVING here is that only groups with COUNT(*) >= 2 will be left
Order of SELECT clauses
SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY
LIMIT