LeetCode Medium 176 Amazon Interview SQL Question with Detailed Explanation
ฝัง
- เผยแพร่เมื่อ 5 ก.ย. 2024
- Question: leetcode.com/p...
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;
Yes, this query with MAX will work and take care of the null issue. Great catch. I wanted to put in a generalized solution so that if instead of "null", the question asks to return something else, then all all we have to do is replace that specified_value to the value asked.
@@EverydayDataScience makes sense. That is the whole purpose of using IFNULL.
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
SELECT MAX(salary) AS SecondHighestSalary
FROM Employee
WHERE salary < (SELECT MAX(salary) FROM Employee)
This query is working too.
can anyone hellp me with solution for mssql, i'm trying with offset but not getting the solution
thankyou sir
Glad that you found the video useful 😊
select max(case when x.ct >2 and x.rk=2 then salary when x.ct