Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Case Expression in SQL

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 436
    Comment on it

    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:

    1. SELECT CASE (columnname)
    2. WHEN value1 THEN result1
    3. WHEN value2 THEN result2
    4. ...
    5. ELSE resultN
    6. END
    7. FROM tablename;

    Example:

    1. SELECT EmpName, CASE EmpName
    2. WHEN 'Sam' THEN 'Salary * 2'
    3. WHEN 'Mitchell' THEN 'Salary * 1'
    4. ELSE 'Salary+1000'
    5. END
    6. "RevisedSalary"
    7. Ctiy
    8. FROM Employee;

    Output:

    1. EmpName RevisedSalary City
    2. -------------------------------------------------------------------------
    3. Sam 30000 Boston
    4. Shaun 21000 Texas
    5. Mitchell 40000 Boston
    6. 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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: