The SQL EXISTS clause

Поделиться
HTML-код
  • Опубликовано: 6 сен 2024
  • How to use the EXISTS clause in SQL. For beginners.

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

  • @vulcanstarlight
    @vulcanstarlight 2 года назад +2

    Doug, you have helped me tremendously! When the part about NOT EXISTS was explained, this in itself shed light onto the exact problem I needed to mitigate. Thank you 🙂

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

      same here! 👍 so useful

  • @babybob8823
    @babybob8823 6 лет назад +4

    Very clear and to the point. Thanks a lot.

  • @adreamer9999
    @adreamer9999 5 лет назад +6

    Thank you for the explanation. Very helpful.

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

    Finally, someone answered the nagging question in my head, "Why not just use a join?" Thanks Doug!

  • @voodoochili12
    @voodoochili12 7 лет назад +6

    Great explanation, thank you

  • @bagindamirza
    @bagindamirza Год назад +1

    much thanks for your video! great explanation on this topic!

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

    Thank you very much for such great content.

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

    Fantastic video and exactly what I needed!

  • @bruville
    @bruville 5 лет назад +2

    Very good explanation! Thanks!

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

    Thank you for making it understandable.

  • @Sonny0276
    @Sonny0276 7 лет назад +1

    Great Video. Thank you for posting.

  • @dfence1985
    @dfence1985 8 лет назад +4

    Very useful and detailed. Thank you!

  • @user-dr7pb8ek7g
    @user-dr7pb8ek7g 7 лет назад +1

    Thanks for your lesson that's easy to understand.

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

    Very good explanation. Tank you so much!

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

    Very nicely explained 👍TY

  • @debayan89
    @debayan89 8 лет назад +2

    It cleared my doubts. Very very helpful.

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

    this was awesome

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

    Very clear!

  • @erlendstlie4431
    @erlendstlie4431 7 лет назад +1

    Thanks, really helpful

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

    That is very helpful

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

    Great, thanks man

  • @padminimurthy7160
    @padminimurthy7160 8 лет назад +2

    Very nicely explained

  • @yeoshuabenzaken7736
    @yeoshuabenzaken7736 6 месяцев назад

    in the first example (select 17) the exists is acting like a Boolean since the subquery is true so it brings everything in the outer query but in the 2nd example (the correlated query) exists is acting like "in" . how is that?

    • @DatabasebyDoug
      @DatabasebyDoug  3 месяца назад

      Sorry for the delayed response. You are right EXISTS is an operator that returns a boolean value. In the first "trivial" example, SELECT 17 always returns exactly one record, so EXISTS (SELECT 17) is always True. In the second example (correlated subquery), the subquery's results change based on a record in the outer query. So EXISTS() returns true or false, based on a record in the outer query, i.e., it is *correlated* with the outer query. You can phrase the same intended result using IN. In English, you could say "Categories where a Product exists in that category" or "Categories that are in a Category list drawn from the Products table". Both end up with the same result. I hope that helps!

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

    Good video!

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

    love it

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

    you the MAN!

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

    in subquey, can I select * or any random columns ? does it have to follow any patterns of select in subquery?

    • @DatabasebyDoug
      @DatabasebyDoug  9 месяцев назад +1

      Technically, you can use any column - EXISTS does not check any column values, only for the existence/nonexistence of a row. For code clarity, * is good because it is succinct and doesn't lead a reader of your code to think you have some other intention.

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

      @@DatabasebyDoug tks u

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

    Thx for the explanation. Do you mean exist will dedup and same with IN statement it will dedup too?

    • @DatabasebyDoug
      @DatabasebyDoug  3 месяца назад

      That's an interesting way to look at it. Yes! EXISTS will avoid looking at duplicates once it finds one that matches. Similarly with IN. But, I'm not sure whether it *efficiently* finds the first match.

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

    i don't understand. in the first example we get ALL CategoryNames from Categories if there is at least one row in subquery ? or only CategoryNames that have products?

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

      in the first statement the subquery is true, so the query returns all records. in the second, because of the correlated subquery, only individual records correlated with each true instance are returned. at least thats what i believe to be true

  • @oksanaveretiuk1653
    @oksanaveretiuk1653 8 лет назад +2

    Thanks a lot!

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

    Thanks a lot. What does the query return if WHERE EXISTS returns false?

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

      Hi and thanks for the question. As with any WHERE clause, if the WHERE clause evaluates to false for a record, the record is not shown. So if WHERE EXISTS returns false for a record, that record is not shown. If WHERE EXISTS returns false for *every* record in the outer query, then there will be no records in the result.

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

    Thank you Sir.

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

    May I ask you why Joins are more popular than the Exists clause? At least when I go to interviews or I speak to people they always ask me about Joins but never about the Exists clause

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

      because of the use cases these keywords support

  • @Sinha.ritesh
    @Sinha.ritesh 7 лет назад

    thanks 😃

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

    can we use where exists. or where not exists with insert query ?

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

      Hi Abdul. Sounds like you want to insert a record only if it doesn't exist? You can use WHERE NOT EXISTS in the SELECT that creates the records to be inserted, and you can reference the same table you are inserting into in your SELECT. You might also take a look at the MERGE statement - it might do everything you want.

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

    which software is that? ty

    • @DatabasebyDoug
      @DatabasebyDoug  8 лет назад

      +Chethan Prabhu I'm using Microsoft SQL Server.

  • @seanangeluasmolo1471
    @seanangeluasmolo1471 2 года назад +2

    exists are not for beginners xD