Query To Find Nth Highest Salary In SQL | SQL Interview Question

Поделиться
HTML-код
  • Опубликовано: 3 фев 2025

Комментарии • 203

  • @Indian_mutualfund_expert
    @Indian_mutualfund_expert 4 года назад +46

    Hi Mam I regularly follow your videos... Mam in subquery at end of the brackets you need to mention any alphabet then only the query will get executed other wise it given error...
    Select top 1 sal from (select top 3 sal from emo order by sal desc) s order by sal asc...

    • @crackconcepts
      @crackconcepts  4 года назад +8

      Yes. You’re right

    • @danishdude6750
      @danishdude6750 3 года назад +18

      @@crackconcepts And you should do a select distinct top N salary in your inner select so you don't get duplicates if more people have the same salary.

    • @AsifIquebalSarkar
      @AsifIquebalSarkar 3 года назад +1

      rows can have same salary and we may need to fetch all of them, so WITH TIES would be a better option

    • @ashutoshanand4040
      @ashutoshanand4040 3 года назад +1

      YES I NOTICED THAT AS WELL BUT WHAT IS THE REASON?

    • @Somnath-je9nd
      @Somnath-je9nd 3 года назад +1

      why s outside that bracket ? can you explain

  • @crackconcepts
    @crackconcepts  4 года назад +13

    Hi, do check out the whole SQL Playlist for more such videos!

    • @anerishah679
      @anerishah679 4 года назад

      Really wonderful explanation!

    • @manimuttu1
      @manimuttu1 4 года назад

      Nice explanation!! and please add the playlist url as well :)

  • @akashlondhe1110
    @akashlondhe1110 3 года назад +78

    Instead of
    select top 1 from (subquery) order by sal desc ..
    you can write
    select min(sal) from (subquery)

  • @bharatiyadav8333
    @bharatiyadav8333 3 года назад +33

    Other clean way is to use limit and offset :
    "Select sal from Emp Order By sal Desc Limit 1 Offset 2 ;"
    Limit X Offset Y : select X rows from (Y+1)th row

    • @user-pr9pq8en2d
      @user-pr9pq8en2d 3 года назад

      I had this in my mind..

    • @preexa5797
      @preexa5797 3 года назад

      Hn re limit use krne ka simple and easy

    • @sarikadatta3706
      @sarikadatta3706 2 года назад

      Thank you Bharati Yadav
      You made it so much easier. I tried your method and it worked. What she showed in video is logical but for some reason it gives me an error in mysql
      Thanks a ton for helping me out

    • @thehavoc6331
      @thehavoc6331 2 года назад +1

      Damn, this is so easy and in one line, but I don't understand how offset works😅
      I understood until LIMIT 1 but how does "offset 2" give you the third highest salary
      Can anyone explain this to me?

  • @yogeshwarjoshi
    @yogeshwarjoshi 3 года назад +29

    I think you need a distinct for salary otherwise it will fail if few people have same salaries.

  • @vivekkrishnan514
    @vivekkrishnan514 4 года назад +79

    Thanks a lot, you just changed my life.
    I had watched almost all the videos and attended the interview. I passed 1 written exam and 3 technical rounds and I got selected for the SQL Developer Job only because of you.
    Thanks a lot for the effort you made. Keep teaching us, videos are really informative and easily understandable. 😇🙏🏻😇

    • @crackconcepts
      @crackconcepts  4 года назад +8

      I’m so glad my videos helped you. Congratulations on the new job!

    • @danushhu1317
      @danushhu1317 3 года назад +1

      Shata 😂
      dengda sariyagi

    • @ranaprathap8292
      @ranaprathap8292 3 года назад

      Bro any requirements for SQL pls reply me

  • @kushalgupta8353
    @kushalgupta8353 4 года назад +15

    Other Solution: select salary from (select salary from employee order by desc limit 3) order by salary asc limit 1;

    • @Jagadish12345
      @Jagadish12345 3 года назад +3

      it’s not a different solution, it’s just different database’s query
      like top works for mssql where as limit works for mysql and postgresql

    • @av98
      @av98 3 года назад +1

      @@Jagadish12345 you are right
      For Oracle it's FETCH

    • @thinkingmad1685
      @thinkingmad1685 3 года назад

      @@Jagadish12345 similar way can you tell for sql plus

  • @aashishprashant2402
    @aashishprashant2402 4 года назад +56

    Select T.column_name, Dense_Rank() over (order by salary desc) as Rank from T_Tablename where Rank = N;

    • @universal4334
      @universal4334 3 года назад +6

      Geeks for geeks

    • @jv___6729
      @jv___6729 3 года назад +2

      ;with rno as
      (
      Select Id, salary , dense_rank() over (order by salary) as salarycte
      From emp
      )
      Select Id, salary from rno where salarycte = 3

    • @Jyotisingh-qm4fm
      @Jyotisingh-qm4fm 3 года назад

      Hello aashis

  • @roshnimohare1798
    @roshnimohare1798 9 дней назад

    Easiest explanation ever... Thank you!

  • @paramvirsohi5922
    @paramvirsohi5922 3 года назад

    The bestest and the easieST place ON ENTIRE INTERNET TO FIND ANSWER. TYSM. HELPED ME IN INTERVIEW

  • @pratikkulkarni4929
    @pratikkulkarni4929 3 года назад +1

    Absolutely super today this is asked in interview for me and today i got this answer

  • @dhivyakamalakannan
    @dhivyakamalakannan 4 года назад +2

    Love this !! And I wish there was a love button in RUclips for subscribers

  • @vireshmohan7554
    @vireshmohan7554 3 года назад +1

    Hey, just wanted to let u knw that u r amazing.....The explanation Is just awesome...Thnx for such vids...Much Appreciated!!

  • @zubairahmed-om4vd
    @zubairahmed-om4vd 4 года назад +2

    Very informative may Allah bless u

  • @anissiddiqui2649
    @anissiddiqui2649 4 года назад +1

    Congratulations for 9 thousand subscribers.

  • @RupinderKaur-oq4kl
    @RupinderKaur-oq4kl 2 года назад

    Thanks alott 💖very easy to understand ☺️

  • @raveendrakumar4499
    @raveendrakumar4499 3 года назад

    Excellent explanation, This is really helpful to understand the concept of this. Thank you so much for the video and keep posting interview based videos like this

  • @rajeshg3570
    @rajeshg3570 3 года назад

    This is very good explanation.. really like it.

  • @saiteja5246
    @saiteja5246 3 года назад +16

    We can use select * from(select *,rank() over(order by sal desc) rno from emp)src where rno = Nth value

    • @GANESH-zi2xb
      @GANESH-zi2xb 3 года назад +1

      This query is wrong

    • @himanshugupta4807
      @himanshugupta4807 3 года назад +1

      In case of duplicate your query will not give expected result. We can use dense function instead rank function because rank give pit holes while generating rank on duplicates.

    • @akulakalyan1242
      @akulakalyan1242 3 года назад

      Select * from (select *, denserank() over (partition by empid order by sal desc)a,sal) src where src.a= Nth value;

    • @himanshugupta4807
      @himanshugupta4807 3 года назад +1

      @@akulakalyan1242 Hey Akula, query seems to be correct just one correction if you do partition over emp id then each salary will become unique and all records will have dense rank =1 so no need to use partition by clause.

    • @akulakalyan1242
      @akulakalyan1242 3 года назад

      @@himanshugupta4807 Then we can use dept id partition right

  • @parthchoudhary4165
    @parthchoudhary4165 3 года назад

    Its really helpful .I understand in once ..tyku so much

  • @lavanyasakhamuri9693
    @lavanyasakhamuri9693 3 года назад

    Very Nice explanation Mam.

  • @equiwave80
    @equiwave80 4 года назад

    I have just just started to learn SQL. These videos will be very useful for for the me!!! BTW I have subscribed to your channel!!!

  • @veenijeyasri2516
    @veenijeyasri2516 4 года назад

    Excellent mam, keep it up, expect more video from your page like this

  • @lucky4y4
    @lucky4y4 4 года назад +5

    Hi, I had watched your videos on SQL and it worked like a miracle for me. My logics gt better and better and finally I got the job offer that too on 31st of December 2020. I could never thank you. Thank you for your help. Keep posting keep helping keep growing. Thank you.

  • @priyaingle832
    @priyaingle832 3 года назад

    Wow so easy explaination,

  • @akshitdhadwal
    @akshitdhadwal 3 года назад

    You explain things really well

  • @mcaddit6802
    @mcaddit6802 3 года назад +2

    Why top 1 keyword doesn't support in most of the SQL compiler?

  • @mallikarjunhagargi7830
    @mallikarjunhagargi7830 4 года назад

    You earned one subscriber dear thank you for the good content,,keep doing we appreciate

  • @vishakhaborse7242
    @vishakhaborse7242 3 года назад

    Your teaching is really amazing...thank you

  • @anissiddiqui2649
    @anissiddiqui2649 4 года назад

    Congratulations to 10 thousands subscribers

  • @suhas3785
    @suhas3785 3 года назад

    Thx for create video like this.....

  • @arunachaleshwartiruvannama3188
    @arunachaleshwartiruvannama3188 3 года назад

    Useful one Thanks for this

  • @ramanjaneyulum5739
    @ramanjaneyulum5739 3 года назад +1

    Select max(sal) from emp where salary not in(select distinct top 2 salary from emp order by salary describe)

  • @its_me7363
    @its_me7363 4 года назад +1

    very informative video...can you please make videos on more advanced SQL queries like partition, window function and pivoting table etc. ?

  • @rushikeshkhandagale6472
    @rushikeshkhandagale6472 3 года назад

    hi thanks for your SQL query lectures they are very helpful.

    • @ranaprathap8292
      @ranaprathap8292 3 года назад

      Bro any requirements for SQL server pls reply me

  • @devanaidu9406
    @devanaidu9406 4 года назад

    Nice answer.. thank you for sharing your knowledge.❤️

  • @shubhammittal4389
    @shubhammittal4389 3 года назад

    very insightful

  • @guruquebec
    @guruquebec 3 года назад

    Nice and thanks. You should try to teach to pass exams like 70-761 topic wise. since you have good explaining skills.

  • @mercifulmedia9393
    @mercifulmedia9393 4 года назад

    Tittle mistake: "Nth" Don't mind.

  • @vedantpatil3752
    @vedantpatil3752 2 года назад

    Thank you soo much god bless you

  • @mashapoguajay3322
    @mashapoguajay3322 3 года назад

    Nyc explaination

  • @talhamuneer2599
    @talhamuneer2599 4 года назад +1

    Please Make Video Every Week!

  • @ramanmanocha4800
    @ramanmanocha4800 4 года назад

    Amazing content, eagerly waiting for more videos!!

  • @ashishsahu1310
    @ashishsahu1310 3 года назад

    nice explanation.

  • @anissiddiqui2649
    @anissiddiqui2649 4 года назад

    Congratulations for 11 thousands subscribers

  • @daminipatil8183
    @daminipatil8183 4 года назад

    Thank u so much
    Nice explanation

  • @geniuschess7916
    @geniuschess7916 3 года назад

    perfect explanation ! very well understood :)

  • @daminipatil8183
    @daminipatil8183 4 года назад

    Best teaching.. Thank u

  • @parshu3238
    @parshu3238 3 года назад

    Very well explain thank you very very much

  • @AbhishekSharma-hy4nl
    @AbhishekSharma-hy4nl 4 года назад

    Medium level leetcode question ... I just tried to solve it but couldn't. Thnx alot...

  • @kumarraju9784
    @kumarraju9784 2 года назад

    Select from emp1 rownum

  • @vishalsaxena5081
    @vishalsaxena5081 3 года назад

    i have a query pls hlp me to solve this
    column_1
    A
    A1,B2,C5
    how can we insert in column like this and after that i want change where A1 is given i need only A
    pls help

  • @mazingTechnical
    @mazingTechnical Год назад

    select * from emp order by salary desc limit 2,1;

  • @NextGenCoding545
    @NextGenCoding545 3 года назад

    thanks a lot didi...

  • @shushmamalhotra6538
    @shushmamalhotra6538 2 года назад

    Mam plz provide a plat form where we can share some question in which we have problem related to SQL query

  • @cloudqaTutorials
    @cloudqaTutorials 2 года назад

    Excellent

  • @gbharathi5556
    @gbharathi5556 3 года назад

    Can u say what is the best approach to find n th highest salary....and explain it...

  • @jayantmishra4270
    @jayantmishra4270 2 года назад

    3rd height salary:-
    Can i write like this?? :-
    Select salary from employees order by salary desc limit 2,1

  • @jagadishteluguvlogs
    @jagadishteluguvlogs 2 года назад

    Hlo mam I have one dought ... Given requirement is fetch the highest salary in a table so your are said top 1 salary in a table but but inner query results give 6000,5000,3000 like that but in this salary top 1 salary is 6000 but your said 3000 why mam..plz clearly say that mam IAM full confused..

  • @sivaakumalla8687
    @sivaakumalla8687 4 года назад

    Nice explanation.👍 if it possible explain 'With '

  • @balaguravareddy4001
    @balaguravareddy4001 3 года назад

    Thank you so much

  • @ritika29
    @ritika29 4 года назад +14

    Instead of TOP 1 in the outer query can we use min() function?

  • @Naresh_9182
    @Naresh_9182 9 месяцев назад

    I think this query is not supported by Oracle SQL developer 19thC version

  • @rajeevmayekar1775
    @rajeevmayekar1775 3 года назад

    Nice work 🙌

  • @kunaldarwesh2481
    @kunaldarwesh2481 4 года назад

    I like your explanation keep it up.

  • @v_naykp4168
    @v_naykp4168 3 года назад

    Select * from employees
    Order by Salary Desc
    Limit 3 ;

  • @amulbobbili3813
    @amulbobbili3813 Год назад

    Tqsm

  • @sohrabkhan9823
    @sohrabkhan9823 4 года назад

    AOA, Urdu may banao na videos mam, or computr pr practical bhi sikhaiye plz

  • @surendraavvaru8536
    @surendraavvaru8536 3 года назад

    Select * from (select ename,esal,dense_rank()over(order by sal desc)rank from Emp) where rank=#

  • @chinmayk2657
    @chinmayk2657 2 года назад

    Maam - need help - How to find people in a database who live in the same cities?

  • @unknowninformation3529
    @unknowninformation3529 Год назад

    What about the nth minimum topic? If there is max it should have minimum also right

  • @ganeshkumaars859
    @ganeshkumaars859 3 года назад

    A DIRECT TOP 1 SELECTION MUST DO HOPE SO, AS WE ARE USING DESC AND AGIN MAKING IT ASC ,(the inner query must give result in asc by default)

  • @RobbyTicknor
    @RobbyTicknor 3 года назад +2

    You could use a window function kind of like this
    select *
    from (
    select *,ROW_NUMBER() OVER(ORDER BY sal ASC) AS Row#
    from emp
    ) SubQueryWithRowNum
    where Row# = 3

  • @gangamshravan2394
    @gangamshravan2394 3 года назад

    thank a lot

  • @jatinpant6992
    @jatinpant6992 3 года назад

    Mam, I am using MySQL and i am not able to use TOP Query, Please tell me what to do?

  • @apamwamba
    @apamwamba 7 месяцев назад

    Given say:
    Person Salary
    tom 10
    mary 20
    hillary 30
    joe 10
    apam 200
    cecilia 30
    zuk 30
    hope 80
    euphrasia 100
    Maureen 150
    SELECT [Person]
    ,[Salary]
    FROM
    (
    SELECT [Person]
    ,[Salary]
    ,DENSE_RANK() OVER (ORDER BY Salary) as ranking
    FROM [dbo].[sal_01]

    ) AS x
    WHERE x.ranking=3
    RESULT:
    Person Salary
    hillary 30
    cecilia 30
    zuk 30

  • @Vivek-gx6zd
    @Vivek-gx6zd 2 года назад

    select salary(select salary from employees order by salary dec)where rownum

  • @vasanthavasu8696
    @vasanthavasu8696 3 года назад +1

    We can use min(sal) in outer query instead of top 1. Am I wright?

  • @siddharthkakade7779
    @siddharthkakade7779 3 года назад

    Select n salary and why do you select top 3 changes the question directly

  • @beingrishi6450
    @beingrishi6450 11 месяцев назад

    Can it be resolved by using row_number OVER clause ORDER BY SALARY DESC ???

  • @parttimedude5440
    @parttimedude5440 4 года назад

    Thank You Ma'am ...

  • @saurabhwaghmare393
    @saurabhwaghmare393 3 года назад

    Excellent...

  • @xyz-vv5tg
    @xyz-vv5tg 3 года назад

    SELECT DISTINCT Sal FROM emps ORDER BY Sal LIMIT n;
    Here n =3
    This might work as well.

  • @souravnandy8385
    @souravnandy8385 Год назад

    Is there any other way to write this query?

  • @Faisal1504
    @Faisal1504 3 года назад +1

    Hi After the subquery I had to write as Temp - like give the subquery an alias then it worked. Is that correct way to do this qry? otherwise it says incorrect syntax near keyword order

  • @vinodkumarvicky1149
    @vinodkumarvicky1149 4 года назад

    We'll explained keep it madam

  • @anirudhsodhani9397
    @anirudhsodhani9397 3 года назад

    what if we have a table that has many duplicate salary values? I think the better way to write the query is to fetch distinct N salary from the tables first ordered in descending order, and then finding out the min out of the N distinct salary/.

  • @nikhilkadadevar5289
    @nikhilkadadevar5289 4 года назад

    Take 3 tables and solve query including all 3 tables mam

  • @sanjayisure4580
    @sanjayisure4580 3 года назад

    I am day by day going depressed bcz of not able to crack interview , they given scinario which is not solve by me , so please help me

  • @jaisingh1641
    @jaisingh1641 3 года назад

    Why internal query order is descending , why can we keep it ASC , then then later we would not need to order again...

  • @aravindg4801
    @aravindg4801 4 года назад +1

    Hi,
    From which version onwards TOP clause will work?? In oracle

  • @shubhamingale4625
    @shubhamingale4625 3 года назад

    Is there any other trick in which we don't need use inner outer query

  • @vyshnavib8023
    @vyshnavib8023 3 года назад

    writing inner query only sufficient no mam..why can we write outerquery also..explan mam

  • @medhakhairnar6142
    @medhakhairnar6142 4 года назад

    Thanks ditto same question came in the interview

  • @mrameer15
    @mrameer15 3 года назад

    great

  • @sngparminder
    @sngparminder 3 года назад

    I tried but the top query but it is not working, I use toad & my sql

  • @MaheshKumar-dj2nj
    @MaheshKumar-dj2nj 3 года назад

    i use that query subquery worked but above condition not working alert was invalid column name

  • @rayavenugopal8847
    @rayavenugopal8847 3 года назад

    Mam i need full course training on plsql. Can u?

  • @snehalgholap5261
    @snehalgholap5261 3 года назад

    Your voice 💕

  • @umeshgolla2730
    @umeshgolla2730 3 года назад

    Can you start course on AWS kubernetes