How to use aggregate functions in a WHERE?

0

I'm doing college work on Database, and one of the Query I should do is this:

  • Show all lenders who have had a larger total purchase that R $ 5000.00;

I tried:

SELECT NomeCredor, SUM(Compras.Valor)

FROM Credores, Compras

WHERE (idCredores = Compras.Credores_idCredores)

AND (SUM(Compras.Valor)) > 5000

GROUP BY NomeCredor

ORDER BY NomeCredor;

The problem is that SUM(Compras.Valor) can not be passed as a condition of WHERE . Does anyone know an alternative?

    
asked by anonymous 11.12.2017 / 18:41

1 answer

3

Try to use the HAVING clause after Group By, which is very similar to WHERE, but checks the condition within the grouping.

Excellent reference on HAVING in SQLite .

    
11.12.2017 / 18:43