…
…
Apply a scholarship board's approvals, rejections, fund transfers and corrections with BEGIN, COMMIT, ROLLBACK and SAVEPOINT so each one happens completely or not at all.
The campus scholarship board gives out no-strings scholarships from four funds. Last term the decisions were entered one command at a time, and one payment stopped halfway: the money shown in the ledger was never subtracted from what remained in the fund. This term you will apply all of the board's decisions; you will make every change that belongs together in one transaction, check the result before confirming it, and roll back what turned out wrong, completely or partly. At the end you will also find the half-done job left over from last term and finish it.
Data: four tables, fictional sample data.
fund: id, name, award_amount (the scholarship per term, in dollars), remaining (the money left in the fund, in dollars; it cannot go below zero) (4 funds)student: id, first_name, last_name, department, year (40 students)application: id, student_id, fund_id, score (0–100), status ('pending', 'approved', 'waitlist' or 'rejected'); a student applies to a fund once (61 applications)ledger: the funds' money ledger. id, fund_id, application_id (the application if it is a scholarship payment, otherwise NULL; at most one payment per application), amount (money in is positive, money out is negative), description, entry_date (19 rows)A fund's remaining value must equal the sum of its rows in the ledger.
Lessons used: INSERT, UPDATE and safe changes (34–36), transactions (37), views (38); joins, grouping and FILTER from earlier units. No new concept is taught.
Project cells are persistent: Every command you run here stays in the database; it is not run on a clean copy and discarded as in the lesson exercises. A change you COMMIT is still in effect in later cells. Write with that in mind:
ROLLBACK; line at the top of the transaction cells closes a broken transaction on the next run; if no transaction is open, it only gives a warning and changes nothing.-- Check query lines at the end of the cells.What you'll do
Required topics
scholarship.sqlDownload file
Reading file…
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
The scholarship board is at the first meeting of the new term. Before moving on to decisions, it wants to see the state of every fund in one table: the award amount per term, the money remaining in the fund, how many applications are waiting for a decision, and how many more full scholarships the remaining money can pay for.
This step only reads. Once you have written the table, ask yourself: which fund has many applications but no money, and which has money but few waiting applications? In the next steps you will change this table with the board's decisions.
This step's code cell is for members.Sign up free
The board approved application 10 (score 98) for the Merit Scholarship. One approval means three changes: the application's status becomes approved, a payment row is written to the ledger, and the fund's remaining money drops by the award amount. If one happens and the others do not, the ledger and the fund no longer agree; so all three must happen together or not at all.
Values of the ledger row: id 201, fund 1, application 10, amount -600 (money leaving the fund is written as negative), description 'Scholarship payment: application 10', date 2026-12-01.
What happens if you run the cell a second time? Row 201 already exists in the ledger; the INSERT is rejected and the whole transaction breaks. That way the same scholarship is not paid twice and the fund is not charged twice. If you see an error message, do not worry: nothing changed.
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
This step's code cell is for members.Sign up free
The board's second decision: at the Science Scholarship, applications with a score below 60 that are still waiting for a decision will be rejected. A clerk prepared the command and acted wisely: they ran it inside a transaction and looked at the result before confirming. Their screen shows this table:
| fund_id | rejected |
|---|---|
| 1 | 5 |
| 2 | 5 |
| 3 | 4 |
| 4 | 5 |
Does this table match the board's decision? One of fund 3's four rejections is a student who won a scholarship last term with a portfolio and is approved right now. The clerk's transaction is ready in the cell; you decide how to end it. Then do what the board asked in a separate transaction that touches only the requested applications, and confirm it.
This step's code cell is for members.Sign up free
Remember the problem in the first table: the Science Scholarship has many applications waiting but no money to pay a single award, while the Arts Scholarship has money piled up. With the donors' approval, the board decided to transfer $1,000 from the Arts Scholarship (fund 3) to the Science Scholarship (fund 2).
Each fund gets its own ledger row: row 202 is $1,000 leaving fund 3 (description 'Transfer out'), row 203 is $1,000 entering fund 2 (description 'Transfer in'); both have the date 2026-12-02 and no application. The funds' remaining amounts change accordingly. The check query puts the remaining amount of the two funds next to the ledger total: if the transfer is right, the two are equal for each fund.
This step's code cell is for members.Sign up free
After the transfer, the Science Scholarship has $1,250. The board is discussing the three highest-scoring pending applications (21, 18 and 33); the clerk has prepared code that approves all three in one transaction. First run the cell as it is and read the error message. Which command was rejected, and because of which rule? Did the approvals of applications 21 and 18 survive this error?
One more question: if you had run a SELECT in a new cell right after the error, what would you see? The transaction stays broken and every command is rejected until it is closed. That is why the cell starts with a ready-made ROLLBACK; line: on the next run it closes the broken transaction.
The board's rule is clear: a candidate whose budget does not cover the award is not rejected but put on the waitlist; they are called first when the spring donations arrive. Fix the transaction by this rule: the two approvals that have money stay as they are.
This step's code cell is for members.Sign up free
At the Merit Scholarship, two decisions will be made in the same transaction. First, application 4 (score 95) will be approved; this part is ready and correct in the cell. Then, because the remaining money covers only one more award, the board asked that applications with a score of 70 or higher that are still waiting for a decision be put on the waitlist.
The clerk wrote the waitlist command and looked at the result inside the transaction:
| status | applications |
|---|---|
| pending | 6 |
| waitlist | 11 |
Not a single approved application is left: six scholarship students, including the newly approved number 4, were dropped to the waitlist. Rolling back the whole transaction would also erase the approval of application 4. What you are asked to do: put a savepoint after the approval, rewind the effect of the wrong command only back to that point, do what the board asked with the right condition, and confirm the transaction.
This step's code cell is for members.Sign up free
Since the start of the project you have made every change with a transaction; but this system ran without transactions last term. The board's auditor asks: is each fund's remaining value equal to the sum of its rows in the ledger? If not, somewhere a job was left half done.
This audit will be asked again at the end of every term; instead of writing the query each time, save it as a view named fund_audit. Its columns: id, name, remaining, ledger_total (the sum of the amounts in the ledger) and difference (remaining minus ledger total). A view is also a change: in PostgreSQL it can be created inside a transaction and is not permanent until confirmed. The transaction is ready in the cell; you write the view.
Interpret the result: in which fund is the difference not zero, by how many dollars and in which direction? If remaining is larger than the ledger, what might have happened?
This step's code cell is for members.Sign up free
Follow the trail of the difference in the Sports Scholarship. There is a scholarship payment in the ledger, but the application behind this payment still does not look approved, and the fund's remaining amount does not reflect the payment at all. The system that ran without transactions last term did only one of the three changes and stopped midway. Accounting confirmed the payment was really made: the ledger row is correct, and the other two changes are missing.
First find which application it is (you can search with a SELECT in the cell; only the result of the last query is shown). Then make the two missing changes in one transaction and confirm it. The check query shows that the difference drops to zero in all funds and that no payment is left for an application that is not approved.
If you run this cell twice by accident, the fund should not be charged twice: consider writing the remaining amount by calculating it from the ledger instead of "reduce by this much".
This step's code cell is for members.Sign up free
The board's last request is the term summary: in each fund, how many applications were approved, how many are on the waitlist, how many were rejected, and how many are still waiting for a decision; how much money is left in the fund and how many more full awards that money can pay.
Once you have written the report, compare it with the table from the first step. For which fund should a donation campaign be opened in the spring term? The fund with the longest waitlist, the fund whose number of affordable awards is zero, or are they the same fund? Note in one sentence which columns of the report you base your answer on.
This step's code cell is for members.Sign up free