Sql Case — Free Sql Tutorial
Learn Sql Case in Sql with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.
TL;DR: Learn Sql Case in Sql with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.
Written & reviewed by the Syllab.in Academic Team (CBSE/NCERT subject experts) · Updated
Sql Case in Sql
The CASE expression adds if/else logic to SQL queries. It lets you transform or categorise data on the fly without needing to create a separate column or table.
CASE WHEN condition THEN result ELSE default END is the general form. You can have multiple WHEN clauses. The ELSE is optional (returns NULL if no WHEN matches and ELSE is absent).
Use CASE for: Grade assignment (85+ = A, 70+ = B, etc.), Categorising products by price range, Converting codes to labels (1='Male', 2='Female'), Custom sorting orders, Computing different values based on conditions.
Sql Case — Syntax
-- Simple CASE (like switch): CASE column WHEN value1 THEN result1 WHEN value2 THEN result2 ELSE default_result END -- Searched CASE (like if/elif/else): CASE WHEN marks >= 90 THEN 'A+' WHEN marks >= 80 THEN 'A' WHEN marks >= 40 THEN 'Pass' ELSE 'Fail' END
Learn Sql Case step by step with Syllab's free interactive Sql tutorial — runnable code examples, practice exercises and instant AI feedback, all free with no signup. Explore the full Sql course →