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.
- 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 → - 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 → - 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 → - 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 → - 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 → - 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 → - 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 → - 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 → - 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 → - 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 → - 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 → - 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 → - 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 →
Attempt
Write a query or design from the brief before opening any hints.
Validate
Run it against the included dataset and inspect the output.
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
Build the next SQL skill
WHERE Clause & Filtering
Practice SQL WHERE clauses with realistic boundary, NULL, text, date, exclusion, and production-filtering problems.
Basic SQL Functions
Practice production-oriented string cleanup, numeric transformations, NULL handling, tolerant conversion, and delimiter parsing.
Database Changes
Practice safe SQL data changes, schema evolution, retry-safe upserts, transactions, rollback, views, and indexes in isolated databases.
Open the interactive workspace and practice across SQL topics.