WHERE tests each log row before the count is calculated. Run the example to see the failure count for every environment. HAVING can then remove the small groups from that result.
SELECT environment, COUNT(*) AS failure_count
FROM deployments
WHERE status = 'failed'
GROUP BY environment;Keep runs with status failed. Group them by environment and return environment and failure_count for environments with at least three such runs.