Interview SQL Queries


Query to find employees with second Highest salary:
Using inner query:
Select max(salary) as second highest_sal
from employees where salary < (select max(salary) from employees);

Using window function and CTE:
with sec_high as (
select employee_id,
employee_name,
dense_rank() over ( order by salary desc) as rank_sal
from employees );
select * from sec_high where rank_sal = 2;