Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
MySQL + SQL · Lesson 65

SQL से Fee Summary Query

एक fee report

Fee summary एक मुख्य school-management report है: हर student ने कितना paid किया, कितना due है, और कुल collection। यह SUM और CASE (conditional aggregation) और student names को JOIN से बनती है — एक बहुत व्यावहारिक query।

💡 SUM(CASE WHEN status='paid' ...) और SUM(CASE WHEN status='due' ...) प्रति student paid और due totals देते हैं।

Tables

students(id, name)
fees(id, student_id, amount, status, paid_date)
-- status 'paid' या 'due' है

प्रति fee item एक row, status और date के साथ store करना हमें status से total करने, dues list करने, और महीने से group करने देता है — सब सरल queries से।

प्रति student paid और due

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 | +------+------+------+

दो CASE-SUMs amount को paid और due columns में बाँटते हैं, प्रति student grouped।

सिर्फ़ dues वाले students

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;

HAVING due > 0 जोड़ना सिर्फ़ उन students को रखता है जो अब भी पैसे बकाया हैं — बिल्कुल वो list जो office को fee reminders को चाहिए।

कुल collection

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

School-भर के आँकड़े को, बस सारे paid amounts जोड़िए। यह असल में collect किया कुल पैसा है।

महीने से collection

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;

Payment date के year-month से group करना monthly collection report देता है — साल भर cash flow track करने को उपयोगी।

आम गलतियाँ

  • Paid/due को एक total store करना। लचीली reports को status वाली per-item rows रखिए।
  • Due filter को WHERE इस्तेमाल करना। Due total एक aggregate है — HAVING इस्तेमाल कीजिए।
  • CASE में ELSE 0 भूलना। इसके बिना, SUM NULLs अजीब ढंग से शामिल कर सकता है।
  • Name और id से group न करना। सुरक्षित रहने को student key से group कीजिए।

सारांश

  • प्रति item एक fee row status और date के साथ store कीजिए।
  • प्रति student paid और due बाँटने को SUM(CASE ...) इस्तेमाल कीजिए।
  • HAVING due > 0 अब भी बकाया students list करता है।
  • कुल collection को paid amounts SUM कीजिए।
  • Monthly collection report को महीने से GROUP BY कीजिए।

आगे, conditional totals सीखिए pivot table में, या पूरा school database बनाइए।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

MySQL में fee summary query कैसे बनाऊँ?
हर fee record को status (paid या due) के साथ store कीजिए, फिर प्रति student paid और due amounts total करने को CASE के साथ SUM इस्तेमाल कीजिए, names को students table से joined। प्रति एक summary row पाने को student से GROUP BY कीजिए।
Paid और due amounts अलग-अलग कैसे निकालूँ?
Conditional aggregation इस्तेमाल कीजिए: SUM(CASE WHEN status = 'paid' THEN amount ELSE 0 END) AS paid और वही 'due' के साथ। हर SUM सिर्फ़ उस status से मेल खाते amounts जोड़ता है।
अब भी fees बकाया वाले students कैसे ढूँढूँ?
Student से group करके due total पर HAVING इस्तेमाल कीजिए: HAVING SUM(CASE WHEN status = 'due' THEN amount ELSE 0 END) > 0। यह सिर्फ़ उन students को रखता है जिनकी due amount शून्य से ज़्यादा है।
कुल fee collection कैसे पाऊँ?
सारी rows में paid amounts जोड़िए: SELECT SUM(amount) FROM fees WHERE status = 'paid';। Monthly report को, payment date के महीने से GROUP BY कीजिए।
अच्छी reports को fee data कैसे store करूँ?
प्रति fee item या installment एक row store कीजिए, student_id, amount, status और date के साथ। यह आपको आसानी से total करने, status से filter करने, और महीने से group करने देता है, और partial payments व history support करता है।
🔗

Share this topic with a friend

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

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

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

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicMySQL Joins आपकी प्रगति: 65 / 151 (43%) · Lesson 66 / 151 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें