SQL GROUP BY statement and practical examples

SQL GROUP BY statement

GROUP BY Statement
The GROUP BY statement is used in conjunction with aggregate functions to group the result set by one or more columns.

#SQL GROUP BY 语法
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name

In short, it is to column_nameperform the function according to the function. For example, if column_namethere are multiple repeated data, and the data is column_name2different, you can call it SUM()to merge the data column_name2and select to display it.

topic

10-14 A4-4 Find product information in the product table whose average order quantity is greater than a specific value (20 points)
Find product information in the product table (products) whose total order quantity is greater than 15, displayed as: product number (ProductID), and Total Order Quantity (renamed to sumUnitsOnOrder)

Tip: Please use the SELECT statement to answer.

Table Structure:

insert image description here

table sample

insert image description here

# output sampleinsert image description here

the code

SELECT ProductID, SUM(UnitsOnOrder) sumUnitsOnOrder 
FROM products 
GROUP BY ProductID
Having sumUnitsOnOrder>15

MySQL authentication

insert image description here
insert image description here
insert image description here

END

Guess you like

Origin blog.csdn.net/horizon08/article/details/114004329