Skip to content

Instantly share code, notes, and snippets.

@khuyentran1401
Created March 20, 2023 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khuyentran1401/b9098fd5e3b1df7b16785f39787373c1 to your computer and use it in GitHub Desktop.
Save khuyentran1401/b9098fd5e3b1df7b16785f39787373c1 to your computer and use it in GitHub Desktop.
/*
Write an SQL query to report the second highest salary from the Employee table. If there is no second highest salary, the query should report null.
*/
WITH rank_table AS (
SELECT
salary,
dense_rank() OVER (
order by
salary desc
) as rnk
from
employee
)
select
case
when max(rnk) = 1 then null
else salary
end as SecondHighestSalary
from
rank_table
where
rnk = 2;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment