Database Changes SQL Topic exerciseMediumVerified answerSQLite + PostgreSQL live · 3 guided

Commit a Coordinated Account Upgrade

In one transaction, add 1000 to account 2's credit limit and insert order 505 for 1000 with pending status and date 2025-03-01.

  • Transactions
  • COMMIT
  • Atomicity

Exercise brief

Understand the request

Account operations The credit change and new order form one business event and must be committed together.

Northstar Growth's approved credit increase and its companion order must succeed as one unit.

Return

  • Commit both database changes as one unit of work.

Constraints

  • Use BEGIN before the changes.
  • Run UPDATE before INSERT.
  • Finish with COMMIT.

Data you will use

Review the relevant tables before deciding how to join, filter, or aggregate them.

accounts

  • account_idINTEGER
  • credit_limitINTEGER

orders

  • order_idINTEGER
  • account_idINTEGER
  • total_amountINTEGER
  • statusVARCHAR(20)
  • ordered_atDATE

Hints, when you need them

Open one clue at a time so you still do the reasoning.

Hint 1

Transaction boundaries group multiple changes into one atomic unit.

Hint 2

Use the sequence BEGIN, UPDATE, INSERT, COMMIT.

Hint 3

BEGIN; UPDATE accounts ...; INSERT INTO orders ...; COMMIT;

Verified SQL answer

Attempt the problem first, then compare structure and reasoning—not just syntax.

Reveal solution and explanation
BEGIN;
UPDATE accounts
SET credit_limit = credit_limit + 1000
WHERE account_id = 2;
INSERT INTO orders (order_id, account_id, total_amount, status, ordered_at)
VALUES (505, 2, 1000, 'pending', '2025-03-01');
COMMIT;

Why this works

The explicit transaction documents that both writes represent one business event and should become durable together.

Success check

Account 2 has a 13000 credit limit and order 505 exists after the transaction commits.

Learn the concepts behind this answer

Strengthen your understanding with these targeted learning topics:

Continue practicing

SQL Practice Online

Open the interactive workspace and practice across SQL topics.