Problem: Group Things Together
You want to find the average age of people in each city. Instead of looking at individual rows, you'll group them by city and calculate the average age within each group.
Hint: Use
GROUP BYto organize data into groups based on thecitycolumn, then applyAVG(age)to each group.
The people table
ID | Name | Age | City |
1 | Aisha | 30 | Karachi |
2 | Dan | 24 | New York |
3 | Fatima | 27 | Lahore |
4 | Lee | 22 | Seoul |
5 | Omar | 31 | Karachi |
6 | Nora | 29 | New York |
7 | Ali | 33 | Lahore |
8 | Zara | 25 | Seoul |
9 | John | 40 | New York |
10 | Hina | 26 | Karachi |
Problem: Group Things Together
You want to find the average age of people in each city. Instead of looking at individual rows, you'll group them by city and calculate the average age within each group.
Hint: Use
GROUP BYto organize data into groups based on thecitycolumn, then applyAVG(age)to each group.
The people table
ID | Name | Age | City |
1 | Aisha | 30 | Karachi |
2 | Dan | 24 | New York |
3 | Fatima | 27 | Lahore |
4 | Lee | 22 | Seoul |
5 | Omar | 31 | Karachi |
6 | Nora | 29 | New York |
7 | Ali | 33 | Lahore |
8 | Zara | 25 | Seoul |
9 | John | 40 | New York |
10 | Hina | 26 | Karachi |