Cash Donations and 80G Eligibility — SQL Problem
An NGO issues 80G tax receipts for the financial year 2024-25 (1 April 2024 to 31 March 2025, both included).
- Difficulty: Easy
- Topics: Conditional Logic, Basics
- Dialect: MySQL
- Problem: #80
Problem statement
An NGO issues 80G tax receipts for the financial year 2024-25 (1 April 2024 to 31 March 2025, both included). Under the rule it follows, a donation paid in cash above ₹2,000 cannot get an 80G receipt; every other donation can (a cash gift of exactly ₹2,000 still qualifies).
For every donation made in FY 2024-25 return donation_id, donor_name, amount and eligible_80g ('Yes' or 'No'), ordered by donation_id.
Tables
Table: donations
| Column | Type |
|---|---|
| donation_id | int |
| donor_name | varchar |
| amount | int |
| payment_mode | enum(cash, upi, cheque, netbanking) |
| donated_on | date |
Primary key: donation_id.
One row per gift; amount is in rupees.
Examples
Example 1
donations
| donation_id | donor_name | amount | payment_mode | donated_on |
|---|---|---|---|---|
| 1 | Saanvi Shah | 5000 | upi | 2024-04-01 |
| 2 | Rohan Gupta | 2500 | cash | 2024-06-15 |
| 3 | Aditi Rao | 2000 | cash | 2024-08-02 |
| 4 | Karan Singh | 10000 | cheque | 2024-03-31 |
| 5 | Meera Nair | 1500 | cash | 2025-01-26 |
| 6 | Liam Das | 7500 | netbanking | 2025-03-31 |
| 7 | Riya Joshi | 3000 | cash | 2025-04-01 |
Output
| donation_id | donor_name | amount | eligible_80g |
|---|---|---|---|
| 1 | Saanvi Shah | 5000 | Yes |
| 2 | Rohan Gupta | 2500 | No |
| 3 | Aditi Rao | 2000 | Yes |
| 5 | Meera Nair | 1500 | Yes |
| 6 | Liam Das | 7500 | Yes |
How to solve Cash Donations and 80G Eligibility
Indian financial years run April to March, so FY 2024-25 is the closed range from 2024-04-01 to 2025-03-31; the example deliberately has gifts on 31 March 2024 and 1 April 2025 just outside it. BETWEEN or a pair of >=/<= comparisons both work because the dates are stored in sortable YYYY-MM-DD form.
The label is one CASE. Ineligibility needs both conditions together, payment_mode = 'cash' AND amount > 2000; the comparison is strict, so the ₹2,000 cash gift still gets 'Yes'. The IF alternative writes the negation instead — by De Morgan's law, "not (cash and above 2,000)" is "not cash, or at most 2,000" — and returns the same labels.
Every row is read once and nothing is grouped, so the cost is a single scan, or an index range scan on donated_on.
Reference solution (MySQL)
SELECT donation_id, donor_name, amount,
CASE WHEN payment_mode = 'cash' AND amount > 2000 THEN 'No' ELSE 'Yes' END AS eligible_80g
FROM donations
WHERE donated_on >= '2024-04-01' AND donated_on <= '2025-03-31'
ORDER BY donation_idAnother way
SELECT donation_id, donor_name, amount, IF(payment_mode <> 'cash' OR amount <= 2000, 'Yes', 'No') AS eligible_80g FROM donations WHERE donated_on BETWEEN '2024-04-01' AND '2025-03-31' ORDER BY donation_id← Library Cards Expiring in March 2025 · Library Members Blocked for Unpaid Fines →