WHERE vs HAVING in SQL (Why did I get different result from the same database?)

Screen Link:
Do you know why I got different results when I use “WHERE” instead of “HAVING”? “WHERE” gave me 5 results while “HAVING” gave me 3.

My Code:

–Here is the code that I used “WHERE”–
SELECT race_group, AVG(writing_score) AS writing_score_avg, test_prep_course
FROM performance
WHERE test_prep_course<>‘completed’
GROUP BY race_group;

–Here is the code that I used “HAVING”–
SELECT race_group, AVG(writing_score) AS writing_score_avg, test_prep_course
FROM performance
GROUP BY race_group
HAVING test_prep_course<>‘completed’;

I found my own issue. The 1st code did the average based on filtering criteria before grouping, which is what I want. The 2nd code did the average of all data, grouped, and then filtered.

2 Likes