LeetCode 176: Second Highest Salary [SQL]

แชร์
ฝัง
  • เผยแพร่เมื่อ 12 ม.ค. 2025

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

  • @frederikmuller
    @frederikmuller  2 ปีที่แล้ว

    If you want to have access to more free SQL problems, check out StrataScratch: stratascratch.com/?via=frederik

  • @Kaviarasu_S
    @Kaviarasu_S 10 หลายเดือนก่อน

    Watching Frederik explain feels like a meditative session ❤

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

    Very good explanation. Really solved all my confusion about this problem. Keep going like. Really enjoy content like this!

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

    5: 14 updated
    Use DISTINCT Keyword

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

    I am really appreciate your work. Are you going to make all SQL problems on Leetcode (153 questions) ? Thank you!

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

      yes, I'm planning to go through all of them!

    • @khoanguyen1194
      @khoanguyen1194 3 ปีที่แล้ว

      @@frederikmuller Thanks sir, its so great I tried to study a lot sources I feel I understand them but when I actually work on real problem I feel I dont know anything and really appreciate your explanation for every single problems.

    • @frederikmuller
      @frederikmuller  3 ปีที่แล้ว

      @@khoanguyen1194 I'm glad it's helpful! I think the most important thing is to learn how to approach a problem so you can apply your knowledge to any problem.

  • @truptipradhan86
    @truptipradhan86 2 ปีที่แล้ว

    Thanks for explaining the solution really well. Just thought of another solution with rank function.
    SELECT
    case when b.salary is null then null else b.salary end as salary
    FROM
    (select 2 as rnk) as a
    LEFT OUTER JOIN
    (select salary,rnk from
    (select
    salary,
    rank() over(order by salary desc) as rnk
    from
    dbo.Employee
    ) a
    where rnk = 2
    ) as b
    on a.rnk = b.rnk
    Got bit messy as the requirement was if there is no second highest salary , return NULL.

    • @ft_peakhd2921
      @ft_peakhd2921 2 ปีที่แล้ว

      select ifnull((select distinct salary from table_name ordre by salary desc limit 1 offset 1),null) as second_highest_salary
      This will give you NULL as return incase if there's no second highest. simple but effective one

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

    while selecting " where salary not in select MAX(Salary) from employee , why it does not includes 3rd also as we did not set any limit

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

    I think the second approach did not work because salary column can contain duplicate salaries so when you write offset 1 it will skip 1 row but because of duplicate entries your limit 1 will give you again same salary.. I think you are getting me.??

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

      Try "Select distinct"

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

    -- Write your PostgreSQL query statement below
    select max(salary) as SecondHighestSalary
    from Employee
    where salary < (select max(salary) from Employee)

  • @vichitravirdwivedi
    @vichitravirdwivedi 2 ปีที่แล้ว

    Wonderful explaination. As a recommendation,
    Please zoom in. It is difficult to see the letters clearly

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

    Nice explanation my brother

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

    What an Amazing explanation. Pl take more problems from leetcode, scratascatch and hackerrank. You explain perfectly. May Shree Raam bless you with joy and happiness..❤

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

    now this is a medium level question ...tricky but fun

  • @DarkKnight-xl5rj
    @DarkKnight-xl5rj 2 ปีที่แล้ว

    Whats gonna happen to the 100? as it is also less than the max salary 300?

  • @VsEdits59
    @VsEdits59 2 ปีที่แล้ว

    man these are too good 🙌

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

    Great explanation

  • @CoumbaSACKO
    @CoumbaSACKO 2 ปีที่แล้ว

    Hello Frederik,
    Thank you so much for your content; very helpful.
    I have a question for this video, why did you use the WHERE Clause with an aggregation 'AVG.'? I am a bit confused here.

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

      Do you mean MAX()? Since the aggregation is in a separate subquery that will be evaluated first, it’s fine. But you’re right, you’re not able to use aggregation functions in a where clause otherwise.

    • @CoumbaSACKO
      @CoumbaSACKO 2 ปีที่แล้ว

      @@frederikmuller Thank you so much; I appreciate your quick feedback.

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

    Are you not making anymore videos on SQL ?

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

      I'll start with the premium SQL questions soon ;)

    • @alkamaahmed2872
      @alkamaahmed2872 4 ปีที่แล้ว

      @@frederikmuller That's great !!

  • @khoanguyen1194
    @khoanguyen1194 2 ปีที่แล้ว

    Hello Frederik, Are you still making premium leetcode SQL problems? Thank you

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

      Not at the moment. I feel like I’ve covered all major question types on LeetCode and I have a sponsorship with StrataScratch which is why I’m prioritizing SS. Also more questions on there!

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

    Second highest salary
    create table Person
    (id int not null primary key,
    Salary INt
    );

    insert into person VALUES(1,1000),(2,2000),(3,3000),(4,4000),(5,5000);

    select * from person;

    select salary
    FROM
    (
    select id,salary,rank() over(order by salary desc) as rnk
    from person
    ) A
    where A.rnk = 2;
    This is a right approach or not please any one can tell me

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

      Hey I did this but it failed for me in Case 2, because it doesn't print NULL in case there is no data in the table

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

    SELECT MAX Salary as SecondHighestSalary
    FROM employes
    WHERE salary NOT IN(SELECT MAX Salary) FROM employees

  • @muhammadsalar607
    @muhammadsalar607 2 ปีที่แล้ว

    Wasn't aware Fardeen khan taught SQL in his younger days..

  • @mrprime557
    @mrprime557 2 ปีที่แล้ว

    AWESOME !

  • @tttttt6808-l7u
    @tttttt6808-l7u 4 ปีที่แล้ว

    very nice thanks!"

  • @stellaueda4565
    @stellaueda4565 4 ปีที่แล้ว

    Tricky indeed

  • @АлексейСоловьев-о9д
    @АлексейСоловьев-о9д 3 ปีที่แล้ว

    Thanks

  • @ceyhunozturk5115
    @ceyhunozturk5115 2 ปีที่แล้ว

    Thanks