Answer: Subquery and Calculations
Find a detailed explanation of using subqueries.
Solution
The solution is given below:
Press + to interact
MySQL
/* The query to find the items and categories with more than average sales */SELECT p.ProductName, p.CategoryFROM Products AS pWHERE p.UnitsSold >(SELECT AVG(p2.UnitsSold) FROM Products AS p2)
Explanation
The explanation of the solution code is given below:
Line 2: The
SELECTquery selects theProductNameandCategorycolumns from theProductstable. TheProductstable has been given an aliaspfor easy referencing.Line 3: The
WHEREclause applies the condition that sales ofUnitsSoldis greater than the result of the subquery.Line 4: The subquery returns the calculated average of all units sold of the products.
Recall of relevant concepts
We have covered the following concepts in this question: ...
Ask