SQL Topic collection

CASE Statements & Conditional Logic SQL Topic exercises

Build NULL-aware classifications, precedence-safe decisions, flags, scores, and guarded calculations with portable CASE expressions.

Exercises
13
Difficulty
Beginner to Advanced

What this collection tests

Skills and query patterns

Simple and searched CASE, first-match precedence, three-valued logic, boundary hygiene, typed results, composition, and shallow nesting

Curated exercises

Choose an exercise

Work in the live editor where supported. Advanced guided labs state their engine boundary before you open the workspace.

  1. Exercise 1Easy

    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

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  2. Exercise 2Easy

    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

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  3. Exercise 3Easy

    Bucket Latency at Exact SLA Boundaries

    Classify each event using actual_ms relative to target_ms, treating only values strictly above two times target as Severe Breach.

    • CASE expressions
    • NULL handling
    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  4. Exercise 4Easy

    Distinguish NULL Ownership from Named Teams

    Use searched CASE to label NULL owner_team as Unassigned, platform as Platform-Owned, and all other values as Assigned.

    • CASE expressions
    • NULL handling
    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  5. Exercise 5Medium

    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

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  6. Exercise 6Medium

    Combine Boolean Rules into a Response Action

    Suppress maintenance events, Page repeated failures, Ticket other failures, warnings, or three-plus retries, and Observe everything else.

    • CASE expressions
    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  7. Exercise 7Medium

    Emit Reusable Data Quality Flags

    Create failed, SLA-breach, and unowned 1/0 flags for every event.

    • CASE expressions
    • NULL handling
    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  8. Exercise 8Medium

    Compose CASE Expressions into a Risk Score

    Add five points for failure, severity points of 3/2/0, two points for three-plus retries, and one point for missing ownership.

    • CASE expressions
    • NULL handling
    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  9. Exercise 9Medium

    Model a Four-State SLA Outcome

    Return Not Measured, Invalid Target, Breached, or Met in that precedence order.

    • CASE expressions
    • NULL handling
    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  10. Exercise 10Medium

    Guard a Conditional Failure-Rate Calculation

    Return NULL for zero or missing processed_rows; otherwise calculate failed_rows as a percentage of processed_rows.

    • CASE expressions
    • NULL handling
    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  11. Exercise 11Hard

    Route Incidents with Nested CASE

    Branch first by is_customer_facing, then apply a smaller status-and-severity decision inside each branch.

    • CASE expressions
    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  12. Exercise 12Hard

    Combine Simple CASE and Searched CASE Points

    Map severity to 8/5/3/1/0 points with simple CASE, then add two points when retries are at least three.

    • CASE expressions
    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  13. Exercise 13Hard

    Build a Precedence-Safe Operations Work Queue

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

    • CASE expressions
    • NULL handling
    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
01

Attempt

Write a query or design from the brief before opening any hints.

02

Validate

Run it against the included dataset and inspect the output.

03

Explain

Compare the verified answer and explain each choice aloud.

Learn before practicing CASE Statements & Conditional Logic

Strengthen your understanding with these targeted learning topics:

Continue practicing

SQL Practice Online

Open the interactive workspace and practice across SQL topics.