How to Write a Case Statement in Redshift

Case statements are useful when you're dealing with multiple IF statements in your select clause. It comes in two forms:

-- Redshift documentation calls this form a searched CASE expression.
    WHEN score < 70 THEN 'failed'
    WHEN score BETWEEN 70 AND 80 THEN 'passed'
    WHEN score BETWEEN 81 AND 90 THEN 'very good'
    ELSE 'outstanding'
  END AS performance
FROM test_scores;

-- Redshift documentation calls this for a simple case expression
  CASE grade
    WHEN 'A' THEN 'Excellent'
    WHEN 'B' THEN 'Good'
    WHEN 'C' THEN 'Needs Improvement'
    ELSE 'Failed'
  END AS grade_interpretation
FROM grades;
database icon
Real-time SQL collaboration is here
Get started with PopSQL and Redshift in minutes