SQL Join 3 Tables: How-To with Example

Поделиться
HTML-код
  • Опубликовано: 25 июл 2024
  • 📝 Get my free SQL Cheat Sheets: www.databasestar.com/get-sql-...
    🎓 Learn and become confident in SQL: databasestar.mykajabi.com/sql...
    When working with SQL, you'll learn to join two tables together so you can get data from both of the tables.
    But what about joining 3 tables?
    This is possible to do in SQL. It works in a similar way to joining two tables and can be extended to join to 4 or 5 or many more tables.
    In this video, you'll learn how to join 3 tables together in an SQL query, and follow a step-by-step example of a query that does this.
  • НаукаНаука

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

  • @bhumikalamba186
    @bhumikalamba186 2 года назад +11

    Thanks for the lucid explanation.
    Summary:
    1. SELECT columns to show from the first table
    2. (INNER/OUTER) JOIN to the second table
    3. add the columns to join ON
    4. add any additional columns to step 1.
    5. (INNER/OUTER) JOIN to third table
    6.add the columns to join ON . The columns can belong to the first/second table and then join it with the column for the third table
    7. add any additional columns to step 1.

  • @scottgee3089
    @scottgee3089 2 года назад +13

    This video saved me. You explained this better than a text book, 15 minutes video, and a lecture from my prof combined did. Thank you

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

    I have been trying to make sense of joins for the past 4 weeks - but the very essence of it had eluded me. NOW I understand! Thank you for simplifying this to the point where even I could understand it!

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

      Thanks! I'm glad you found it useful and are able to understand it!

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

    Clear and simple presentation, thank you very much

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

    Extremely easy to understand. Thank you so much for this!!!!

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

    Thank you! I have watched so many videos where I get completely lost. I was able to follow along in the order and understand how! I really appreciate you and the method you teach.

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

      Thanks! Glad it helped you understand it.

  • @bebe4235
    @bebe4235 Год назад +2

    hours upon hours of trying to figure this stuff out and your short video was all i needed. wish I could have found it sooner. THANKS!!

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

    Thank You! I've been banging my head against a wall trying to figure this out. This video finally helped me. Many many thanks!

  • @jordiadistira
    @jordiadistira 4 месяца назад +1

    Thank you so much! Just 2 words, "Amazing Explanation"

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

    Thank you, thank you, thank you!!! Very clear!

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

    Thanks for the illustrated example

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

    Great video, still very relevant after 2 years!

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

    Needed to turn a setting off on certain records in one table based off of a category stored in another table. I'm just beginning to learn SQL for my job and this helped!

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

    Thanks looks so easy now, well explained

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

    Thanks, very easy to understand. :D

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

    Ive been rattling my brain for over an hour...to do what you did in 4:05....thank you

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

    Thanks so much! This saved me! Love from Sri Lanka

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

    Thank you sir, could not have finished my MySQL project without. Thank you!!!!!!!!!

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

    THANK YOU SO MUCH!

  • @SZU1881
    @SZU1881 10 месяцев назад

    Fantastic! Thank you!

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

    Saved my life with this one

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

    It really helpful. Thanks

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

    Thank you so much!

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

    Thank you!

  • @redlobsta1
    @redlobsta1 2 года назад +7

    it would be great if you could include what the tables originally looked like and how they look after each statement.

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

      Thanks for the feedback! I’ll keep that in mind for future videos. I’ll see if I can add something for this to the Description area.

  • @The.Wellerman
    @The.Wellerman 2 года назад

    Thank you thank you! This helped me with a project for school. Thank you a ton!

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

      Awesome! Glad it helped.

    • @The.Wellerman
      @The.Wellerman 2 года назад +1

      @@DatabaseStar I ended up passing the project. Again, thank you.

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

    i enjoyed the video.thank you dad

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

    Lifesaver thank you sooooo much

  • @Metanfetamino-san
    @Metanfetamino-san Год назад

    thanks to your video

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

    Really specific and helpful,thank you

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

    Great explanation

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

    thanks a lot ❤️

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

    You are a life saver, I am assuming this can be applied for even more than 3 tables, I definitely need to master MySQL for college projects, tysm.

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

      Thanks! Yes you can use the same process for as many tables as you need.

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

    Very helpful, thankyouuuu

  • @AM-Boluke1210
    @AM-Boluke1210 4 месяца назад

    Thank u 👍

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

    I learned something new

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

    The diagram at the beginning of the table is misleading. It shows it as A join B, B join C and A join C. The query however is just two joins, A join B, A join C. I was expecting the video to be dealing with a circular join. Thank you and keep them good videos on complex data modeling concepts coming.

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

      That's a good point! I can update the thumbnail to make it clearer (I don't think I can update the video), but this is good to know.

  • @gauravsrivastava17
    @gauravsrivastava17 Месяц назад

    Steps make it so easier
    Step 5

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

    Is this standard SQL, PL/SQL or T-SQL? Or it does not matter? :-) Thanks for the video! I just failed an interview with this EXACT question. Feeling awful. But life goes on!

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

      This is standard SQL. The inner join, left join, and right join are all part of the SQL standard. There are some extra join features in specific database types, but I don't think I mentioned any of them in this video.

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

    How to add table with different column name

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

      You should be able to use the same process. You can specify the column name in the ON clause of the join. For example, if table 2 has a column called some_other_id: JOIN table2 ON table1.id = table2.some_other_id

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

    I’m using SQL management server and I need with the scenario, I have table A, junction table and Table B I need one row for table A and concatenate multiple rows of table b into one row with a comma. I’ve search the hours and can find no clues at all. I’d really appreciate it if you help me out

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

      Hey, I think you would need to use a technique called “string aggregation”. This is done using a function called string_agg in sql server. If you have the right results in separate rows showing for table a and b, you can then use this function to combine rows from table b into one row.

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

    Mate I wish I had this back in uni.

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

      Thanks! Yeah me too, I’m glad it’s helping many people.

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

    Excuse me Sir I have a question. So in step 5 and 6, when joining the 3rd table and later specifying the target columns, suppose now I want to join based on the columns on the 2nd table & the 3rd table, not the 1st table & the 3rd table. What decides which join type to use? In this case does the sequence of specifying table names matter? What if there're more than 3 tables? Thank you in advance!

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

      Good questions!
      - When joining the 3rd table, you can join to any column in either the 1st table or 2nd table. It depends on how you write the query and add the columns and what makes sense for your data.
      - The results that you need will determine the join type to use. I see either an INNER JOIN or LEFT JOIN most commonly, depending on whether you want to see rows that have matches or not.
      - The sequence that you specify the tables will not matter for an INNER JOIN, but it will matter for a LEFT JOIN and RIGHT JOIN.
      - If there are more than 3 tables, you repeat the process: join to table 4, then specify the join condition to either table 1, 2, or 3.

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

      @@DatabaseStar Thank you Sir for your answers, it makes sense! And thank u for your prompt reply!

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

    is language id the same number as publisher id in your example??

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

      No, in this example they are different fields.

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

      @@DatabaseStar so how are they equal to each other if they are not the same number type?

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

    Please I did some but data do not display in the table

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

      It could be because there is no matches based on your join criteria

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

      @@DatabaseStar thank you very much, it is now working

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

    Hello sir... Thanks for making this video. Result will get change if order of the tables changes? Pls reply. 🙏

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

      Thanks! No, the result should stay the same if you are using an Inner Join. If you're using an outer join (e.g. left join or right join) then yes the result will change.

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

      @@DatabaseStar.. Thank you sir.

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

    hm, I am getting "ambiguous column name" error.

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

      Ah that’s because the column name exists in more than one table and the database does not know which column you want. If you add the table name and a . Before the column it should work. Eg customer.firstname instead of just firstname.

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

      That worked! Thanks for responding and clarifying!

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

    How about show the result in a final form? Your title says with example. However no example....

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

      The final result is shown from about 3:16 where the query has fields from three different tables, which is the example. Were you looking for something else?\

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

      Like how can i get my tittle

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

    This query wouldnt work lol

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

      Oh, what error are you getting?

  • @X-Agn
    @X-Agn 2 года назад

    Lol show inner with left join . Don't show easy one

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

      Good point. Left join would be the same query with Left instead of Inner. But the results would be different

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

    Thank you!