Total PageViews

Tuesday, August 20, 2019

How to Use Case Statement with Order By Clause in Oracle?


Suppose we have a condition to get the result from SQL query on the basis of check conditions.
E.g:- we have a table called EmpoyeeDetails
EmployeeDetails
Id
Name
101
Ravi
102
Yash
103
Harsh

Condition:- We have a condition to fetch the Id, Name from EmployeeDetails table when Name is “Yash” then it will come in first priority, when the name is “Ravi” then it will come in the second priority then rest of the records.
Query:-  select emp.* from EMPLOYEESDETAILS emp ------> (Emp is alias of the table)
               order by case when emp.Name='Yash' then 1
                                       when emp.Name='Ravi' then 2 else 3
                               end;
O/P:-
EmployeeDetails
Id
Name
102
Yash
101
Ravi
103
Harsh

No comments:

Post a Comment