Search⌘ K
AI Features

Answer: The GROUP BY Clause and the AVG Function

Explore how to use the SQL GROUP BY clause alongside the AVG function to calculate average values grouped by specific columns. Understand how aggregate functions and aliases work within queries, and learn alternative methods like using DISTINCT with subqueries. This lesson prepares you to handle common SQL interview questions involving data grouping and averaging.

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: ...