SQL Practice: GROUP BY & HAVING Examples

Date: June 17, 2026

Today I focused on GROUP BY and HAVING clauses. Below are clear examples using both AdventureWorksDW2017 and simple healthcare scenarios.


1. Basic GROUP BY

AdventureWorks Example

SELECT 
    EnglishProductCategoryName AS Category,
    COUNT(*) AS ProductCount
FROM DimProductCategory
GROUP BY EnglishProductCategoryName
ORDER BY ProductCount DESC;

Healthcare Example

SELECT 
    ClaimType,
    COUNT(*) AS ClaimCount,
    SUM(ClaimAmount) AS TotalAmount
FROM Claims
GROUP BY ClaimType
ORDER BY TotalAmount DESC;

2. GROUP BY with Multiple Columns + HAVING

AdventureWorks Example

SELECT 
    EnglishEducation AS EducationLevel,
    COUNT(*) AS CustomerCount
FROM DimCustomer
GROUP BY EnglishEducation
HAVING COUNT(*) > 500
ORDER BY CustomerCount DESC;

Healthcare Example

SELECT 
    ProviderName,
    COUNT(*) AS PatientCount,
    AVG(RiskScore) AS AvgRisk
FROM PatientRisk
GROUP BY ProviderName
HAVING COUNT(*) >= 50 AND AVG(RiskScore) > 2.5
ORDER BY AvgRisk DESC;

Key Points I Learned Today

  • GROUP BY summarizes data by categories
  • HAVING filters groups (after aggregation), while WHERE filters individual rows
  • Always put HAVING after GROUP BY
  • Use meaningful aliases for better readability

More examples and practice will be added.

Scroll to Top