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
    • 391
    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:

    SELECT CASE (columnname)
      WHEN value1 THEN result1
      WHEN value2 THEN result2
      ...
      ELSE resultN
      END
    FROM tablename;
    

    Example:

    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
    

    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
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: