Aggregate Functions
Aggregate functions are used to calculate a set of values and return a single result value. They are very important in data analysis and statistics.
Common Aggregate Functions
COUNT
Count rows:
SUM
Calculate sum of numeric columns:
AVG
Calculate average of numeric columns:
MIN and MAX
Find minimum and maximum values:
Grouping Aggregation
GROUP BY Basics
Group by columns for aggregation:
Result:
Multi-column Grouping
Using HAVING to Filter Groups
HAVING is used to filter aggregated results:
Practical Examples
Example 1: Sales Statistics
Example 2: User Behavior Analysis
Performance Optimization
1. Use WHERE Instead of HAVING
2. Create Indexes for Grouping Columns
Summary
Aggregate functions are foundational tools for data analysis. Mastering them is crucial for data statistics and analysis.
Next Step: Learn GROUP BY