SQL Case File: 003
Case Closed
Aggregation| Base Component | Group |
|---|---|
| Answer Space | The result contains one row per group, and HAVING filters groups after aggregates exist. |
| Problem Space | Relevant data, constraints, relationships, ordering, NULLs, duplicates, and scale. |
| Decision Space | Choose the operation that lawfully transforms evidence into the answer. |
| SQL Reasoning Engine | Use the engine to define what one output row means, filter source rows first, then measure the aggregate work left for SQL Server. |
| SQL Pattern | Rows become groups; groups become aggregate evidence. |
| Common Trap | Filtering groups in WHERE when the condition belongs in HAVING. |
| Hands-On Lab | Group order rows manually and compare COUNT(*), COUNT(column), SUM, and HAVING. |
| Interview Principle | Explain the result, then the query, then the likely SQL Server work. |
If you remember only one thing from this Case File: before writing SQL, ask what data is needed, how to filter it, whether the columns are indexed, whether the predicate is sargable, and how much work SQL Server has to do.