0

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.

4
  • SUM(CASE WHEN number_grade >= 90 THEN 1 ELSE 0 END) AS A_count might work Commented May 10 at 14:27
  • No need to use SUM which requires an ugly ELSE clause, simply write COUNT(CASE WHEN number_grade >= 90 THEN 1 END) Commented May 10 at 15:23
  • 1
    You are selecting the name, implying one row per student. You're also trying to get the number of students with each grade, implying one row per grade. This contradiction is why it's important to write a minimal reproducible example including the results you want. Commented May 10 at 15:45
  • 2
    Please read : Why should I provide a Minimal Reproducible Example, even for a very simple SQL query? Commented May 10 at 15:45

1 Answer 1

0

You can use conditional COUNT for that purpose

CREATE TABLE student_grades(number_grade  int,fraction_completed  DOUBLE)

INSERT INTO student_grades VALUES (91,96.5),(81,20), (71,10)

SELECT * FROM
student_grades
number_grade fraction_completed
91 96.5
81 20
71 10
SELECT

    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

,SUM(CASE  WHEN number_grade >= 90 THEN 1 ELSE 0 END) AS count_A
,SUM(CASE  WHEN number_grade >= 80 AND number_grade < 90 THEN 1 ELSE 0 END) AS count_B
,SUM(CASE  WHEN number_grade >= 70 AND number_grade < 80 THEN 1 ELSE 0 END) AS count_C
,SUM(CASE  WHEN number_grade >= 60 AND number_grade < 70 THEN 1 ELSE 0 END) AS count_D
FROM student_grades
GROUP BY letter_grade;
number_grade percent_completed letter_grade num_of_students count_A count_B count_C count_D
91 9650 A 1 1 0 0 0
81 2000 B 1 0 1 0 0
71 1000 C 1 0 0 1 0

fiddle

Sign up to request clarification or add additional context in comments.

3 Comments

No need to use SUM which requires an ugly ELSE clause, we can simply write COUNT(CASE WHEN number_grade >= 90 THEN 1 END). Modified version of your fiddle here
Why is number_grade included in the SELECT block?
because it is in the original query

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.