Answer: The GROUP BY Clause and the SUM Function
Find a detailed explanation of using the GROUP BY clause to group the records using SQL query.
We'll cover the following...
We'll cover the following...
Solution
The solution is given below:
MySQL
/* The query to find the total price of products for each category */SELECT Category, SUM(Price) AS TotalPriceFROM ProductsGROUP BY Category;
Explanation
The explanation of the solution code is given below:
Line 2: The
SELECTquery selects theCategorycolumn and the total price of thePricecolumn by calculating it using theSUMfunction. 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. ...