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

ความคิดเห็น • 9

  • @rushikeshjaisur6576
    @rushikeshjaisur6576 2 ปีที่แล้ว +6

    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;

    • @EverydayDataScience
      @EverydayDataScience  2 ปีที่แล้ว +4

      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.

    • @sivasrimakurthi206
      @sivasrimakurthi206 ปีที่แล้ว

      @@EverydayDataScience makes sense. That is the whole purpose of using IFNULL.

  • @meenayegan6634
    @meenayegan6634 2 ปีที่แล้ว +3

    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

  • @mohammedjunaidazhar1844
    @mohammedjunaidazhar1844 9 หลายเดือนก่อน

    SELECT MAX(salary) AS SecondHighestSalary
    FROM Employee
    WHERE salary < (SELECT MAX(salary) FROM Employee)
    This query is working too.

  • @chiragshetty3079
    @chiragshetty3079 ปีที่แล้ว +1

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

  • @sakshamp4488
    @sakshamp4488 ปีที่แล้ว +1

    thankyou sir

  • @ujjwalvarshney3188
    @ujjwalvarshney3188 ปีที่แล้ว

    select max(case when x.ct >2 and x.rk=2 then salary when x.ct