almost 9 years ago
Case Statement:
Case Statement is used to provide IF-ELSE functionality in a SQL query to fetch result. We provide multiple conditions and according to condition matches the value will change for that that column value.
Syntax:
Example:
- SELECT EmpName, CASE EmpName
- WHEN 'Sam' THEN 'Salary * 2'
- WHEN 'Mitchell' THEN 'Salary * 1'
- ELSE 'Salary+1000'
- END
- "RevisedSalary"
- Ctiy
- FROM Employee;
SELECT EmpName, CASE EmpName WHEN 'Sam' THEN 'Salary * 2' WHEN 'Mitchell' THEN 'Salary * 1' ELSE 'Salary+1000' END "RevisedSalary" Ctiy FROM Employee;
Output:
- EmpName RevisedSalary City
- -------------------------------------------------------------------------
- Sam 30000 Boston
- Shaun 21000 Texas
- Mitchell 40000 Boston
- Bucky 21000 Boston
EmpName RevisedSalary City ------------------------------------------------------------------------- Sam 30000 Boston Shaun 21000 Texas Mitchell 40000 Boston Bucky 21000 Boston
In the above example we used CASE with EmpName column. When EmpName is SAM then Salary will get double for that employee, When EmpName is Mitchell then Salary will remain as it is for that employee and for other employees the salary will incremented by 1000.
0 Comment(s)