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
/* The query to find the average price of products for each category */
SELECT Category, AVG(Price) AS AveragePrice
FROM Products
GROUP BY Category;

Explanation

The explanation of the solution code is given below:

  • Line 2: The SELECT query selects the Category column and the average price of the Price column by calculating it using the AVG function. We use AS to set an alias for the column.

  • Line 3: The FROM clause specifies the table, Products.

  • Line 4: ...

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.