Problem: Only Keep Interesting Groups
Write a query that:
Groups people by city.
Calculates the average age for each city.
Shows only the cities where the average age is less than 28.
Displays the city name and the average age (use an alias like
avg_age).
Tables
Here is the people and pets table that you’ll use:
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 |
The pets table
ID | Name | Type | Age |
1 | Coco | dog | 5 |
2 | Luna | cat | 3 |
3 | Goldie | fish | 1 |
4 | Rex | dog | 4 |
5 | Whiskers | cat | 2 |
6 | Bubbles | fish | 1 |
7 | Max | dog | 6 |
8 | Milo | cat | 4 |
9 | Bella | dog | 2 |
10 | Shadow | cat | 3 |
Problem: Only Keep Interesting Groups
Write a query that:
Groups people by city.
Calculates the average age for each city.
Shows only the cities where the average age is less than 28.
Displays the city name and the average age (use an alias like
avg_age).
Tables
Here is the people and pets table that you’ll use:
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 |
The pets table
ID | Name | Type | Age |
1 | Coco | dog | 5 |
2 | Luna | cat | 3 |
3 | Goldie | fish | 1 |
4 | Rex | dog | 4 |
5 | Whiskers | cat | 2 |
6 | Bubbles | fish | 1 |
7 | Max | dog | 6 |
8 | Milo | cat | 4 |
9 | Bella | dog | 2 |
10 | Shadow | cat | 3 |