Home:ALL Converter>Comparing AVG() in SQL

Comparing AVG() in SQL

Ask Time:2015-02-02T13:13:37         Author:ln206

Json Formatter

This is my first SQL query practice and I came up with this problem where I have to find an average of Quantity of items in stock from somewhere house that is greater than the average of a specific warehouse. All i got is a list of the warehouse with their average (the specific warehouse is not included) and I have to keep only the warehouse with its average that is greater than the average of that specific warehouse. How do I solve this? This is my first time learning SQL. Thank you

SELECT Warehouse, AVG(QuantityInStocks) AS Average
FROM SomeTable
WHERE Warehouse Not In ('Specific_Warehouse')
GROUP BY Warehouse;

Author:ln206,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/28271470/comparing-avg-in-sql
Ivan Gerasimenko :

You should use subquery in HAVING clause:\n\nSELECT Warehouse, AVG(QuantityInStocks) AS Average\nFROM SomeTable\nWHERE Warehouse NOT IN ('Specific_Warehouse')\nGROUP BY Warehouse\nHAVING AVG(QuantityInStocks) > \n (SELECT AVG(QuantityInStocks) \n FROM SomeTable \n WHERE Warehouse IN ('Specific_Warehouse') \n GROUP BY Warehouse);\n",
2015-02-02T05:32:06
yy