MySQL INNER JOIN Tutorial & Examples
This tutorial shows you how to write ANSI-Style inner joins with the INNER JOIN keywords. Included are a general description, some syntax examples and a comparison between inner and cross joins.
Note: In MySQL the join keywords JOIN and CROSS JOIN are synonymous with INNER JOIN. That means: All example statements found in this article work fine when you replace INNER JOIN with JOIN or CROSS JOIN.
Syntax
Here are syntax examples for the impatient. Basically, ANSI-style join conditions can be specified with two different keywords: USING and ON. Take a look at the following examples:
-- inner join with USING clause SELECT * FROM <firstTable> a INNER JOIN <anotherTable> b USING(<columnName>)
-- inner join with ON clause SELECT * FROM <firstTable> a INNER JOIN <anotherTable> b ON a.<someColumn> = b.<anotherColumn>
Now we should take a closer look at inner joins to understand what they really do.
Basics
Probably the most common join operation MySQL supports is an inner join. It identifies and combines only matching rows which are stored in two related tables. A join condition, which indicates how the tables are related, is added with the keywords ON or USING :
- ON is used when the relationship column has a different name
- USING is used when the relationship column has the same name in both tables
Take a look at the examples:
-- INNER JOIN with ON clause SELECT * FROM tableA a INNER JOIN tableB b ON a.someColumn = b.otherColumn
-- INNER JOIN with USING clause SELECT * FROM tableA a INNER JOIN tableB b USING(columnName)
Inner Join vs Cross Join
In MySQL, the keywords CROSS JOIN and INNER JOIN are synonymous. ANSI SQL defines a CROSS JOIN as a join without a condition which builds the Cartesian product of two tables. In that case, MySQL combines every row in the left table with every row in the right table and returns the result set.
-- inner join without a condition: cross join SELECT * FROM <firstTable> CROSS JOIN <anotherTable>
When you have to build the Cartesian product of two tables, use the CROSS JOIN keywords to indicate your intensions. It makes it easy to read your statement and of course, keeps your code more portable.
Inner Join vs Outer Join
The major difference between outer and inner joins is: an outer join is able to identify rows that were not matched by any row in the joined table. Therefor, if you’re searching for rows from tableA that have no related entry in tableB at all, you have to use an outer join. Please see the tutorial about outer joins for a more information.
More than one join
It’s also very common to add more than one join to a single statement. There is no special syntax required. You only have to write a second (a third, and so on) join:
-- more than one inner join SELECT * FROM tableA a INNER JOIN tableB b ON a.someColumn = b.otherColumn INNER JOIN tableC c ON b.anotherColumn = c.nextColumn
Inner Join with comma operator
An inner join can also be written with the help of the so called comma operator. Please read the dedicated tutorial about writing inner joins with the comma operator for more information.
You can host your MySQL projects on regular web hosting packages or a dedicated server. Nowadays, all of them should support inner joins.
Very good explanation on MySQl joins.
Was this answer helpful?
LikeDislikeThank you very much for your feedback!
Was this answer helpful?
LikeDislikeThank you very much
Really great work, I was searching basic for mysql JOINS, I think this is a great and easiest example. Thanks
Was this answer helpful?
LikeDislikeThank you very very much! It’s awesome if people like this site and it’s content! :)
Was this answer helpful?
LikeDislikethanku so much its very nice explanation from you really thanks.
Was this answer helpful?
LikeDislikeBig thanks, really great Tutorials about Joins.
Easy to understand and very short.
Was this answer helpful?
LikeDislikeReally great, easy-to-understand tutorials … much appreciated!
Was this answer helpful?
LikeDislikeThank you very much. You guys rock! :-)
Was this answer helpful?
LikeDislikecan u tell we one thing.
Can we use join statement to insert / update /delete the data inot multiple table at once?. we can do it by trigger. but can we use join?
thanks..
Was this answer helpful?
LikeDislikeHi there. I think you can only delete, update and insert data into one table at a time. When you do INSERT…SELECT or UPDATE…SELECT you can use join operation in the SELECT statement, though.
Was this answer helpful?
LikeDislikeThanks a lot.This is a great tutorial
Was this answer helpful?
LikeDislikeReally very easy to understand with the a lot of optional queries for a single work piece
Was this answer helpful?
LikeDislikeThanks for this awesome feedback! We really appreciate that. Thanks once again!
Was this answer helpful?
LikeDislikeYou showed an example of an INNER JOIN between multiple (>2) tables.
Can you also use the ‘comma operator’ alternative to join more than 2 tables, like:
SELECT *
FROM tableA a, tableB b, tableC c
WHERE a.someColumn = b.otherColumn
AND b.anotherColumn = c.nextColumn
I know it is possible in other DBMS’s, but would like to know whether I can use that in MySQL?
Was this answer helpful?
LikeDislikeHi. Thanks for your comment. This is possible in MySQL as well. For more readable statements I prefer to write ANSI-style joins (with INNER JOIN and so on), though. If you write larger queries, with sub-selects for example), it’s much easier to understand what a statement does.
Was this answer helpful?
LikeDislikegood tutorial…
Was this answer helpful?
LikeDislikeGreat dude ……………..’
Was this answer helpful?
LikeDislikeI have been doing simple equijoins for years. Your explanation and tutorial cut through Teh Complicated.
Thank you.
Was this answer helpful?
LikeDislikeNice..
Was this answer helpful?
LikeDislikenice tutorial……………..
Was this answer helpful?
LikeDislikeThank you very much! :)
Was this answer helpful?
LikeDislikeAwwh man, thankyou very very much!!
I’ll learn so many things by here!!
Was this answer helpful?
LikeDislikei have to join two tables having two common columns by Natural Join. and then the resultant table i have to left outer join with another table. how?
Was this answer helpful?
LikeDislikeHi. Can you paste the CREATE statements of the tables in question to pastebin.com? Just leave a link and some informations about the relationship between the tables/columns.
Was this answer helpful?
LikeDislikeThanks for this wonderful post! It has been very useful. I wish that you’ll carry on sharing your wisdom with us.
Was this answer helpful?
LikeDislikeI just want to ask using JOIN statement will increase the speed of searching some data in a database? is that correct? Can u also give a simple example a JOIN statement with the combination of WHERE?
Was this answer helpful?
LikeDislikeHi. Will improve the tutorial with WHERE examples.
Regarding performance: This depends. To make your joins fast, you have to use proper indexes. It’s hard to cover within an comment, but I can recommend the following book: http://www.mhprofessional.com/product.php?isbn=0071782796&cat=7
Was this answer helpful?
LikeDislikeExcellent.
Was this answer helpful?
LikeDislikeNo where on the net have I found such a great description of joins. Thank you for taking the time to explain.
Was this answer helpful?
LikeDislikeThank you very much, Sir! :D
Was this answer helpful?
LikeDislikeThanks to for explanation.
Was this answer helpful?
LikeDislikeSimple and straight forward.
Was this answer helpful?
LikeDislikeThanks !! :) this is one of the best article or tutorial on using join with more than two tables out there – if not the best. bookmarked but share buttons came back with errors for linked in and twitter.
Was this answer helpful?
LikeDislikenice one sir , thanks !
Was this answer helpful?
LikeDislikethank you so much for providing such a nice tutorial.
Was this answer helpful?
LikeDislikeI’m still learning from you, as I’m trying to reach my goals. I definitely enjoy reading all that is posted on your site.Keep the tips coming. I loved it!
Was this answer helpful?
LikeDislikeThank you so much for the great info! I am trying to join two tables one table defines a relationship between 2 users (userA, userB) and the other contains user information (id, name, email, etc…) I simply want to be able to display a users “friends” from the relationship table filtered by their user ID (session). I was advised to try and Join that tables and have been able to do that but I can’t seem to filter the results by the session variable… Any help is appreciated. Thanks so much.
Was this answer helpful?
LikeDislikeHi. Thanks for your feedback! Can you maybe post your table structure using pastebin.com ? Would make it much easier :)
Was this answer helpful?
LikeDislikeThank you sir. As I am a beginner this is helped me a lot.
Was this answer helpful?
LikeDislikeThis is the best tutorial i have ever seen for Mysql.
The Venn-diagrams penetrates right into brains to explain the concept.
Thanks a lot.
You rock…..
Was this answer helpful?
LikeDislikethis is a sweet lovely mysql website, plz visit it, thx.
http://swordsky.com/
Was this answer helpful?
LikeDislikeThanks Thanks Thanks Thanks Thanks a lot
you saved me friend. Best tutorial I have ever seen.
……….You really Rock………….
Was this answer helpful?
LikeDislikeThanks a lot for your feedback! We really appreciate if our small contribution to the world is somehow helpful for others :D
Was this answer helpful?
LikeDislike