COUNT / SUM / AVG / MAX / MIN
Feb 23, 2026
·
5
min read
Category: Aggregate Functions
Platform Support:
✅ Snowflake | ✅ BigQuery | ✅ Databricks
Description
Core aggregate functions that compute summary values across rows. COUNT counts rows, SUM adds values, AVG calculates average, MAX finds maximum, and MIN finds minimum. Essential for data analysis, reporting, and statistical calculations.
Syntax
Platform-Specific Notes
All Platforms:
Standard SQL aggregate functions
Identical behavior across platforms
Ignore NULL values (except COUNT(*))
Used with GROUP BY for grouped aggregation
Example 1: Basic Aggregations
All Platforms:
Sample Data (orders table):
order_id | customer_id | order_date | total |
|---|---|---|---|
101 | 1 | 2024-01-15 | 250.00 |
102 | 2 | 2024-01-16 | 180.00 |
103 | 1 | 2024-01-20 | 320.00 |
104 | 3 | 2024-01-22 | 450.00 |
105 | 2 | 2024-01-25 | 290.00 |
Result:
total_orders | unique_customers | total_revenue | average_order_value | largest_order | smallest_order | most_recent_order | first_order |
|---|---|---|---|---|---|---|---|
5 | 3 | 1490.00 | 298.00 | 450.00 | 180.00 | 2024-01-25 | 2024-01-15 |
Example 2: GROUP BY with Aggregates
All Platforms:
Sample Data (employees table):
employee_id | name | department | salary |
|---|---|---|---|
1 | Alice | Sales | 75000 |
2 | Bob | Sales | 65000 |
3 | Carol | Sales | 70000 |
4 | David | IT | 95000 |
5 | Emma | IT | 92000 |
6 | Frank | IT | 88000 |
7 | Grace | Marketing | 72000 |
Result:
department | employee_count | total_payroll | avg_salary | highest_salary | lowest_salary | salary_range |
|---|---|---|---|---|---|---|
IT | 3 | 275000 | 91666.67 | 95000 | 88000 | 7000 |
Sales | 3 | 210000 | 70000.00 | 75000 | 65000 | 10000 |
Marketing | 1 | 72000 | 72000.00 | 72000 | 72000 | 0 |
Example 3: COUNT Variations
All Platforms:
Sample Data (customers table):
customer_id | name | phone | |
|---|---|---|---|
1 | John | 555-1234 | |
2 | Maria | NULL | |
3 | Sarah | NULL | 555-5678 |
4 | Bob | 555-9012 | |
5 | Alice | NULL | NULL |
Result:
all_rows | rows_with_email | unique_emails | rows_with_phone | missing_emails | missing_phones |
|---|---|---|---|---|---|
5 | 3 | 3 | 3 | 2 | 2 |
Example 4: Conditional Aggregation
All Platforms:
Sample Data (products table):
product_id | product_name | product_category | price | in_stock |
|---|---|---|---|---|
1 | Laptop | Electronics | 999 | TRUE |
2 | Mouse | Electronics | 29 | TRUE |
3 | Monitor | Electronics | 349 | FALSE |
4 | Desk | Furniture | 299 | TRUE |
5 | Chair | Furniture | 199 | TRUE |
Result:
product_category | total_products | in_stock_count | out_of_stock_count | premium_products | avg_price_in_stock | max_available_price |
|---|---|---|---|---|---|---|
Electronics | 3 | 2 | 1 | 2 | 514.00 | 999 |
Furniture | 2 | 2 | 0 | 2 | 249.00 | 299 |
Example 5: Time-Based Aggregation
All Platforms:
Sample Result:
month | transaction_count | unique_customers | monthly_revenue | avg_transaction | largest_sale | smallest_sale | revenue_per_customer |
|---|---|---|---|---|---|---|---|
2024-01-01 | 450 | 280 | 125000 | 277.78 | 2500 | 15 | 446.43 |
2024-02-01 | 520 | 310 | 142000 | 273.08 | 3000 | 10 | 458.06 |
Example 6: Nested Aggregations
All Platforms:
NULL Handling
Important differences:
Function | NULL Behavior | Example |
|---|---|---|
COUNT(*) | Counts NULLs | 5 rows with 2 NULLs = 5 |
COUNT(column) | Ignores NULLs | 5 rows with 2 NULLs = 3 |
SUM(column) | Ignores NULLs | SUM(10, 20, NULL) = 30 |
AVG(column) | Ignores NULLs | AVG(10, 20, NULL) = 15 |
MAX/MIN(column) | Ignores NULLs | MAX(10, 20, NULL) = 20 |
Aggregate Function Combinations
All Platforms:
Window Functions with Aggregates
All Platforms:
Common Use Cases
COUNT:
Row counting: Total records, unique values
Data quality: Missing values, duplicates
Reporting: Customer counts, transaction volumes
SUM:
Financial: Revenue, expenses, totals
Inventory: Stock quantities
Metrics: Page views, clicks, conversions
AVG:
Performance: Average response time, ratings
Business: Average order value, basket size
Analytics: Mean metrics, benchmarks
MAX/MIN:
Ranges: Price ranges, date ranges
Extremes: Highest score, oldest record
Thresholds: Peak usage, minimum stock
Performance Tips
Index GROUP BY columns: Improves grouping performance
Use APPROX_COUNT_DISTINCT: Faster for large datasets
Filter before aggregating: Use WHERE not HAVING
Avoid unnecessary DISTINCT: COUNT(DISTINCT) is expensive
Consider materialized views: For frequently aggregated queries
Best Practices
Handle NULLs explicitly: Use COALESCE if needed
Use meaningful aliases: Name aggregates clearly
Validate results: Check for division by zero
Document calculations: Explain complex aggregations
Test with edge cases: Empty groups, all NULLs
Use appropriate precision: ROUND results when needed





