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

Fee Summary Query in MySQL

In this lesson: School fee report in SQL — total paid, dues and month-wise collection with GROUP BY and JOIN — query with output. In Hindi & English.

A fee report

A fee summary is a core school-management report: how much each student has paid, how much is due, and the total collection. It is built with SUM and CASE (conditional aggregation) plus a JOIN for student names — a very practical query.

💡 SUM(CASE WHEN status='paid' ...) and SUM(CASE WHEN status='due' ...) give paid and due totals per student.

The tables

students(id, name)
fees(id, student_id, amount, status, paid_date)
-- status is 'paid' or 'due'

Storing one row per fee item, with a status and date, lets us total by status, list dues, and group by month — all from simple queries.

Paid and due per student

SELECT s.name,
    SUM(CASE WHEN f.status = 'paid' THEN f.amount ELSE 0 END) AS paid,
    SUM(CASE WHEN f.status = 'due'  THEN f.amount ELSE 0 END) AS due
FROM students s
JOIN fees f ON f.student_id = s.id
GROUP BY s.id, s.name;
+------+------+------+ | name | paid | due | +------+------+------+ | Aman | 5000 | 1000 | | Diya | 6000 | 0 | +------+------+------+

Two CASE-SUMs split the amount into paid and due columns, grouped per student.

Only students with dues

SELECT s.name,
    SUM(CASE WHEN f.status = 'due' THEN f.amount ELSE 0 END) AS due
FROM students s
JOIN fees f ON f.student_id = s.id
GROUP BY s.id, s.name
HAVING due > 0;

Adding HAVING due > 0 keeps only students who still owe money — exactly the list an office needs for fee reminders.

Overall collection

SELECT SUM(amount) AS total_collected
FROM fees
WHERE status = 'paid';
+-----------------+ | total_collected | +-----------------+ | 110000 | +-----------------+

For the school-wide figure, simply sum all paid amounts. This is the total money actually collected.

Collection by month

SELECT DATE_FORMAT(paid_date, '%Y-%m') AS month,
    SUM(amount) AS collected
FROM fees
WHERE status = 'paid'
GROUP BY DATE_FORMAT(paid_date, '%Y-%m')
ORDER BY month;

Grouping by the year-month of the payment date gives a monthly collection report — useful for tracking cash flow over the year.

Common mistakes

  • Storing paid/due as one total. Keep per-item rows with a status for flexible reports.
  • Using WHERE for the due filter. The due total is an aggregate — use HAVING.
  • Forgetting ELSE 0 in CASE. Without it, SUM may include NULLs oddly.
  • Not grouping by name and id. Group by the student key to be safe.

Summary

  • Store one fee row per item with a status and date.
  • Use SUM(CASE ...) to split paid and due per student.
  • HAVING due > 0 lists students who still owe.
  • SUM paid amounts for total collection.
  • GROUP BY month for a monthly collection report.

Next, learn conditional totals in the pivot table, or build the full school database.

Frequently Asked Questions

How do I make a fee summary query in MySQL?
Store each fee record with a status (paid or due), then use SUM with CASE to total paid and due amounts per student, joined to the students table for names. GROUP BY the student to get one summary row each.
How do I calculate paid and due amounts separately?
Use conditional aggregation: SUM(CASE WHEN status = 'paid' THEN amount ELSE 0 END) AS paid and the same with 'due'. Each SUM adds only the amounts matching that status.
How do I find students who still owe fees?
Group by student and use HAVING on the due total: HAVING SUM(CASE WHEN status = 'due' THEN amount ELSE 0 END) > 0. This keeps only students whose due amount is greater than zero.
How do I get the total fee collection?
Sum the paid amounts across all rows: SELECT SUM(amount) FROM fees WHERE status = 'paid';. For a monthly report, GROUP BY the month of the payment date.
How should I store fee data for good reports?
Store one row per fee item or installment, with student_id, amount, status and a date. This lets you total, filter by status, and group by month easily, and supports partial payments and history.
🔗

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 topicJoins in MySQL Your progress: 65 / 151 (43%) · Lesson 66 / 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