Answer: Aggregate Records Using AVG
Find a detailed explanation of using the AVG function to aggregate records in a table using SQL query.
Solution
The solution is given below:
Press + to interact
MySQL
/* The query to find the average price of products for each category */SELECT Category, AVG(Price) AS AveragePriceFROM ProductsGROUP BY Category;
Explanation
The explanation of the solution code is given below:
Line 2: The
SELECTquery selects theCategorycolumn and the average price of thePricecolumn by calculating it using theAVGfunction. We useASto set an alias for the column.Line 3: The
FROMclause specifies the table,Products.Line 4: The
GROUP BYclause specifies theCategorycolumn on which we want to group our data. ...
Ask