Aggregate Functions in T-SQL
Aggregate Functions in T-SQL: Calculations vs Aggregations, Common Aggregate Functions, Grouping Aggregations, HAVING.
Updated
- Calculations vs Aggregations (Apprentice): A calculation works within one row and returns a value for every row; an aggregation works across many rows and returns one value for all of them.
- Common Aggregate Functions (Apprentice): COUNT counts rows or values, SUM adds them, AVG averages them, and MIN and MAX find the smallest and largest. All but COUNT(*) skip NULL.
- Grouping Aggregations (Journeyman): GROUP BY splits the rows into groups and runs the aggregates once per group. Every column in the SELECT that isn't aggregated must be in the GROUP BY.
- HAVING (Journeyman): WHERE filters rows before they're grouped; HAVING filters the groups afterward, by their aggregates. Know which to use, and the order a query's clauses run in.