EXIST Function in SQL

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

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

  • @lowkeygo
    @lowkeygo 11 месяцев назад +14

    I struggled because I did not understand the logic of how outer query recognizes the conditions in the where clause of the subquery. So this has helped me understanding the concept:
    1. First look at the outer query and think it as "Each row is about to be returned!"
    2. But in order for a row to be returned, "where exists" clause must be true.
    3. Now we look at the where clause inside the subquery. Here, usually we make outerquery.id = subquery.id because "we are determining whether the current row should be returned or not". And this current row has its own id. Furthermore, usually, you put 1 more condition such as subquery.quantity > 2.
    4. Now, the subquery will look for the id that is about to be returned in the outer query inside the subquery, and see if there's any row that meets the second condition (quantity >2).
    5. If there's any row that matches the condition inside the subquery, outer query can return the current row. Otherwise, it skips and the same process repeats for the next row.

  • @brontesaurus1
    @brontesaurus1 5 лет назад +35

    Thank you! I have struggled with this for a while. No one (teacher nor book) ever emphasized that the outer query would or would not run based on the subquery.

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

      same with me. glad i found this video

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

      Same here. Others teach like it was obvious

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

      Difference of an effective teacher :)

  • @trollanddrump5326
    @trollanddrump5326 4 года назад +21

    How can you forget SEX? Unbelievable!

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

    What is the advantage of using EXIST func in your example over setting up the query like:
    SELECT * FROM professor
    WHERE eid IN (SELECT adviser FROM student WHERE sex = 'F');
    ??

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

      Is this an alternative?

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

      IN returns an array. EXIST returns a simple TRUE or FALSE. So EXIST is more efficient in this particular example. But you cannot EXIST where you need the entire array of employee ids as an output.

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

    Use of exists function is cleared. But we can write sub queries instead of this. That will be easy to write. Then what is the purpose of using exists. Can anybody clarify on this

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

    Are you writing with mouse...!!! Coz your onscreen writing is so clear and flawless

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

    You explained it so well, thank you so much!

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

    How is this different from using IN ? In the above example wouldn't IN be a better option and take care of the same logic?

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

    is it compulsory to select ROLLNO in subquey, can I select * or any random columns ? do it have to follow any patterns of select in subquery?

  • @lorraineliu8422
    @lorraineliu8422 6 лет назад +6

    Very clear. Thank your for the instruction!

  • @VanBhardwaj
    @VanBhardwaj 5 лет назад +1

    Are the results produced by replacing "exists" with "not exists" in a query always mutually exclusive?

  • @sujaysshenoy247
    @sujaysshenoy247 5 лет назад +3

    super clear explaination of the exists concept,
    i was struggling to understand this concept
    Thank you!!

  • @gaborszepes8107
    @gaborszepes8107 8 лет назад +16

    Dear Sir,
    I guess "EXIST" is actually "EXISTS"...
    I am a beginner in this field this is the reason why I am asking.
    Thank You for your answer in advance.
    Best regards,
    G.Sz.

    • @Edredo
      @Edredo  8 лет назад +3

      +Gábor Szepes
      You are absolutely correct. The video should be improved ASAP

  • @Avict-18
    @Avict-18 6 месяцев назад

    Thank you very much,
    I had been struggling with this for quite a while now
    I guess you are the one that has explained it the best>
    I was wondering how the outer query has recognized the condition of the inner query but now i get it

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

    will there be any value returned if only the inner query is executed? (ignore the exist, outer query)

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

    What if I want
    Professers with at least 2 female students? Do I do a
    groupby prof, count >=2
    in the internal query?

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

      he uses at least one for the EXISTS function however if such a scenario happens then we use groupby clause along with the Having Clause for conditions.

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

      atleast 1 means:- 1 or more than 1
      (i guess we need to use this in inner query using group by and having )
      The video explained answer is:- find out the name of professor who has female student !
      correct me if im wrong !

  • @tanishktripathi8773
    @tanishktripathi8773 4 года назад +7

    Doubts cleared at a time immense need ♥️

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

    Sir why arent we using join in the inner query as two tablesare being joined

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

    Thank you very much for your lesson..Looks easy but I do not know why I am finding it difficult

  • @SamChan-p4j
    @SamChan-p4j 11 дней назад

    what if the condition changes to "for at least 2 female students" ?

  • @jackdaniel1579
    @jackdaniel1579 7 лет назад

    Can this whole Exist can be used with IN?

  • @ŁukaszSmykał
    @ŁukaszSmykał Год назад

    obvious but super helpful
    thanks to You I bypass my problem in BigQuery :)
    thx:)

  • @caitancorreia8379
    @caitancorreia8379 8 лет назад +1

    A question sir,
    What would happen if instead of P.Eid =S. Advisor we just say Eid=Advisor without renaming as P and as S.
    What would happen then? Is that wrong?

    • @sukantasen1
      @sukantasen1 8 лет назад +3

      It will work s well as EID and Advisor are different column name but having same datatype and you will get the same result

    • @pallavi4501
      @pallavi4501 7 лет назад

      Sukanta Sen ya correct

    • @lordmefisto255
      @lordmefisto255 6 лет назад +1

      U should use Professor.Eid=Student.Advisor because ( P is for Professor table and S is for student)

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

    Thanks mate, that's actually a good vid

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

    Thank you so much for this video. You demystified this concept in-toto! I appreciate you.

  • @pradeepkumar.5172
    @pradeepkumar.5172 4 года назад

    thank u so much.........i understood this concept now.

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

    Amazing explanation! Thank you so much!

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

    All the best coding tutorials on RUclips are made with Indian accent, lol. Thank from Ukraine, btw

  • @praveenpalina6591
    @praveenpalina6591 7 лет назад

    Hi sir,
    Which one fast exists or in?why?

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

      "In" is faster because the outer query is run once while in EXISTS, the outer query is run for each inner query element.

  • @lokeshphoenix
    @lokeshphoenix 6 лет назад

    can we write select name , AVG(sal)
    from employee
    group by department
    having sal>AVG(sal) ??

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

    Thank You so much sir, i have been searching for a good video for the long time, and i finally discovered this. It is actually6 fruitful for me.

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

    Exists: IF Subquery is empty, then not execute outer-query

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

    I have a question. A colleague wrote averysimilarly structured query, except in the subquery instead of writing "Select RollNo From....", they wrote "Select 1 From".
    Could one speculate this was done for efficiency? Or Could you provide your thoughts why this might be? I commonly see "Select 1" and "EXISTS" together quite frequently and am curious why it is used.

    • @archidar1
      @archidar1 4 года назад +3

      You only need the table after the EXISTS clause to exist (i.e. contain any value).
      The value itself does not matter.
      Hence, you can use SELECT 1 ... to save time, or show that you do not care about the value.

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

    Really nice handwriting ;)

  • @p.m8042
    @p.m8042 6 лет назад

    Are you the guy who made coding school series?

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

    THANNNKS YOU ARE THE BEST!

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

    THANNKSS A LOOOOOOOOOOT!!! YOU SAVE MY LIFE DUDE!!

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

    Thanks!

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

    thank you

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

    thank u very helpful

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

    Thanks..

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

    It's EXIST or EXISTS? I'm confused rn

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

    I didnt understand

  • @maheshmenta6078
    @maheshmenta6078 8 месяцев назад

    thank u

  • @codestorywithMIK
    @codestorywithMIK 7 лет назад

    very well explained

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

    thank

  • @vohuuthang7217
    @vohuuthang7217 2 месяца назад

    great

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

    Thank you sir, that was helpful :)

  • @adaseth
    @adaseth 8 лет назад +6

    #ASMR ;)

  • @jordan5253
    @jordan5253 5 лет назад +1

    okay

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

    fking genius

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

    Atleast do your homework first...its existS not Exist...plural...

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

    Sexuality and Gender are not same

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

      Gender and sex are the same, however, what we call sexuality these days are nothing more than fetishes people identify with.

  • @ofek5365
    @ofek5365 5 лет назад

    Thank you
    And thank god for Google Auto Generated Subtitles

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

    Thank you

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

    okay