Using SQL AVG with WHERE clause for specific filtering

In database management systems, the AVG function is commonly used to calculate the average value of a particular column. However, you can further refine your average calculations by using the WHERE clause to specify specific filtering conditions. This allows you to calculate the average on a subset of data that meets certain criteria.

Let’s say we have a table named grades with the following structure:

CREATE TABLE grades (
    student_id INT,
    subject VARCHAR(50),
    score FLOAT
);

To calculate the average score for only a specific subject, you can use the AVG function along with the WHERE clause. Here’s an example query using the WHERE clause to filter for only English grades:

SELECT AVG(score) AS average_score
FROM grades
WHERE subject = 'English';

In this query, we are selecting the average score from the grades table where the subject is ‘English’. The result will be the average value of the scores for the English subject.

You can also apply more complex filtering conditions using the WHERE clause. For example, let’s calculate the average score for students who scored above 80 in the subject ‘Mathematics’:

SELECT AVG(score) AS average_score
FROM grades
WHERE subject = 'Mathematics' AND score > 80;

This query filters for only the Mathematics subject and selects the average score for students who scored above 80. The result will be the average score for Mathematics subject, considering only the students who have scored above 80.

Using the AVG function with the WHERE clause provides a powerful way to calculate average values based on specific filtering conditions that you need in your analysis or reporting tasks.

#SQL #AVGwithWHEREClause