LeetCode Medium 176 Amazon Interview SQL Question with Detailed Explanation

preview_player
Показать описание

In this video I solve and explain a medium difficulty leetcode SQL question using MySQL query. This question has been asked in Apple, Facebook, Amazon, Google, Adobe, Microsoft, Adobe interviews or what we popularly call FAANG interviews.
I explain the related concept as well. This question includes points to keep in mind to develop SQL queries.

LeetCode is the best platform to help you enhance your skills, expand your knowledge and prepare for technical interviews.

If you found this helpful, Like and Subscribe to the channel for more content.

#LeetCodeSQL #FAANG #SQLinterviewQuestions
Рекомендации по теме
Комментарии
Автор

We can use MAX(salary) to handle null .
with RankTable as
(select *, dense_rank() over(order by salary desc) as rnk from employee)

select MAX(salary) as SecondHighestSalary from RankTable where rnk = 2;

rushikeshjaisur
Автор

Alternate 1: case-when

select case when dr = 2
then salary
else null
end
as secondHighestSalary
from
(
select *,
DENSE_RANK() over (order by salary desc) dr
from employee
)x

Alternate 2: coalesce

select coalesce
((
select salary
from
(
select *,
DENSE_RANK() over (order by salary desc) dr
from employee
)x where dr=2
), null) as secondHighestSalary

meenayegan
Автор

SELECT MAX(salary) AS SecondHighestSalary
FROM Employee
WHERE salary < (SELECT MAX(salary) FROM Employee)

This query is working too.

mohammedjunaidazhar
Автор

can anyone hellp me with solution for mssql, i'm trying with offset but not getting the solution

chiragshetty
Автор

select max(case when x.ct >2 and x.rk=2 then salary when x.ct<2 then null end ) as SecondHighestSalary from
(
select *, Count(*) over() as ct, rank()over(order by salary desc) as rk
from Employee) as x

ujjwalvarshney