I have the statement
SELECT COUNT(*) as num_requests, ip_address FROM requests
GROUP BY ip_address
ORDER BY num_requests DESC;
When I use the following clause WHERE num_requests = 3 I get a syntax error
SELECT COUNT(*) as num_requests, ip_address
FROM requests
WHERE num_requests = 3
GROUP BY ip_address
ORDER BY num_requests DESC;
Unknown column ‘num_requests’ in ‘where clause’
Is there any way I can use this WHERE clause without writing a nested query or join statement on the entire result set returned by the above?
You need
having🙂To add a note on why
WHEREcouldn’t be used butHAVING: with aggregates such asSUM, MIN,MAX,AVG,COUNTetcWHEREdoen’t work. So you have to use the latter.