What are Subquery and Co-related Queries in SQL Server ? | SQL Server Interview Questions & Answers

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

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

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

    20+ SQL Server Interview Questions : th-cam.com/video/SEdAF8mSKS4/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
    SQL Server Joins : th-cam.com/video/KTvYHEntvn8/w-d-xo.html
    SQL Step by Step - th-cam.com/video/uGlfP9o7kmY/w-d-xo.html
    Software Architecture Interview Questions : th-cam.com/video/AtTgcbLOqMM/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 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
    20 MySQL Interview Questions : th-cam.com/video/9hfjC-BpY20/w-d-xo.html

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

    Searched from different channels, but at last , you clarified what subquery actually is. Thank You Sir who ever you are❤️

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

    Your explanations are very clear sir,
    keep up your good work,
    may god bless you!!

  • @dipranjan1987
    @dipranjan1987 9 ปีที่แล้ว +4

    This is the best explanation I have ever seen.Thank you so much for such elaborated explanation

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

    good explanation on sub query and co-related queries, but i think the example given for co-related queries is a terrible solution for finding the second highest salary, this can be much better achieved using a sub query like -> select top 1 .... from (select top 2 ... from ..... order by salary desc) as top2salaries order by salary asc

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

    Thanks...you cleared my this concept 👍👍👍

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

    Great and simple explanation with examples. came here after watching a couple of videos that failed to explain this concept so neatly.

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

    Your all videos of both channel are just excellent..I wish if all may be free to access

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

    👊👊👊👊👊👊Smash Subscribe button if you like it.👊👊👊👊👊👊

    • @kirankumar-iz3tw
      @kirankumar-iz3tw 6 ปีที่แล้ว +2

      i already smashed.....i cant do one more smash....it will get un subscribe

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

      @@kirankumar-iz3tw LOL!

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

    Clear, precise and concise, thank you for your time.

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

    Excellent explanation sir.....it's really helped me

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

    Thank you so much bro, helped me a lot than my professor or books! Keep it up!

  • @cassondrad2280
    @cassondrad2280 8 ปีที่แล้ว

    OUTSTANDING. You're explanation/demonstration. Well done. I get it. What a big help, thank you, thank you. I finally understand.

  • @abdulsacur2615
    @abdulsacur2615 9 ปีที่แล้ว +5

    best explanation about Correlated Subqueries

    • @itwithfun3843
      @itwithfun3843 8 ปีที่แล้ว

      have u purchased subcription ??

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

    Like a loop withn a loop , Each row of e1 is passed and checked with the condition e2.salary greater than e1.salary and if it satsfies count e2.salary, then outer query conditions that is if is two then execute the outer query and print empname

  • @RameshK-rn3hf
    @RameshK-rn3hf 8 ปีที่แล้ว

    Decent explanation without confusing.. Excellent

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

    Nicely explained! Thank you for sharing!

  • @maheshwara.r.7600
    @maheshwara.r.7600 4 ปีที่แล้ว

    THANKs for clear explanation of Co-related Sub-query.
    But the query shown for Co-related Sub-query does NOT fetch ANY records when multiple employees have SAME salaries and also gives Incorrect records of Nth Highest salary if multiple employees have SAME salaries in a Table. We can correct the query by adding "DISTINCT" in INNER query.
    select *
    from emp E1
    where 2 =(select Count(DISTINCT E2.salary)
    from emp E2
    where E2.salary >= E1.salary)

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

    Thanks got clear cut idea about corelated query.

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

    Yr explaintion is awesome, pls make more

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

    The difference between both of them is of references ...as in subquery we have no reference or relation between both of inner and outer query but in the latter one we referenced the outer query inside the inner query.

  • @shellprince790
    @shellprince790 11 ปีที่แล้ว

    Thank you so mch... Finally i understood subqueries and co-related queries...

  • @yennamnava7580
    @yennamnava7580 11 ปีที่แล้ว

    Your Videos are seriously worthful...... thanku...

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

    Very good explanation..really helpful

  • @joshc6763
    @joshc6763 10 ปีที่แล้ว +5

    Good accent. Fast and furious nice and clean

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

      have u purchased subcription ??

  • @RamcharitmanasHindi
    @RamcharitmanasHindi 5 ปีที่แล้ว

    VERY NICE LECTURE .. SUPER EXPLATION .. HELPED ME A LOT

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

    Great Explaination

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

    Excellent 👌

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

    Your X Highest salary will only worked when there is only one employee having x Highest salary. If there are more then one employee with X highest salary then your query will return no output

  • @AbhishekKumarMishra_abhi101009
    @AbhishekKumarMishra_abhi101009 6 ปีที่แล้ว

    well explain the concept but this will work only if we do not have duplicate salaries, I think you need to use count(distinct(salary))

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

    Hi sir
    When I am converting Date to integer or decimal using cast and convert, it's reflecting the date value less than its actual value.
    Please suggest.

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

    Sir,, a very nice explanation from your side.., sir plz tell me which software you are using for SQL queries,,, and if it is available for free,, send me the link.

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

    This query is not produced the result if salary will be same for another employee means two different employees salary will be same as 200

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

      select * from empsal where salary=(
      select min(salary) from empsal where salary in(
      SELECT top(3) salary from EmpSal order by salary desc ))

  • @279Palu
    @279Palu 10 ปีที่แล้ว

    very clear explanation.. thanks

  • @jhjhj2172
    @jhjhj2172 5 ปีที่แล้ว

    very well explained .. thank you so much

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

    Superrr explanation thank u

  • @haticetercuman4546
    @haticetercuman4546 6 ปีที่แล้ว

    Excellent explanation

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

    have been reading ur other book on software testing ,awesome book!!

  • @mihaelaneagu7844
    @mihaelaneagu7844 10 ปีที่แล้ว +4

    It,s so clear

  • @kishorpatel3369
    @kishorpatel3369 9 ปีที่แล้ว

    Thanxs for good explainatory video....
    Keep it up...

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

    Thanks for your video. It was easy to follow.
    I had a question in related to co-related query.
    All the record in table has unique salary. What happens if there are two people who had same salary and we wanted the second highest salary? Is that number going to be 2 still??

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

      I had that doubt too. Did you get the query to get the 2nd highest if the salary are duplicates?

    • @Dark-wc4pu
      @Dark-wc4pu 4 ปีที่แล้ว +1

      Then the result will show two employees name instead of one because both the employees have second highest salary among all the salaries.

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

    thanks for this buddy!

  • @SatishKumar-ji4us
    @SatishKumar-ji4us 11 ปีที่แล้ว

    Nice and clear explanation

  • @johnedwards6253
    @johnedwards6253 7 ปีที่แล้ว

    Thank you! I finally got it!

  • @umeshkanaujia6700
    @umeshkanaujia6700 7 ปีที่แล้ว

    Thanks. It was nice and clear.

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

    Great explanation, but I have a question. Why was the count function used in the subquery ?

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

      Actually through innere query he is counting number of employees having greater salary than outer query employees id
      In fist case count is 4
      In second case count is 4
      Like dt when ever count is 2 dt employee ID will be selected as final answer

  • @shaikhshahfaisalshaikh6517
    @shaikhshahfaisalshaikh6517 8 ปีที่แล้ว

    thnx sir... this is very good explanation

  • @705chacko
    @705chacko 11 ปีที่แล้ว

    thanks,nice and simple explanation.

  • @knowledgeworld3666
    @knowledgeworld3666 6 ปีที่แล้ว

    select * from empsal where salary=(
    select min(salary) from empsal where salary in(
    SELECT top(3) salary from EmpSal order by salary desc ))

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

    sir , not getting result when the same salary of 2 employees.

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

    Will output of subquery always distinct

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

    Fine but it will be helpful to us if you explain hiw to frame a co related query in detail instead of pasting code here

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

    Great sir

  • @madanrajpoot
    @madanrajpoot 10 ปีที่แล้ว

    Awesome Video..

  • @indrasenareddy2193
    @indrasenareddy2193 10 ปีที่แล้ว

    very well explained..

  • @VinodKumar-nn7go
    @VinodKumar-nn7go 3 ปีที่แล้ว

    great videos, by the way i did not know SQL server is a 'he' ... lol

  • @nasimbeksaidmurodov3419
    @nasimbeksaidmurodov3419 6 ปีที่แล้ว

    Hi guys,
    I have a question.
    I want to figure out e2 from the query. How can I find that? I know if we want to change Table's name with 'as' after that it shows in new table where EmpSal changes to e2. But could we put it before 'from'? How COUNT(e2.Salary) work their?

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

      It works like this row1 of outer query is compared with all the rows of inner query if condition is matched it takes the count of inner query.

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

    Grt learning

  • @sss2005sunil1
    @sss2005sunil1 11 ปีที่แล้ว

    good video, great examples

  • @MrMMohsen
    @MrMMohsen 7 ปีที่แล้ว

    Can correlated queries be always rewritten as a JOIN query and return the same results ?

  • @noufal_mundodan
    @noufal_mundodan 11 ปีที่แล้ว

    thank U.... thank U........... finally i got it.

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

    Thank you for the explanation sir, just a doubt I had and that being can't we right a simple code in order to get the second highest salary? Like:
    SELECT EmpName from EmpSal
    ORDER BY Salary DESC
    LIMIT 1 OFFSET 1;
    Pls. do correct me if I did something wrong :)

    • @d.ndriotis9209
      @d.ndriotis9209 2 ปีที่แล้ว

      I do not know the answer to your question. However subqueries are used in order to make filtering a lot easier and often they are used in very complex situations. In this video we only have a simple example so it is expected that we can use many different methods to accomplish the same thing.

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

    Nice Video

  • @amsavallit
    @amsavallit 12 ปีที่แล้ว

    good explanation

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

    love from nepal.

  • @TheGiselamother
    @TheGiselamother 12 ปีที่แล้ว

    good job! thank you for share........

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

    thank you

  • @rahulhameed2215
    @rahulhameed2215 8 ปีที่แล้ว

    audio is very loud and good, how did u record it, which software u used here ?

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

      While recording he just shouted like a girl watching horror movies ,just like that u can try

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

    I guess we need to add count ( distinct e2.salary ) incase of duplicate salaries !

  • @bhuwanpandey4342
    @bhuwanpandey4342 6 ปีที่แล้ว

    Remove = from inner query than only you will get the correct 2nd highest salary or nth highest salary

  • @Ganeshw0
    @Ganeshw0 7 ปีที่แล้ว

    this correlated query is not working for for second highest salary if there is duplicate records

    • @SamVsCode
      @SamVsCode 7 ปีที่แล้ว

      yes, if the highest salary is repeated twice, the co-related query solution will fail.

    • @knowledgeworld3666
      @knowledgeworld3666 6 ปีที่แล้ว

      @@SamVsCode yes right. See this query which will find any higest number of query like 2nd, 3rd...10th highest
      select * from empsal where salary=(
      select min(salary) from empsal where salary in(
      SELECT top(3) salary from EmpSal order by salary desc ))

  • @zeelanmca2011
    @zeelanmca2011 10 ปีที่แล้ว

    nice video..:)

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

    How many nested query we can use within a Sub - query ??

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

      There is a limit do not know upfront. We have limit of 16 in oracle i think and in SQL Server also there should be a limit.

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

      Last I saw, Oracle allows up to 255 levels of subqueries in the WHERE clause

  • @kirankumar-iz3tw
    @kirankumar-iz3tw 6 ปีที่แล้ว

    thanks

  • @mohammadroufa8591
    @mohammadroufa8591 7 ปีที่แล้ว

    Shiv raj. (Empsal table)
    100. 150
    I want out put like this how to write a query to this problem

  • @kiransurvase96
    @kiransurvase96 10 ปีที่แล้ว

    in sub query can we use NOT IN Keyword ....
    please Explain???
    Thanks.
    It mean a lot.....

    • @saifahmedyassen3725
      @saifahmedyassen3725 10 ปีที่แล้ว

      yes you can , and when you use it all the salaries which are less than 150 will be the output.

    • @kiransurvase96
      @kiransurvase96 10 ปีที่แล้ว

      Thanks ....means Exactily the reverse Output...???

    • @itwithfun3843
      @itwithfun3843 8 ปีที่แล้ว

      have u purchased subcription ??

  • @pulkitindia
    @pulkitindia 7 ปีที่แล้ว

    What if there are more than 1 user on the same 2nd highest salary i.e. 200?

    • @manjit534
      @manjit534 7 ปีที่แล้ว

      Use ROW_NUMBER OR RANK window functions to achieve what you've asked.
      Assume the table has an entry as : pulkit 150 5 respectively. Then the below RANK function will display both pulkit and raju.
      select * from (select e. * , ROW_NUMBER() over (order by salary desc) row_num from empsal e) where row_num=3;
      OR
      select * from (select e. * , RANK() over (order by salary desc) rank from empsal e) where rank=3;

  • @sonu981
    @sonu981 11 ปีที่แล้ว

    nice stuff

  • @Debjit625
    @Debjit625 7 ปีที่แล้ว

    Great

  • @bilatenawtube2520
    @bilatenawtube2520 6 ปีที่แล้ว

    great

  • @smastan3975
    @smastan3975 6 ปีที่แล้ว

    Nice

  • @bmkkar8568
    @bmkkar8568 8 ปีที่แล้ว

    when we use EXIT and NOT EXIT in sub query.

  • @raghunadhav8287
    @raghunadhav8287 8 ปีที่แล้ว

    nice

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

    actually 200 is the third highest value in this table but you said 2nd highest.

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

      yes thats true and everyone here are appreciating..Explaination is Good But answer is Wrong..

  • @vasssrini
    @vasssrini 5 ปีที่แล้ว

    his name murali gud lecturer for learn sql

  • @mrneb8995
    @mrneb8995 7 ปีที่แล้ว

    wow

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

    In almost every sentence there is a 'you know'. Very irritating.

  • @HarpalSingh-wi6fe
    @HarpalSingh-wi6fe 4 ปีที่แล้ว

    . .

  • @madansanagapalli1638
    @madansanagapalli1638 8 ปีที่แล้ว

    excellent explaination .

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

    Very well explained