Search⌘ K
AI Features

Answer: The GROUP BY Clause and the AVG Function

Understand how to group data in SQL using the GROUP BY clause combined with the AVG aggregate function. Explore solution techniques, including aliases and subqueries, to calculate averages within grouped data. Gain skills to handle variations like finding highest marks per subject, preparing you for common SQL interview questions.

Solution

The solution is given below:

MySQL
/* The query to find the average of marks in each subject */
SELECT Subject, AVG(Marks) AS averageMarks
FROM StudentGrades
GROUP BY Subject;

Explanation

The explanation of the solution code is given below:

  • Line 2: The SELECT query finds the average of students’ marks for each subject using the AVG() function.

  • Line 3: The FROM clause specifies the table, StudentGrades.

  • Line 4: ...