SQL Practice Logo

SQLPractice Online

CASE Statements & Conditional Logic: Functions

Module: SQL Fundamentals

SELECT

employee_id,

CASE department

WHEN 'Sales' THEN 'Revenue'

WHEN 'Engineering' THEN 'Product'

ELSE 'Other'

END AS division

FROM employees;

SELECT

employee_id,

salary,

CASE

WHEN salary > 100000 THEN 'High'

WHEN salary > 50000 THEN 'Medium'

ELSE 'Low'

END AS salary_band

FROM employees;

SELECT * FROM employees

WHERE CASE

WHEN department = 'Sales' THEN salary > 60000

WHEN department = 'Engineering' THEN salary > 80000

ELSE salary > 50000

END;

CASE evaluates top-to-bottom, stops at first TRUE

ELSE clause optional (returns NULL if no match)

All THEN results must be same data type

Can be used in SELECT, WHERE, ORDER BY, GROUP BY

Simple CASE: compares expression to values

Searched CASE: evaluates boolean conditions

Core references in this topic include WHERE, =, <, >, <=, >=. Learn what each one does, when to use it, and the execution or engine rules that matter.

WHERE

Filters rows before projection and sorting. It decides which rows continue through the query pipeline.

SELECT ... FROM table WHERE condition;

Most performance issues start with a weak WHERE clause or a missing supporting index.

=

Returns rows where the left and right values are exactly equal.

column = value

Use with exact matches. Do not use = NULL.

<, >, <=, >=

Range comparison operators for less-than, greater-than, and inclusive boundary checks.