Quick Tip: Working with MySQL and INNER JOIN
videos

Quick Tip: Working with MySQL and INNER JOIN

Tutorial Details
  • Topic: SQL JOINs
  • Difficulty: Moderate
  • Estimated Completion Time: 5 Minutes

In today’s video quick tip, we’ll go over when and why to use JOINs when working with your databases.

Add Comment

Discussion 60 Comments

  1. Robert says:

    Found this really helpful. Thanks a lot.

  2. John Cox says:

    Heh, another lost reference:). Nice job.

  3. Lol, I had just read about this, and thought, man, I wish there was a quick tip for this.

    So I logged on to Nettuts+ to search for it, and it was right here, on the front page.

    Thanks Jeff!

    • I read about it in the SitePoint “Build Your Own Database Driven Web Site Using PHP & MySQL”, a book I recommend to PHP beginners, as it’s very helpful. I wouldn’t call myself a beginner though, it’s got some nice theories applied, that can be brought elsewhere quite easily.

    • Edgar says:

      Me too!! Just reading about JOIN, and I thought, “Let´s go to internet”.

      Suprise, in my bookmarked Net.Tuts. LIke magic.

  4. Zack says:

    I was literally struggling with joins last night coding an app and look what shows up on tutsplus the next day. Just in time! how ironic. This is going to help me out a lot i hope. it seems like these types of queries are mal-documented.

    Thanks a lot nettuts!

    • Zack says:

      I think this is a great starting point for a tutorial, but it would be wonderful if you could make a longer more in depth look at all the types of joins and when and why to use them.

      It doesn’t even have to be a video, as long as it is thoroughly explain because im getting a lot of error like ambiguous ids and col name “blah” doesn’t exist.

      I look forward to more about these join tutorials.

      Thanks Jeff!

  5. Navarr says:

    Is doing something like

    SELECT `tasks`.`id`,`title`,`description`,`users`.`name` FROM `tasks`,`users` WHERE `tasks`.`user_id`=`user`.`id`; wrong?

    Is one better than the other?

    • Juan says:

      Using JOIN is faster, obviously you won’t notice it in a simple query like that, but in a longer, meaner and complex query you propably see the difference.

    • Burak says:

      What you did is technically an OUTER JOIN followed by a WHERE clause. I would expect MySQL would optimize a simple query like this so it wouldn’t take any longer than the INNER JOIN, however, it would be a better practice to utilize the ON clause.

    • Daniel says:

      I guess it depends on what you mean by “better”. I think mySQL will convert this to an INNER JOIN internally, so it might be a tad slower if that’s what you’re asking.

      I always hesitate to say something is “wrong” if it solves the problem at hand. I, personally, would not want to do it this way because it’s to easy to read past the code not realizing that you’re actually doing a JOIN and not a simple SELECT.

  6. alan says:

    Nice job,This is what I was looking for..
    @Damon Bridges,

    thanks for recommendation !:)

  7. Alistair says:

    Hey Jeff, thanks for this quicktip! Very very useful. INNER JOIN is something we studied at college although I could make it work from instruction from Lecturers it never really sunk in ya know. Over 2 years actually of MySQL tuition. Yet your way of explaining has really helped it. A simple “on the condition” really drove it home, and it’s power.

    Many thanks

    Alistair

  8. KalebAustin says:

    Nice Tut, Jeffrey, however….

    Its “My S-Q-L”.

    =)

  9. Nick says:

    Great tip, but would have been good to do a quick mention of outer joins as well. Inner joins only return exact records matched from both tables, whereas outer joins (left or right) include all records from one or the other tables whether there is a match between them or not.

  10. Eric says:

    I’ve created a third table, basically a join table that just assigns task_id and user_id. Is there any benefit to doing it that way or am I just wasting time?

  11. Codeforest says:

    I really admire how Jeffrey can say so much in under 5 minutes video. Great job

  12. arnold says:

    nice jefff , this is something new you do in “quick tips”

  13. Julius says:

    Hey Jeffrey,
    nice quick tip.
    Could you also explain the difference between Inner, Left and Right Join?
    That would be really great.

    • Monk says:

      Inner join – query compares all row of both columns to find which pair satisfy the join-predicate, these pairs are put in the resultset

      Left (outer) join:
      - resultset contains all the records from the left column, and adds the values from the right column wich satisfy the join-precidate.
      - if join-predicate isn’t satisfied, it contains the record for the left column and adds “null” for the right column
      - if more than one record satisfies the predicate, it repeats the record from the left column, adding the different records from the right column

      Right (outer) join:
      - the same as left (outer) join, but switch the columns

      Full outer join:
      - resultset contains all records for both columns
      - if join-predicate isn’t satisfied, records for left or right column will be containing “null”
      - if join-predicate is satisfied, records for left and right column are combined

    • Jeffrey Way says:

      I had planned to; however, one of our writers, John Cox, will be submitting a more in depth overview of JOINs in the next week or so. That should cover everything.

      • Josh says:

        Can not wait for this, the app that im creating is hinging on me figuring out JOINS thats Jeff :)

    • Peter says:

      Julius, see http://www.pafumi.net/SQL_Joins.html for a good visual-aided description (it also appears to be where the image associated with this article was borrowed from).

  14. Silwing says:

    Clean and simple introduction to joins! Just what I needed to get it ^^ thanks a lot.

  15. Darren says:

    Wow, never really did anything with mysql, but that quick tip got me kind of excited about it lol

  16. Inner joins are great for bringing tables together, thanks for the tut.

  17. Matt Bridges says:

    You never cease to amaze me, Jeffrey. Great job! Excellent!

  18. michael says:

    nice tut, exactly what i was looking for right now ;)

    jeffrey, how do you group your icons in your bottom-bar?

    • adrian says:

      there’s a terminal command you can use to put a separator on the left or right side (applications or stacks side) and then you can move it like a regular app. I also separate my apps and I use a prefpane called secrets http://secrets.blacktree.com/ that has a database of all kinds of terminal commands like that.

  19. adrian says:

    woah, i had no clue mysql was so powerful

  20. Ross says:

    Jeffrey, do you know if there is a Snippets program like the one you used in the video for Windows? I am unfortunately stuck using Windows for my web development, but I would love to use a program like that.

  21. vanderson says:

    You have been paid to speak to mysql?

  22. Davidmoreen says:

    Joins are the effing best. Oddly enough I only learned about them three months ago. Before I learned about joining I would always do one query, get it’s array and do the second query…

  23. Peter says:

    Oh my god! Joins?! That’s so awesome, never heard about this before! Unbelivable they inventies this now!

  24. Anouck says:

    Nice Tut!!!

    Now we need one for Left and Right joins, outer joins

  25. Brad says:

    Its so much easier to learn things like this, to actually see them performed. Thanks Jeff

  26. Hi Jeffrey,
    fantastic tip! :)

    Maybe I’m OT but I’d like to know which client you use in the tutorial.

    Thanks :)

  27. Rob says:

    Joins constantly give me grief in MySQL. I would love to see further coverage on the various types of joins and how to effectively use them. My biggest request though; would be a tutorial over a screen cast (or perhaps in addition).

    Granted; it’s not really a quick tip at that point :D

  28. What’s the difference between using inner join and selecting from multiple tables? Like this for example:

    SELECT tasks.id,tasks.title,tasks.details,users.name
    FROM tasks,users
    WHERE users.id = tasks.user_id

  29. Syahmi says:

    Cool! Really helpful.

  30. Muhammad Omar says:

    Thanks Jeff. But i have a totally different question, which database is better, SQL server or MySQL? and why?

  31. rickycheers says:

    Thank’s a lot! simple and useful..

    It’s allways cool to learn the basics this way ;)

  32. Elan says:

    where i can download Querious?

  33. niks says:

    Hey, Really Nice tutorial on mysql. Thanks for sharing it. Really useful.

  34. Andrew-David says:

    Let’s say I want to fetch rows from two tables, linked by “user_id”, where user_id=1 only.
    But I want to order the results based on the “order” row which they both have.
    Example:

    table1:
    user_id | order | text
    1 | 1 | blah
    1 | 3 | lorem
    2 | 1 | info

    table 2:
    user_id | order | text
    1 | 2 | ipsum
    1 | 4 | dolor
    2 | 2 | text

    And I want to return:
    -blah
    -ipsum
    -lorem
    -dolor

    What would the query for that be?

  35. David says:

    Sorry guys, cannot open this tutorial…

    I do pay regular premium subscription but as soon as I log in, the only thing I can do with this tut is to download the sources and read the header, nothing else… using Firefox…

    any help?

    Thanks a lot

    David

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.