Senior
GROUP BY & HAVING
SQL & Databases
How do you find customers who ordered every month in a year?
Short answer: Filter orders to the year, group by customer, and HAVING COUNT(DISTINCT month) = 12.
Sample solution
T-SQL
SELECT CustomerId FROM Orders WHERE OrderDate >= '2025-01-01' AND OrderDate < '2026-01-01' GROUP BY CustomerId HAVING COUNT(DISTINCT DATEPART(MONTH, OrderDate)) = 12;
COUNT(DISTINCT month) is the key — COUNT(*) alone is wrong if multiple orders/month.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png