Find nth highest salary in sql server | sql query to find 2nd, 3rd highest salary

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ก.ย. 2024

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

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

    20+ SQL Server Interview Questions : th-cam.com/video/SEdAF8mSKS4/w-d-xo.html
    Software Architecture Interview Questions : th-cam.com/video/AtTgcbLOqMM/w-d-xo.html
    SQL Step by Step - th-cam.com/video/uGlfP9o7kmY/w-d-xo.html
    SQL Server Joins : th-cam.com/video/KTvYHEntvn8/w-d-xo.html
    20 MSBI Interview Questions : th-cam.com/video/Nw_sHEKnOUE/w-d-xo.html
    10+ Power BI Interview Questions : th-cam.com/video/Cozc9WNBRt4/w-d-xo.html
    Angular Step by Step Tutorial for Beginners : th-cam.com/video/-9VcW7MBDs8/w-d-xo.html
    25 Angular Interview Questions : th-cam.com/video/-jeoyDJDsSM/w-d-xo.html
    35 Important JavaScript Interview Questions : th-cam.com/video/Zb4dPi7CANU/w-d-xo.html
    30 Important C# Interview Questions : th-cam.com/video/BKynEBPqiIM/w-d-xo.html
    25+ OOPS Interview Questions : th-cam.com/video/u99wAoBjDvQ/w-d-xo.html
    25 Important ASP.NET Interview Questions : th-cam.com/video/pXmMdmJUC0g/w-d-xo.html
    20 MySQL Interview Questions : th-cam.com/video/9hfjC-BpY20/w-d-xo.html
    20 PHP Interview Questions : th-cam.com/video/1bpNSynUrl8/w-d-xo.html
    5 MSBI Interview Questions : th-cam.com/video/5E815aXAwYQ/w-d-xo.html

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

    Select * from employee E1 where n-1 = (select count(*) from employee E2 where E1.Salary < E2.Salary)
    n = nth highest salary.

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

      Thank you very much this was the easiest solution i could find on internet i'm glad i came across your comment

  • @wishujadhav
    @wishujadhav 2 ปีที่แล้ว +5

    One of cross question asked by interviewer is, if more than one records having same 2nd higest salary, query in video gives only one record

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

      Write a CTE (Common Table Expression) using DENSE_RANK() function.

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

    Never got such a simple explanation, Thank you very much sir

  • @SowndaryaS-ue7su
    @SowndaryaS-ue7su 23 วันที่ผ่านมา

    Really well explained topics.. I got new interest in learning .. Thank you..

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

    I was asked this question yesterday in the interview.

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

    Declare @N Int =2
    Select EmployeeSalary
    From (
    Select Dense_Rank() Over (Order By EmployeeSalary) SNo, EmployeeSalary From TblEmployee) temp
    Where SNo =@N

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

    Instead of row_number we can use dense_rank , bcoz if the table has duplicate 2nd highest salary and we nned to show both then it is not possible by row_number()

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

    Your training is excellent

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

    So Nicely Explained sir..

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

    Fetch 1 offset n order by employeesalary should be the simplest way i think

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

      Definitely a simple solution, but offset and limit is not ANSI SQL.From TSQL point of view its the simplest solution.

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

    Very helpful. Thank you very much sir

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

    Very nice explanation as asual

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

    Bunch of thanks sir.🙏😊

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

    very helpful, thank you.

  • @saravanan.b
    @saravanan.b 2 ปีที่แล้ว

    In Oracle how can I find the same result. Top command will not work right?

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

    Great content as always.

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

    Thank you so much Sir

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

    use rank and select where rank = 1

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

      Rank is simple , LIMIT will more simple ,but these are TSQL. Will not work for other DB. In todays date with EF and Code first ANSI SQL has become more important. Thanks.

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

      But if it is for 2nd highest salary and the table has duplicate top highest salary , Rank() will not give you exact results

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

    The video demonstrates well the idea but probably is not going to work if there are duplicated salaries in the table.
    To solve this group by the salary in the sub query.
    However using rank will be a better solution selecting the nth rank as the nth top salary.
    In places where top or rank is not available an insert into temp table with autooncrement first column and sequential select the nth row from it will do too.

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

      It surely works for duplicate , top secures it. Temp table would be a bad solution.

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

    In PostgreSQL how to achieve?

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

    I have paid to join the channel but still I am unable to get access to videos.

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

      Just make sure you are accessing membership videos from same google account from where you have made the purchase or you can contact youtube help center if you do not get the access.

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

      Did u get the access?
      Do u recommend it?

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

    the problem for me is that I don't want to get a job, because then I have to go to work... So I prefer to go unprepared to the interview, and then blame the company for xenofobia.
    High 5 !

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

    CREATE FUNCTION getNthHighestSalary(@N INT) RETURNS INT AS
    BEGIN

    RETURN (

    select distinct salary from Employee order by salary desc OFFSET @N-1 ROWS FETCH NEXT 1 ROWS ONLY
    );
    END