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

Map Pipeline Status Codes with Simple CASE

Use simple CASE to map OK, WARN, FAIL, and RUNNING while preserving an Unmapped fallback.

  • CASE expressions
  • Sorting

Exercise brief

Understand the request

Data platform analyst An operations feed exposes compact status codes that need readable labels.

An operations feed exposes compact status codes that need readable labels. Use simple CASE to map OK, WARN, FAIL, and RUNNING while preserving an Unmapped fallback.

Return

  • Return event_id, status_code, and status_label.
  • Order by event_id.

Constraints

  • Use CASE status_code WHEN value rather than searched CASE.
  • Map every unrecognized value, blank, and NULL to Unmapped with ELSE.

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

Simple CASE is best when every branch compares one expression for equality.

Hint 2

Start with CASE status_code and add one WHEN literal per known code.

Hint 3

Finish with ELSE Unmapped so nonmatches do not silently become NULL.

Verified SQL answer

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

Reveal solution and explanation
SELECT event_id, status_code, CASE status_code WHEN 'OK' THEN 'Succeeded' WHEN 'WARN' THEN 'Needs Attention' WHEN 'FAIL' THEN 'Failed' WHEN 'RUNNING' THEN 'In Progress' ELSE 'Unmapped' END AS status_label FROM pipeline_events ORDER BY event_id;

Why this works

Simple CASE is a compact code-to-label lookup. Equality with NULL is never true, so the explicit ELSE safely absorbs missing and unexpected codes.

Success check

Every event has the requested label and the NULL, blank, and UNKNOWN codes use the fallback.

Expected result

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

event_idstatus_codestatus_label
101OKSucceeded
102RUNNINGIn Progress
103WARNNeeds Attention
104FAILFailed
208WARNNeeds Attention
209FAILFailed
210OKSucceeded
303NULLUnmapped
304Unmapped
305FAILFailed

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.