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

Classify Pipeline Health with Searched CASE

Classify missing codes as Unknown, failures as Failed, warnings or three-plus retries as Degraded, running events as Active, and all others as Healthy.

  • CASE expressions
  • NULL handling
  • Sorting

Exercise brief

Understand the request

Reliability reporting analyst A monitoring view needs one health state derived from status and retry signals.

A monitoring view needs one health state derived from status and retry signals. Classify missing codes as Unknown, failures as Failed, warnings or three-plus retries as Degraded, running events as Active, and all others as Healthy.

Return

  • Return event_id, status_code, retry_count, and health_state.
  • Order by event_id.

Constraints

  • Use searched CASE with an explicit ELSE.
  • Test missing and blank status codes before all operational branches.

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

Searched CASE supports different predicates in each WHEN.

Hint 2

Put the NULL-or-blank test first and keep the failure branch ahead of the retry branch.

Hint 3

Use ELSE Healthy only after every exceptional state has been handled.

Verified SQL answer

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

Reveal solution and explanation
SELECT event_id, status_code, retry_count, CASE WHEN status_code IS NULL OR status_code = '' THEN 'Unknown' WHEN status_code = 'FAIL' THEN 'Failed' WHEN status_code = 'WARN' OR retry_count >= 3 THEN 'Degraded' WHEN status_code = 'RUNNING' THEN 'Active' ELSE 'Healthy' END AS health_state FROM pipeline_events ORDER BY event_id;

Why this works

Searched CASE is the standard form for multi-column classification. An explicit missing-data branch prevents SQL three-valued logic from turning unknown inputs into an accidental healthy label.

Success check

Each event receives exactly one state and missing data cannot fall through to Healthy.

Expected result

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

event_idstatus_coderetry_counthealth_state
101OK0Healthy
102RUNNING1Active
103WARN2Degraded
104FAIL3Failed
208WARN2Degraded
209FAIL4Failed
210OK0Healthy
303NULLNULLUnknown
3041Unknown
305FAIL1Failed

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.