Write a query to show the average marks of each city

Description: Show each city with the average marks of its students.

Tables: the exercises use the students (id, name, city, marks, course_id) and courses (id, course_name) tables, with the data from our MySQL INSERT chapter. The students are: Robin (Delhi, 85), Maria (Mumbai, 92), John (Pune, 78), Asha (Jaipur, 88), Vikram (Delhi, 65), Neha (Mumbai, 45).

Hint:

  • Use AVG(marks) with GROUP BY city.
  • Use ROUND() to show 2 decimals.

MySQL query for the average marks by city

SELECT city, ROUND(AVG(marks), 2) AS average_marks
FROM students
GROUP BY city
ORDER BY city;

Output:

Tutorials Class - Output Window
city average_marks
Delhi 75.00
Jaipur 88.00
Mumbai 68.50
Pune 78.00

How it works

  • For Delhi: (85 + 65) / 2 = 75.
  • For Mumbai: (92 + 45) / 2 = 68.5.

Read Aggregate Functions.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.