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

Build a Precedence-Safe Operations Work Queue

Create workstream, failed_flag, and assigned_team using explicit precedence and defaults.

  • CASE expressions
  • NULL handling
  • Sorting

Exercise brief

Understand the request

Data operations director A single work queue must classify the next action, expose a failure flag, and route unowned work to triage.

A single work queue must classify the next action, expose a failure flag, and route unowned work to triage. Create workstream, failed_flag, and assigned_team using explicit precedence and defaults.

Return

  • Return event_id, pipeline_name, workstream, failed_flag, and assigned_team.
  • Order by event_id.

Constraints

  • Workstream precedence is Data Quality, Maintenance, Incident, Needs Measurement, Needs Target, Performance, then Normal.
  • Use independent CASE expressions for the flag and assignment fallback.

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

Write the workstream CASE from highest to lowest business precedence.

Hint 2

Keep failed_flag as its own 1/0 CASE.

Hint 3

Use a third CASE to replace NULL owner_team with triage.

Verified SQL answer

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

Reveal solution and explanation
SELECT event_id, pipeline_name, CASE WHEN status_code IS NULL OR status_code = '' THEN 'Data Quality' WHEN maintenance_mode = 1 THEN 'Maintenance' WHEN status_code = 'FAIL' OR severity = 'critical' THEN 'Incident' WHEN actual_ms IS NULL OR target_ms IS NULL THEN 'Needs Measurement' WHEN target_ms <= 0 THEN 'Needs Target' WHEN actual_ms > target_ms THEN 'Performance' ELSE 'Normal' END AS workstream, CASE WHEN status_code = 'FAIL' THEN 1 ELSE 0 END AS failed_flag, CASE WHEN owner_team IS NULL THEN 'triage' ELSE owner_team END AS assigned_team FROM pipeline_events ORDER BY event_id;

Why this works

The capstone treats CASE as a production transformation tool: precedence resolves overlaps, independent expressions preserve reusable attributes, and explicit fallbacks keep downstream contracts non-NULL.

Success check

Every adversarial row receives one explainable workstream, a numeric failure flag, and a non-NULL assigned team.

Expected result

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

event_idpipeline_nameworkstreamfailed_flagassigned_team
101orders_ingestNormal0core
102orders_ingestNormal0core
103orders_ingestIncident0core
104orders_ingestIncident1core
208customer_syncNormal0triage
209customer_syncIncident1triage
210customer_syncNormal0data
303billing_rollupData Quality0finance
304billing_rollupData Quality0finance
305billing_rollupIncident1finance

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.