How to do Subqueries in SQL with Examples

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

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

  • @kristiankelloway127
    @kristiankelloway127 7 месяцев назад +10

    The idea that a subquery in a FROM statement creates a table and starting the query process from there was a real lightbulb moment - thank you

  • @honeyclover5038
    @honeyclover5038 4 года назад +86

    Finally, a break through. Finally, someone who explains the use of sub queries well.

  • @dabeast0623
    @dabeast0623 4 года назад +17

    CANNOT EXPLAIN ENOUGH HOW MUCH YOU SAVED MY LIFE.

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

    Spending my Christmas binge watching your SQL tutorials haha so helpful. Thank you!!!!

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

      Haha, sounds like a good Christmas to me!

  • @Aaron-il5pk
    @Aaron-il5pk 4 года назад +12

    Thank you for telling us that even when you first learned this it was complex, because holy cow! I cannot wrap my head around this. I can kind of grasp whats going on, but I think you know the confusion that goes through a noob's mind. Thank you for your tutorials! I plan on watching many more of your videos.

  • @EverythingData98
    @EverythingData98 2 года назад +5

    I am getting ready for an internship using SQL as a data analyst and this made a ton of sense, thanks! I will be subscribing

    • @aadhyatiwari9688
      @aadhyatiwari9688 11 месяцев назад +1

      did you get the internship? just curious lol

  • @jorgehernandezdominguez6574
    @jorgehernandezdominguez6574 Год назад +3

    WOW! the way you explained that is like first doing a table to then going and making a query from that table was a breakthrough for my understanding of the concept. I really admire people who can transform concepts into more digestible material. Thanks

  • @sarahemslie6105
    @sarahemslie6105 4 года назад +39

    This is really helpful; more so than my online course! I look forward to watching more of your videos. Thank you!

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

    Just started a Business Systems Analyst entry level position that uses SQL everyday and this video has been so helpful! You explained it better than the corporate instructor, Khan Academy and my Udemy course! THANK YOU!!!!

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

    Your SQL knowledge is admirable.

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

    super helpful and you explain stuff in a way where you don't rush through it but also don't take ages on it.

  • @AlexMar-r
    @AlexMar-r Год назад

    So easy to understand without broken English. Keep them coming!

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

    I bought a course on Udemy, you solve like 10 doubts I had in just 1 video

  • @ripndipp
    @ripndipp 4 года назад +4

    Excellent explanation, these sub queries got me messed up but this video helped. Your great and explaining and hoping you succeed with your channel, you seem like a genuine dude.

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

      Thanks a lot for the kind words, means a lot!

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

    Absolutely amazing explanation and you give hope that you said it took you a while, now look at you.

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

    Very helpful, some key explanation that made it "click" with me. And most of all, I like that you confess to your struggle at the beginning, beacause I have my though times right now....

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

    Where the heck have you been? You’re the only useful person on RUclips!

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

    hope you would keep on making SQL tutorials (or other programming language). you help me a lot in my review for my assessment. you have explained each topic very well! I'm learning a lot from you.

  • @Peter-cx4ir
    @Peter-cx4ir 2 года назад +2

    Really great explanations. Easy to follow and perfect variety. Very much appreciate you posting these tutorials, they're beyond helpful. Thanks!

  • @Michael-1qaz2wsx
    @Michael-1qaz2wsx Год назад

    Thank you for explaining it the way you did, it just clicked for me. Great stuff! Keep up the good videos.

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

    Great video man!
    The best explanation of sub queries that I found!
    This is helping me a lot because I'm starting with a course of T-SQL

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

    A great, and much needed take on this subject.

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

    What does the 1,2 mean in the Group By 1,2?

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

    Really good explanation. I will be glad to share this with developers who write "expensive" queries with complex join statements.😀

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

    overwhelmed?! tuh... i am terrified!
    i came to you, from my online course, for some type of clarity.
    what did I get you might ask. HUMBLED!
    tfs this video anyway, I'm hopeful I will find some gems on your channel.
    -your new (maybe in over her head) subscriber

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

    There are also subqueries in join. It's hard to find a material as good as this. Leaving a like and comment to support your channel.

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

    This was very helpful. It saved me a lot of time. I understood it the first viewing and was able to apply the concepts with ease and complete my homework on time. Thank you, you do an excellent job. I will come back for future questions in SQL.

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

    Thank you for sharing, I feel stupid not understanding, but you kind of make it clearer

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

    Actually for me this makes amazing sense. Thanks for the tutorial.

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

    Thank you, a great explanation and examples for subqueries.

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

    I didn't understand the concept one bit and I didn't understand why we should use them when I was in the university, but I understand now lol. Thanks for the tutorial. It's simple, yet really helpful for me

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

    I like how you explain, keep up the good work.

  • @samuelopoku-acheampong5601
    @samuelopoku-acheampong5601 4 года назад +2

    Thanks for the tutorials, you have being of great help on this journey...Keep them coming

  • @EK-ee4gb
    @EK-ee4gb 4 года назад

    Awesome explanation. Thank you very much. I was completely lost on subqueries.

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

    You sir are such a great teacher. Thank you

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

    you just helped me through my project in SQL, thankss a lot!

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

    And The search ends here.... Perfect 💯

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

    great videos! Its soo much easier too understand when watching your tutorials! Please do more advanced videos with window functions etc.

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

    Thank you! You look so positive and warm person :)

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

    This is exactly what I was looking for - thank you for sharing!

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

      Does the internal table have to be the same as the outer table? "orders" in your example.

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

    All your videos are very helpful and well explained! Helps me a lot in understanding SQL better. Are you planning on doing any more on SQL soon?

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

    Brilliant explanation The online course I took had me lost at this part.

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

    Simple and direct. Just what I was needed.

  • @1mmoralls
    @1mmoralls 2 года назад

    Great video! Thank you so much for bringing a clear explanation of this topic. I look forward to viewing your other videos.

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

    This is so helpful! Thank you!! Rec: time stamps for each example

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

    I love the way you teach. You are clear and concise. How long have you been writing SQL?

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

    Thank you for this brilliant video. Please upload more :) :).

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

    Thank you for summing up subquery. Thank you.

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

    Average number of orders per customer is a bit ambiguous as this could mean per specific customer instead of per one general customer.

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

    Hi there, I really liked the explanation, just a suggestion if you would give a very quick overview of the heads of the dataset so that it is easier to comprehend more how the query/subquery is extracting the data.

  • @kayceeDis
    @kayceeDis 4 месяца назад

    This is an excellent video. Thank you!

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

    I am here wondering if another way to write the first example with freight as
    SELECT AVG(Freight), customer_id
    FROM orders
    GROUP BY customer_id
    Would give same result as the Subquery in the first example?

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

    Question regarding the first example; could you not achieve the same result without a subquery by just adding avg(freight) as avg to the original select statement?

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

    Quick question: what is the group by 1,2 doing? is that just ordering them by the order in which they columns were entered in the select statement?

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

    Hi, may you help me with this if you don’t mind?
    how to exclude from the results similar values of the same row from two columns that are being compare. For example, in the same row, column 1 is 4567 and column 2 can be 4567 or 4568 or 4566. Since numbers are similar, the idea is to exclude that row if the number in column 2 is the same as column 1 or the number is plus or minus 1 of column 1’s number.
    If the column 1 is 4564 and column 2 is 4566, it would not be excluded since the number are not similar.
    I am trying to account for te value in each row of the column that has been rounded to the nearest whole number.

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

    Well explained thanks a lot finally was able to get a better grasp of Sub Queries

  • @tyroneogarrojr.453
    @tyroneogarrojr.453 2 года назад

    Great video, very comprehensive!

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

    how can i get a total count of rows then find the average of it using a date range?

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

    Great video easy to understand

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

    It seems the subquery in the where clause is easiest to understand. SQ in the select statement is probably used to compare as it's gong to give only a single value irrespective of all the records. And finally SQ in the from can be used to create a temp table with aggregates; which will be used by outer query to do further aggregates.

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

    Beginner here with some experience. I don't get the group by 1 , 2. I'm used to grouping by the field name. Is Group BY 1, 2 the same as typing Group BY customerID, shipcountry ?

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

    thanks for the explanations. Why wouldn't you be able to take the average directly instead of needing to first subquery? Also, in the first subquery example, why do you need the subquery instead of writing SELECT AVG(freight)?

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

    Thanks, great explanation!

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

    Amazing Explanation Thank you 🙌

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

    Can we use
    SELECT * FROM (SELECT * FROM USERS) like this ?

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

    wait so why did you use select for avg the first time but the second time you used it without select. Is there a rule?

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

    Lovely. Thanls a lot for this great share 👌🙂

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

    Great explanation. Thank you :)

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

    explained very well. thank you

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

    Like how do WE make a Query of two cities in a STATION table with the shortest and longest CITY names, as well as their respective lengths (i.e.: number of characters in the name). If there is more than one smallest or largest city, choose the one that comes first when ordered alphabetically.

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

    I don't get it why need to use subqueries for the select clause average freight. Why can't just use avg(frieght)? Arent that the same thing?

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

    you are a champion thanks a lot..(im buying your book also!!!)

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

    When doing a sub query (inner SELECT) and referencing it from the outer SELECT statement can I use a WHERE clause in the outer SELECT Statement or does it need to be in the Inner SELECT Statement? Similarly once I have established my inner SeELECT statement can I then use those columns in a CASE Statement?

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

    That was great Explaination.

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

    Thanks for this sql series

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

    it should be a crime that universities get away with charging 50k of tuition for a shittier version of the stuff we learn for free. Really helpful!

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

    You’re a real G

  • @Noah-357
    @Noah-357 4 года назад

    What about inside joins( Select........ )?
    Also, where to find other good sources to understand subquery ?

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

    What do you mean by "group by 1", and "group by 1,2"

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

    Thank you for your effort, keep moving

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

    Can i use a subquery from two tables?

  • @rackellewilkinson-alston
    @rackellewilkinson-alston 3 года назад +1

    loved this!!!

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

    Thanks helped me out heaps!

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

    Subquery in the where clause I get, no problem. Example for subquery in the select statement made no sense to me, why repeat that same figure for every row? Sub query in the from clause...gonna need more practice. Also, what's the difference between a subquery in the from clause and a CTE?

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

    Is that num_orders number of orders or order number?

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

    Very helpful - thank you !!

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

    nice job!

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

    Great explanation! Do you have a video for Correlated Subquery as well? It is super confusing

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

    1:47 how is that more useful than "avg freight per customer" :
    SELECT customerid, avg(freight)
    FROM orders
    GROUP BY customerid

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

    Awesome vid! thanks bud :)

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

    How to find sum of the sum using dub query

  • @abykills
    @abykills 5 месяцев назад

    where is your ER diagram? or where are you getting the info to make your queries

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

    Hi How's going ? Thank you for the video!
    I have a question about "inner query gets executed first", does it only apply to the subquery in FROM statement or no matter in SELECT/ FROM / Where statement ?
    Thank you very much!

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

    Dude this was great.

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

    Hi! What program are you using to code? thank you! and what DBMS are u using

  • @aneeshchandramouli7440
    @aneeshchandramouli7440 4 года назад +4

    Great explanation! What do the numbers 1 and 2 represent in the command "GROUP BY 1, 2" ?

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

      what i wondered too

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

      @@summer9380 My guess is referring to the columns in the select. As group be the first attribute then group by the second (cant remember the exact attribute names).

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

    very helpful video. I just have a question, which one is better(most "performant") , doing a subquery or a join?

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

    Great Thank you. How can i use joins with it?

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

    I hope you don't mind me asking for a help with my sql code..how can I reach you?

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

    Thank you bro man 💥