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

Add and Backfill Account Risk

Add risk_rating VARCHAR(10), then set it to high for credit limits at least 20000 and standard otherwise.

  • Schema migration
  • Data backfill
  • CASE expressions

Exercise brief

Understand the request

Credit operations The new risk_rating column must be introduced into a populated table without leaving old accounts unclassified.

Credit operations is introducing a risk label to every existing customer account.

Return

  • Evolve the schema and backfill all existing rows in one script.

Constraints

  • Run ALTER before UPDATE.
  • Do not change credit_limit.

Data you will use

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

accounts

  • account_idINTEGER
  • credit_limitINTEGER

Hints, when you need them

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

Hint 1

This is a two-step migration: expand the schema first, then populate the new field.

Hint 2

Use ALTER TABLE ... ADD COLUMN, followed by UPDATE with a CASE expression.

Hint 3

ALTER TABLE accounts ADD COLUMN risk_rating VARCHAR(10); UPDATE accounts SET risk_rating = CASE ... END;

Verified SQL answer

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

Reveal solution and explanation
ALTER TABLE accounts ADD COLUMN risk_rating VARCHAR(10);
UPDATE accounts
SET risk_rating = CASE
  WHEN credit_limit >= 20000 THEN 'high'
  ELSE 'standard'
END;

Why this works

Separating the schema change from the backfill mirrors a safe migration workflow and makes each operation auditable.

Success check

Every account has the correct risk_rating derived from its existing credit limit.

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.