How to Use Sub Query in Case Expression


This query returns “High” and “Low” based on their salary compared to the average salary of all employees.

SELECT employee_id, first_name,
CASE
WHEN Salary>=(SELECT Avg(Salary) From Employees)
THEN ‘High’
ELSE ‘Low’  END As PAY,
Salary From Employees

SELECT employee_id,Last_Name,
 (CASE
  WHEN Department_Id =
  (SELECT Department_ID FROM Departments WHERE 
  LOCATION_ID=1800)
   THEN ‘Canada’ ELSE ‘USA’ END) Locations
 FROM Employees

Leave a Reply

Your email address will not be published. Required fields are marked *