LeetCode 177: Nth Highest Salary [SQL]

แชร์
ฝัง
  • เผยแพร่เมื่อ 9 พ.ย. 2020
  • Solution and walkthrough of leetcode database problem 177: Nth Highest Salary. I'm using MySQL but this solution should work in any SQL dialect such as PostgreSQL SQL Server, etc.
    Link to the problem: leetcode.com/problems/nth-hig...
    Check out my StrataScratch videos: • StrataScratch Coding Q...

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

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

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

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

    clear, thorough and quick to digest especially at 1.25 speed :D keep up the very helpful and excellent content! thx Frederik

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

    Love your videos - much love from Berkeley!

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

    Straight to the point, considers edge cases. Thanks for the helpful video!

  • @mohammadghazanfar7228
    @mohammadghazanfar7228 2 ปีที่แล้ว +7

    What about the NULL part? What if the Nth place has no salary, NULL - that wasn't taken in cosnideration?

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

    Love the way you explain. I have been making the same mistakes. Instead of going to the answer directly, youve mentioned your mistakes and guided to answer :)

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

      sometimes you just gotta be honest and not let pride get in the way :)

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

    MS SQLServer :
    select nullif(salary,null) from
    (select DISTINCT Salary
    from (select Salary, DENSE_RANK() OVER(ORDER BY Salary DESC) as d_rank from Employee) as sub where d_rank = @N
    )as salary

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

    Thank you sir!

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

    do we need to learn these function things too ?

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

    When I try to run the code with the solution you described in the video, It fails with "Runtime Error" message. Submitting the same code accepted though, did not find a clear explanation in leetcode discussion as well. Can you let me know why that might be happening?

  • @user-zf3it3vw2e
    @user-zf3it3vw2e 20 วันที่ผ่านมา

    very helpful

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

    Very nice bro. But is it mysql or postgre becz i never studied SET operation and Function in sql. Pl help here. Pl give explanation for this

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

    Question :
    List the riders_id who started their journey yesterday in Delhi and completed their last journey in Noida that too yesterday.
    Columns were:
    riders_id, drivers_id, city_from,city_to,pickup_time(timestamp),drop_off_time(timestamp)
    I was asked this question in one of my uber interviews. Could you please solve it for me?

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

      I think there are some words missing from the question.

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

    hello, i been watching your video, it is helpful and clear, but when submit to leetcode, the codes wont work,

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

      maybe you need to change the SQL dialect. I’m using MySQL.

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

    can anyone tell me that my query in a right way or not
    select salary
    FROM
    (
    select id,salary,rank() over(order by salary desc) as rnk
    from person
    ) A
    where A.rnk = 2;

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

      or
      select salary
      FROM
      (
      select id,salary,dense_rank() over(order by salary desc) as rnk
      from person
      ) A
      where A.rnk = 2;

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

    SQL SERVER
    CREATE FUNCTION getNthHighestSalary(@N INT) RETURNS INT AS
    BEGIN
    RETURN (
    Select distinct salary from (Select salary ,Dense_Rank() over(order by salary desc) DENSERANK from Employee) as q where DENSERANK=@N

    );
    END

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

    This is good but won't work in SQL Server, we don't have anything like offset there.
    this is how I wrote it for SQL Server:
    "select distinct e1.salary from employee e1 where (@N-1) = (select count(distinct salary) from employee e2 where e2.salary > e1.salary)"

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

      Thank you for contributing your solution!

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

      Could you please explain your solution as well?

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

    I was crazy, why this was not working ... I forgot the ; :| 😆