Paul Dessert Paul Dessert - 6 days ago 5
SQL Question

SELECT not returning anything using HAVING clause

I'm trying to get the total percentage off and only return the matches >+ 80. However, this doesn't return any results:

SELECT * FROM products WHERE Available=1 AND Merchant='Amazon' HAVING (LowestUsedPrice - LowestNewPrice) / LowestNewPrice * 100 >= ?


Am I using
HAVING
correctly?

Answer

HAVING specifies a search condition for a group or an aggregate function used in SELECT statement.

HAVING is applied after the aggregation phase and must be used if you want to filter aggregate results.

Your query is wrong. What you can do is do the conditioning in where clause only.

SELECT * 
FROM products 
WHERE Available=1 
AND Merchant='Amazon' 
AND (LowestUsedPrice - LowestNewPrice) / LowestNewPrice * 100 >= ?
Comments