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

Marksheet Database Project in MySQL

In this lesson: Student marksheet database in MySQL — design, sample data, total, percentage, grade and rank queries with output. In Hindi & English.

The project

A marksheet database stores exam marks and produces a result — total, percentage, grade and rank. It is a focused version of a school database, centred on marks and results. The key idea is storing marks in a separate table, one row per subject.

💡 One row per student per subject → GROUP BY student → total, percentage, grade, rank.

Designing the tables

  • students — id, name, roll_no.
  • subjects — id, subject_name, max_marks.
  • marks — id, student_id (FK), subject_id (FK), marks_obtained.

Marks link students to subjects. Keeping max_marks on subjects lets you compute percentage correctly even if subjects are out of different totals.

Creating the schema

CREATE TABLE students (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(50) NOT NULL,
    roll_no INT UNIQUE
);
CREATE TABLE subjects (
    id INT PRIMARY KEY AUTO_INCREMENT,
    subject_name VARCHAR(30),
    max_marks INT DEFAULT 100
);
CREATE TABLE marks (
    id INT PRIMARY KEY AUTO_INCREMENT,
    student_id INT, subject_id INT, marks_obtained INT,
    FOREIGN KEY (student_id) REFERENCES students(id),
    FOREIGN KEY (subject_id) REFERENCES subjects(id)
);

Sample data

INSERT INTO students (name, roll_no) VALUES ('Aman', 1), ('Diya', 2);
INSERT INTO subjects (subject_name, max_marks) VALUES ('Math', 100), ('Science', 100);
INSERT INTO marks (student_id, subject_id, marks_obtained) VALUES
    (1, 1, 88), (1, 2, 76), (2, 1, 95), (2, 2, 90);

The result query

SELECT s.name,
    SUM(m.marks_obtained) AS total,
    ROUND(AVG(m.marks_obtained), 2) AS percentage,
    CASE
        WHEN AVG(m.marks_obtained) >= 90 THEN 'A'
        WHEN AVG(m.marks_obtained) >= 75 THEN 'B'
        WHEN AVG(m.marks_obtained) >= 33 THEN 'C'
        ELSE 'F'
    END AS grade,
    CASE WHEN MIN(m.marks_obtained) >= 33 THEN 'Pass' ELSE 'Fail' END AS result
FROM students s
JOIN marks m ON m.student_id = s.id
GROUP BY s.id, s.name;
+------+-------+------------+-------+--------+ | name | total | percentage | grade | result | +------+-------+------------+-------+--------+ | Aman | 164 | 82.00 | B | Pass | | Diya | 185 | 92.50 | A | Pass | +------+-------+------------+-------+--------+

Ranking students

SELECT s.name, SUM(m.marks_obtained) AS total,
    RANK() OVER (ORDER BY SUM(m.marks_obtained) DESC) AS rank_no
FROM students s
JOIN marks m ON m.student_id = s.id
GROUP BY s.id, s.name;

In MySQL 8.0+, RANK() gives each student a class position by total marks. On older versions, use ORDER BY total DESC.

Common mistakes

  • A column per subject. Use a marks table instead, for flexibility.
  • Ignoring max_marks. Percentage needs the maximum, especially if it varies.
  • Grading before the pass check. One failed subject should fail the result.
  • Forgetting to JOIN. Marks alone lack the student names.

Summary

  • A marksheet database uses students, subjects and a marks table.
  • Store marks one row per student per subject.
  • Compute total (SUM), percentage (AVG) and grade (CASE).
  • Check pass/fail with MIN(marks).
  • Rank with ORDER BY total or RANK() (MySQL 8.0+).

Next, build the query in report card query, or the full school database.

Frequently Asked Questions

How do I design a marksheet database in MySQL?
Create a students table, a subjects table, and a marks table linking them with student_id and subject_id plus the marks. This normalized design lets you compute totals, percentage and grade per student with simple aggregate queries.
How do I calculate the result from a marksheet database?
Join marks to students, GROUP BY the student, and use SUM(marks) for the total and AVG(marks) for the percentage. A CASE on the average gives the grade, and MIN(marks) checks pass or fail.
How do I add exams or terms to a marksheet database?
Add an exam_id (or term) column to the marks table, referencing an exams table. Then you can store marks for multiple exams and filter or group by exam to produce term-wise marksheets.
How do I rank students by their result?
Order by total marks descending, or use RANK() OVER (ORDER BY SUM(marks) DESC) in MySQL 8.0+. This assigns each student a position based on their total, which is their class rank.
Why store marks in a separate table?
Storing one row per student per subject (rather than a column per subject) keeps the design flexible: you can add subjects without altering the table, and easily total, average and grade using GROUP BY.
🔗

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 topicImportant MySQL Practical Commands Your progress: 121 / 151 (80%) · Lesson 122 / 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