You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN condition3 THEN result3
ELSE result4
END;
Search Condition
SELECT first_name, last_name, age,
CASE
WHEN salary <300000 THEN 'Nil'
WHEN salary BETWEEN 300000AND600000 THEN '5%'
WHEN salary BETWEEN 600000AND900000 THEN '10%'
WHEN salary BETWEEN 900000AND1200000 THEN '15%'
WHEN salary BETWEEN 1200000AND1500000 THEN '20%'
ELSE '30%'
END AS tax
FROM payroll;
-- While mapping with the same column:SELECT first_name, last_name, age,
CASE gender
WHEN 'F' THEN 'Female'
WHEN 'M' THEN 'Male'
ELSE 'Transgender'
END AS gender
FROM employee;
Update using CASE
-- Updating the column value based on conditions:UPDATE employees
SET rating = CASE
WHEN RATE <3 THEN 'Satisfactory'
WHEN RATE =3 THEN 'Acceptable'
WHEN RATE >3 THEN 'Excellent'
ELSE "Okay"
END;
DECODE
--- Syntax:SELECT DECODE (ColumnName, IF, THEN value, ELSE IF, THEN value, ELSE, Value)
AS Alias
FROM TableName
--- Example:SELECT DECODE (grade, 'A', 1, `B`, 2, `C`, 3, `D`, 4, 0)
AS points
FROM students
DECODE vs CASE
DECODE
CASE
Function
Statement
Only works for = operator
Works for all the logical =, <>, >, >=, <, <= operators