Database has a few million rows.
This is meant to find products priced above the average price:
SELECT ProductName, Price
FROM Products
WHERE Price > (SELECT AVG(Price) FROM Products);
However, it's pretty slow.
I think this is because it recalculates the average for every row.
Is there a way to optimize this query?
I tried a few different types of queries, though I think I'm missing something.
You can use HAVING: