SELECT Product_Finish AS "Desk Finish",
AVG(Standard_Price) AS "Average Price"
FROM Product_T
WHERE "Average Price">300
ORDER BY "Average Price" DESC;
What did I do wrong?
SELECT Product_Finish AS "Desk Finish",
AVG(Standard_Price) AS "Average Price"
FROM Product_T
WHERE "Average Price">300
ORDER BY "Average Price" DESC;
What did I do wrong?
There are several issues:
you use aggregate function AVG()
but the query has no GROUP BY
clause
the where
clause refers to an aggregated column; you must use a having
clause for this
identifiers are surrounded with double quotes; MySQL wants backticks instead
So:
SELECT Product_Finish AS `Desk Finish`, AVG(Standard_Price) AS `Average Price`
FROM Product_T
GROUP BY Product_Finish
HAVING `Average Price`> 300
ORDER BY `Average Price` DESC;
Better yet would be to use identifiers that require no quoting. Also, it is worth noting that, unlike MySQL, not all databases support column aliases in the having
clause (in which case you need to repeat the aggregate expression). This is standard SQL:
SELECT Product_Finish AS desk_finish, AVG(Standard_Price) AS average_price
FROM Product_T
GROUP BY Product_Finish
HAVING AVG(Standard_Price) > 300
ORDER BY average_price DESC;