codegurukool.com · Free coding tutorials & online compiler

Home › SQL Tutorial › SQL CASE

SQL CASE

If-then-else inside a query.

CASE returns a value depending on conditions, like if / else if / else. Functions such as COALESCE(x, y) (or IFNULL / ISNULL) replace NULL with a default.

Example code

SELECT id,
  CASE
    WHEN amount >= 50 THEN 'big'
    ELSE 'small'
  END AS size
FROM Orders;

Run this code in the online editor

← SQL GROUP BY and HAVINGSQL EXISTS, ANY and ALL →