CASE Statements & Conditional Logic SQL Topic exerciseMediumVerified answerSQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

Apply First-Match Incident Priority

Assign P1 to failures or critical severity, P2 to customer-facing warnings, P3 to remaining warnings, and Monitor otherwise.

  • CASE expressions
  • Sorting

Exercise brief

Understand the request

Incident management lead Overlapping alert rules must resolve to one deterministic incident priority.

Overlapping alert rules must resolve to one deterministic incident priority. Assign P1 to failures or critical severity, P2 to customer-facing warnings, P3 to remaining warnings, and Monitor otherwise.

Return

  • Return event_id, status_code, severity, is_customer_facing, and incident_priority.
  • Order by event_id.

Constraints

  • Place the broad P1 rule before the warning rules.
  • Keep the customer-facing warning branch before the remaining-warning branch.

Data you will use

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

pipeline_events

  • event_idINTEGER
  • pipeline_nameTEXT
  • status_codeTEXT
  • severityTEXT
  • actual_msINTEGER
  • target_msINTEGER
  • retry_countINTEGER
  • processed_rowsINTEGER
  • failed_rowsINTEGER
  • owner_teamTEXT
  • is_customer_facingINTEGER
  • source_systemTEXT
  • maintenance_modeINTEGER

Hints, when you need them

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

Hint 1

CASE stops at the first true WHEN.

Hint 2

Start with status_code = FAIL OR severity = critical.

Hint 3

Put customer-facing WARN before the generic WARN branch.

Verified SQL answer

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

Reveal solution and explanation
SELECT event_id, status_code, severity, is_customer_facing, CASE WHEN status_code = 'FAIL' OR severity = 'critical' THEN 'P1' WHEN status_code = 'WARN' AND is_customer_facing = 1 THEN 'P2' WHEN status_code = 'WARN' THEN 'P3' ELSE 'Monitor' END AS incident_priority FROM pipeline_events ORDER BY event_id;

Why this works

Overlapping decision rules are encoded by branch order. A dataset row that satisfies both P1 and P2 makes incorrect precedence observable instead of coincidentally passing.

Success check

The critical warning is P1 rather than P2, proving that the first matching branch wins.

Expected result

Use this output to verify values, aliases, ordering, and row count.

event_idstatus_codeseverityis_customer_facingincident_priority
101OKlow1Monitor
102RUNNINGmedium1Monitor
103WARNcritical1P1
104FAILcritical1P1
208WARNhigh0P3
209FAILhigh0P1
210OKmedium0Monitor
303NULLmedium1Monitor
304NULL1Monitor
305FAILmedium1P1

Previewing 10 of 14 expected rows. Run the query in the editor to inspect the full result.

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.