CASE Expression

SQL · Conditional Expressions

Theory

CASE brings if / else logic into SQL. The searched form checks conditions in order: CASE WHEN salary > 80000 THEN 'Senior' WHEN salary > 60000 THEN 'Mid' ELSE 'Junior' END. The first WHEN that is TRUE decides the result, and later branches are skipped. If no branch matches and there is no ELSE, the result is NULL. The simple form compares one value against several: CASE dept WHEN 'IT' THEN 'Tech' WHEN 'HR' THEN 'People' END.

CASE is an expression, so it can go anywhere a value can: SELECT, WHERE, ORDER BY, GROUP BY and inside aggregates. SUM(CASE WHEN city = 'Jaipur' THEN amount ELSE 0 END) is the classic conditional sum, used to pivot rows into columns. All branches should return the same data type. Because order matters, put the most specific condition first.

Why we use it

Raw values often need labels, buckets or rules applied: salary bands, pass/fail, region names, custom sort orders. CASE applies that logic right in the query instead of in a spreadsheet afterwards.

When to use it

Use CASE for bucketing, labelling, conditional aggregation and custom sorting. For simple NULL replacement, COALESCE is shorter. For long mappings that change often, a lookup table joined in is easier to maintain.

How it works

  1. Evaluate CASE for a row
  2. Is the first WHEN condition TRUE? Yes: Return its THEN value. No: Check the next WHEN.
  3. Is any later WHEN TRUE? Yes: Return the first matching THEN value. No: Go to ELSE.
  4. Return the ELSE value, or NULL if there is no ELSE
  5. Move to the next row

Syntax

CASE
  WHEN condition THEN result
  ELSE default
END

Example

SELECT name,
  CASE WHEN salary > 80000 THEN 'Senior' ELSE 'Junior' END AS level
FROM employees;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic