How to do Subqueries in SQL with Examples

แชร์
ฝัง
  • เผยแพร่เมื่อ 6 ต.ค. 2024
  • Step-by-step tutorial shows you how to use SQL subqueries in your SELECT statement & FROM and WHERE clauses!
    Queries:
    1) A subquery in the SELECT statement: SELECT customerid, freight, (SELECT AVG(freight) FROM orders) FROM orders
    2) A subquery in the FROM clause: SELECT shipcountry, AVG(num_orders) FROM (SELECT customerid, shipcountry, count(*) AS num_orders FROM orders GROUP BY 1,2) sub GROUP BY 1
    3) A subquery in the WHERE clause: SELECT * FROM orders WHERE employeeid IN (SELECT employeeid FROM employees WHERE LOWER(firstname) LIKE '%b%')
    Get the Northwind Database: • How to Load a Sample P...
    Support the Channel:
    1) Subscribe:
    2) DataCamp:
    3) Udacity:

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

  • @honeyclover5038
    @honeyclover5038 3 ปีที่แล้ว +81

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

  • @kristiankelloway127
    @kristiankelloway127 4 หลายเดือนก่อน +3

    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

  • @dabeast0623
    @dabeast0623 4 ปีที่แล้ว +15

    CANNOT EXPLAIN ENOUGH HOW MUCH YOU SAVED MY LIFE.

  • @juliakristavilladiego4425
    @juliakristavilladiego4425 3 ปีที่แล้ว +17

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

    • @becomingADS
      @becomingADS  3 ปีที่แล้ว +2

      Haha, sounds like a good Christmas to me!

  • @Aaron-il5pk
    @Aaron-il5pk 3 ปีที่แล้ว +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.

  • @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

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

    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 8 หลายเดือนก่อน

      did you get the internship? just curious lol

  • @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!

  • @topemorada
    @topemorada 3 ปีที่แล้ว +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.

  • @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!!!!

  • @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!

  • @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!

  • @kramoo
    @kramoo 4 ปีที่แล้ว +7

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

  • @AlexMar-r
    @AlexMar-r 9 หลายเดือนก่อน

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

  • @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

  • @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....

  • @liwaygorzon
    @liwaygorzon 6 หลายเดือนก่อน

    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.

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

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

  • @EK-ee4gb
    @EK-ee4gb 3 ปีที่แล้ว

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

  • @samuelopoku-acheampong5601
    @samuelopoku-acheampong5601 3 ปีที่แล้ว +2

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

  • @MichaelCarle-f9w
    @MichaelCarle-f9w ปีที่แล้ว

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

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

    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

  • @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.

  • @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.

  • @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?

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

    Thank you, a great explanation and examples for subqueries.

  • @TheOrionMusicNetwork
    @TheOrionMusicNetwork 3 ปีที่แล้ว +2

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

  • @jeevak840
    @jeevak840 8 หลายเดือนก่อน

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

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

    A great, and much needed take on this subject.

  • @Virtualexist
    @Virtualexist 3 ปีที่แล้ว +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.

  • @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.

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

    Where the heck have you been? You’re the only useful person on TH-cam!

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

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

  • @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

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

    Thank you for summing up subquery. Thank you.

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

    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

  • @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.

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

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

  • @jennifergonzalez6431
    @jennifergonzalez6431 3 ปีที่แล้ว +2

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

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

    You sir are such a great teacher. Thank you

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

    Simple and direct. Just what I was needed.

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

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

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

    This is an excellent video. Thank you!

  • @VaaniMogan
    @VaaniMogan 4 หลายเดือนก่อน

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

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

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

  • @akj3388
    @akj3388 2 ปีที่แล้ว +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.

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

    And The search ends here.... Perfect 💯

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

    Thanks for this sql series

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

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

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

    Thanks, great explanation!

  • @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 3 ปีที่แล้ว +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).

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

    you'd think it'd be easy to find this info out there, but my online SQL course didn't cover it as well as you did. Thanks for the video!

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

      was it udemy BA analyst course by any chance because i though the same thing haha

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

    explained very well. thank you

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

    Great video easy to understand

  • @tyroneogarrojr.453
    @tyroneogarrojr.453 2 ปีที่แล้ว

    Great video, very comprehensive!

  • @locke827
    @locke827 8 หลายเดือนก่อน

    Great explanation. Thank you :)

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

    Thank you for your effort, keep moving

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

    Thanks helped me out heaps!

  • @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!

  • @michael95621
    @michael95621 3 ปีที่แล้ว +2

    Just a note for others; The first type of subselect, the way it was written will apply the aggregate of the whole table to each row. As far as I know all major versions of SQL support this.
    It's useful if you need min/max/grand total at the table level. For instance;
    select customerid, sum(freight) as subt, (select sum(freight) from orders) as grandt, subt/grandt from orders group by 1.
    This will get you the customer ID, sum of freight by customerid, and sum of freight for all customerids, and the % of the grandt each customerId makes up.
    Some versions of SQL support using this type of subselect to directly compute the subt. The modification would be;
    (SELECT AVG(freight) FROM orders as o1 where o1.customerid = o2.customerid) from orders as o2. --This should achieve the same result as sum(freight) above, but my experience is not all versions of SQL are capable of understanding how to do this and will throw errors or bad results.
    A final note. Subqueries are also very useful if your version of SQL doesn't support pivot and unpivot functions. What I have also found is almost any time you are creating a temp table you could probably be using a subquery in 1/3 the lines and a lot more clear.

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

    Very helpful - thank you !!

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

    That was great Explaination.

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

    Amazing Explanation Thank you 🙌

  • @rackellewilkinson-alston
    @rackellewilkinson-alston 3 ปีที่แล้ว +1

    loved this!!!

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

    thanks man, appreciate the explanation

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

    Dude this was great.

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

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

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

    Awesome vid! thanks bud :)

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

    Lovely. Thanls a lot for this great share 👌🙂

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

    This video saved my ass, THANK YOU.

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

    Thank you! This helps me a lot.

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

    Great videos - keep it up!

  • @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)?

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

    You’re a real G

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

    Thank you bro man 💥

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

    Thank you from Italy! :) Just a question: what does GROUP BY 1, 2 mean? It is the first time that I see it...

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

      it refers to the order of the columns, i.e. 1 is customerId and 2 is shipcountry

  • @dawitworku821
    @dawitworku821 5 หลายเดือนก่อน

    Very helpful. Than you.

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

    Hi Sir, can you make videos on how to interpret EXPLAIN statements, how each line of explanation tallies with each query or subquery, and how the fixes translates to the new EXPLAIN statements? Thanks for making this video by the way!

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

    Great content! Thanks!

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

    Thank you for this video

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

    nice job!

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

    Great explanation. Thank you!

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

    Thank you for the video

  • @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.

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

    amazing man good job

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

    This helped, thank you!

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

    thank god for this video!

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

    Thank you for this video, think subqueries is slowly starting to sink in

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

    AWESOME! thank you for your effort!

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

    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?

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

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

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

    Thank you again

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

    Great explanation...thanks!

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

      Thanks Erin! Glad it was helpful to ya!

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

      Why were you using Grouping By 1 and 2 in your subquieres?

  • @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.

  • @Noah-357
    @Noah-357 3 ปีที่แล้ว

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