I am trying to use the SQL commands COUNT and GROUP BY to show the number of students with each letter grade, but I'm having difficulty in doing so. A new column that I created contains a letter grade based on a specific range of numeric grades. For example, if the grade is 90 or higher, then it's an "A". If the grade is between 80 and 89, then it's a "B". If the grade is between 70 and 79, then it's a "C". If the grade is between 60 and 69, then it's a "D." Lastly, if the grade is less than 60, then it's an "F". However, I managed to successfully create both the letter grade column and the column containing the number of students grouped by letter grade.
I tried the following SQL query to get the expected result, but it didn't work:
SELECT
name,
number_grade,
ROUND((fraction_completed * 100)) AS percent_completed,
CASE
WHEN number_grade >= 90 THEN 'A'
WHEN number_grade >= 80 THEN 'B'
WHEN number_grade >= 70 THEN 'C'
WHEN number_grade >= 60 THEN 'D'
ELSE 'F'
END AS letter_grade,
COUNT(number_grade) AS num_of_students
FROM student_grades
GROUP BY letter_grade;
However, even though the actual results are a column containing the letter grades and a column containing the number of students grouped by letter grade, with no error results, I still didn't get the correct answer.
SUM(CASE WHEN number_grade >= 90 THEN 1 ELSE 0 END) AS A_countmight workSUMwhich requires an uglyELSEclause, simply writeCOUNT(CASE WHEN number_grade >= 90 THEN 1 END)