Adjust Salaries Atomically

Engineering has an unallocated salary pool of 5000.00. Four changes have been approved and they take effect together:

  • Carol is leaving — remove her row and return her salary to the pool
  • Alice gets +4000.00, funded from the pool
  • Bob gets +3000.00, funded from the pool

Apply all of it inside a single transaction, then return the final state:

SELECT e.name, e.salary, p.unallocated
FROM employee e JOIN pool p ON p.department_id = e.department_id
ORDER BY e.name;

The pool may never go negative — a CHECK enforces it. The raises cost 7000.00 and the pool holds 5000.00, so the sequence has to be arranged for the constraint to hold after every statement, not just at COMMIT.

Expected output


name  | salary   | unallocated
------+----------+------------
Alice | 94000.00 | 4000.00
Bob   | 83000.00 | 4000.00
Dan   | 70000.00 | 4000.00
PostgreSQL

Press Run to start the SQL engine.

SensAI
Beta
Tap the mic to stop voice mode