0Pricing
SQL Interview Prep · Lesson

CASE Expressions in SELECT

Writing inline conditional logic and the searched vs simple CASE forms.

CASE: SQL's Conditional Expression

CASE is SQL's if/then/else, but it's an expression that returns one value — so it works anywhere a value can: SELECT, WHERE, ORDER BY, even inside aggregates.

SELECT order_id,
       amount,
       CASE WHEN amount > 100 THEN 'large' ELSE 'small' END AS size
FROM orders;

Searched CASE Form

The searched CASE checks a boolean per WHEN and returns the first match. It's the most flexible form, and order matters — the first true condition wins.

SELECT score,
       CASE
         WHEN score >= 90 THEN 'A'
         WHEN score >= 80 THEN 'B'
         WHEN score >= 70 THEN 'C'
         ELSE 'F'
       END AS grade
FROM exam_results;

All lessons in this course

  1. Projecting Columns and Aliasing Pitfalls
  2. Computed Columns and Expression Precedence
  3. DISTINCT vs GROUP BY for Uniqueness
  4. CASE Expressions in SELECT
← Back to SQL Interview Prep