Write SQL CASE statement.
The CASE statement used for conditions and returns a value when the first condition is met like
IF THE ELSE IF STATEMENT
Example
Table: Employee+----+-------+--------+--------------+| Id | Name | Salary | DepartmentId |+----+-------+--------+--------------+| 1 | Ram | 85000 | 1 || 2 | Henry | 80000 | 2 || 3 | Sam | 60000 | 2 || 4 | Shyam | 60000 | 1 || 5 | Geeta | 90000 | 1 || 6 | Sheet | 90000 | 1 || 7 | Leela | 80000 | 1 || 8 | Geeta | 70000 | 1 |+----+-------+--------+--------------+
Query: Defining the Employee GRADE based on SALARY
SELECT NAME,SALARY,(CASE WHEN(SALARY<50000) THEN 'E GRADE'WHEN(SALARY>50000 AND SALARY<=60000) THEN 'D GRADE'WHEN(SALARY>60000 AND SALARY<=70000) THEN 'C GRADE'WHEN(SALARY>70000 AND SALARY<=80000) THEN 'B GRADE'WHEN(SALARY>80000) THEN 'A GRADE' END) AS EMP_GRADEFROM EMPLOYEE
Result :
+-------+--------+--------------+| Name | Salary | EMP_GRADE |+-------+--------+--------------+| Ram | 85000 | A GRADE || Henry | 80000 | B GRADE || Sam | 60000 | D GRADE || Shyam | 60000 | D GRADE || Geeta | 90000 | A GRADE || Sheet | 90000 | A GRADE || Leela | 80000 | B GRADE || Geeta | 70000 | C GRADE |+-------+--------+--------------+
Related post
No comments:
Post a Comment