SQL HAVING

The HAVING command is like a WHERE or AND command, but it runs after any group by logic.

-- Works in PostgreSQL, MySQL, SQL Server, and Oracle SELECT building_id, count(*) as number_of_rent_payments FROM rent_payments GROUP BY building_id HAVING COUNT(*) >= 2 ORDER BY COUNT(*);

rent_payments table

building_id payment_date dollar_amount
1 3/1/2018 2000
1 3/2/2018 1500
2 3/1/2018 1800
2 3/3/2018 1900
1 4/2/2018 2000
1 4/2/2018 1500
3 4/2/2018 1600

Query results

building_id number_of_rent_payments
1 4
2 2