Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
MySQL + SQL · Lesson 64

Report Card Query in MySQL

In this lesson: Build a student report card with SQL — total marks, percentage, grade with CASE and rank — full query and output. In Hindi & English.

A real report card

A report card query is a classic school project: from a table of marks, compute each student's total, percentage, grade and pass/fail. It brings together SUM, GROUP BY and CASE — a great practical exercise.

💡 One row per student per subject → GROUP BY student → SUM for total, CASE for grade.

The marks table

-- one row per student per subject
+------------+---------+-------+
| student_id | subject | marks |
+------------+---------+-------+
|          1 | Math    |    88 |
|          1 | Science |    76 |
|          1 | English |    92 |
+------------+---------+-------+

This normalized layout (a row per subject) makes totals and averages easy, and lets you add subjects without changing the table.

Total and percentage

SELECT student_id,
    SUM(marks) AS total,
    ROUND(SUM(marks) / (COUNT(*) * 100) * 100, 2) AS percent
FROM marks
GROUP BY student_id;
+------------+-------+---------+ | student_id | total | percent | +------------+-------+---------+ | 1 | 256 | 85.33 | +------------+-------+---------+

GROUP BY student, SUM the marks for the total, and divide by the maximum (subjects × 100) for the percentage, rounded to two places.

Grade with CASE

SELECT student_id,
    SUM(marks) AS total,
    CASE
        WHEN SUM(marks)/(COUNT(*)*100)*100 >= 90 THEN 'A'
        WHEN SUM(marks)/(COUNT(*)*100)*100 >= 75 THEN 'B'
        WHEN SUM(marks)/(COUNT(*)*100)*100 >= 33 THEN 'C'
        ELSE 'F'
    END AS grade
FROM marks
GROUP BY student_id;

A CASE on the percentage turns the number into a letter grade, right in the query.

Pass or fail

SELECT student_id,
    CASE WHEN MIN(marks) >= 33 THEN 'Pass' ELSE 'Fail' END AS result
FROM marks
GROUP BY student_id;

A student passes only if every subject is at least the pass mark. Checking MIN(marks) >= 33 catches a failure in any single subject.

Ranking students

SELECT student_id, SUM(marks) AS total,
    RANK() OVER (ORDER BY SUM(marks) DESC) AS rank_no
FROM marks
GROUP BY student_id;

In MySQL 8.0+, RANK() assigns positions by total marks, giving each student their class rank. On older MySQL, just ORDER BY total DESC.

Common mistakes

  • Storing all subjects in one row. Use one row per subject for easy SUM/AVG.
  • Wrong percentage divisor. Divide by subjects × max marks, not a fixed number.
  • Grading before pass check. A low subject can fail a high average.
  • Forgetting GROUP BY. Aggregates need it to work per student.

Summary

  • Store marks one row per student per subject.
  • Total = SUM(marks); percentage divides by subjects × 100.
  • Grade with a CASE on the percentage.
  • Pass/fail uses MIN(marks) against the pass mark.
  • Rank with ORDER BY total or RANK() (MySQL 8.0+).

Next, learn grouping in GROUP BY and HAVING, or build the full marksheet database project.

Frequently Asked Questions

How do I calculate total and percentage in a MySQL report card?
Store one row per subject per student, then use SUM(marks) grouped by student for the total, and divide by the maximum possible to get percentage: SUM(marks) AS total, SUM(marks)/(COUNT(*)*100)*100 AS percent. GROUP BY the student.
How do I assign a grade in a report card query?
Use a CASE on the percentage: CASE WHEN percent >= 90 THEN 'A' WHEN percent >= 75 THEN 'B' ... ELSE 'F' END. This turns the numeric percentage into a letter grade in the result.
How do I show pass or fail in MySQL?
Use a CASE comparing to a pass mark, or check the minimum subject mark: CASE WHEN MIN(marks) >= 33 THEN 'Pass' ELSE 'Fail' END. A student fails if any subject is below the pass mark.
How do I rank students by their total marks?
Order by total descending: ORDER BY total DESC. For a numbered rank, use RANK() OVER (ORDER BY total DESC) in MySQL 8.0+, which gives each student a position based on their total.
How should I structure marks for a report card?
Store marks with one row per student per subject (student_id, subject, marks). This normalized layout lets you SUM, AVG and COUNT easily per student, and add new subjects without changing the table structure.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicFee Summary Query Using SQL Your progress: 64 / 151 (42%) · Lesson 65 / 151 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free