Based on your notes, I will format the SQL Aggregate Functions Example note for better readability. Here is the reformatted note:
SQL Aggregate Functions Explained with Tables
Students Table
| ID | Name | Age | Class | Score |
|---|---|---|---|---|
| 1 | 张三 | 18 | A班 | 85 |
| 2 | 李四 | 19 | B班 | 92 |
| 3 | 王五 | 20 | A班 | 78 |
| 4 | 赵六 | 18 | B班 | 96 |
| 5 | 钱七 | 21 | A班 | 88 |
Aggregate Functions Examples
1. COUNT()
sql
1 | SELECT COUNT(*) AS Student Count FROM Students; |
Result:
| Student Count |
|---|
| 5 |
2. AVG()
sql
1 | SELECT Class, AVG(Age) AS Average Age |
Result:
| Class | Average Age |
|---|---|
| A班 | 19.67 |
| B班 | 18.5 |
3. SUM()
sql
1 | SELECT Class, SUM(Score) AS Total Score |
Result:
| Class | Total Score |
|---|---|
| A班 | 251 |
| B班 | 188 |
4. MAX()
sql
1 | SELECT Class, MAX(Score) AS Highest Score |
Result:
| Class | Highest Score |
|---|---|
| A班 | 88 |
| B班 | 96 |
5. MIN()
sql
1 | SELECT Class, MIN(Age) AS Minimum Age |
Result:
| Class | Minimum Age |
|---|---|
| A班 | 18 |
| B班 | 18 |
6. Using HAVING
sql
1 | SELECT Class, AVG(Score) AS Average Score |
Result:
| Class | Average Score |
|---|---|
| B班 | 94 |
This query shows only the classes with an average score higher than 85.
