Summary: In this tutorial, you will learn how to use MySQL INNER JOIN clause to select data from multiple tables based on join conditions.

Introducing MySQL INNER JOIN clause

The MySQL INNER JOIN clause is an optional part of SQL SELECT statement. The MySQL INNER JOIN clause appears immediately after the FROM clause. Before using MySQL INNER JOIN clause, you have to specify the following criteria:

  • First, you need to specify the tables you want to join with the main table. The main table appear in the FROM clause. The table you want to join with appear after keyword INNER JOIN. Theoretically, you can join a table with unlimited number of tables. However, for better performance you should limit the number of tables to join based on join conditions and volume of data in those tables.
  • Second, you need to specify the join condition or join predicate. The join condition appears after the keyword ON of MySQL INNER JOIN clause. The join condition is the rule for matching rows between the main table and other tables being joined with.

The syntax of the MySQL INNER JOIN is as follows:

1 SELECT column_list
2 FROM t1
3 INNER JOIN t2 ON join_condition1
4 INNER JOIN t3 ON join_condition2
5 ...
6 WHERE where_conditions;

For example, if you join two tables A and B, the MySQL INNER JOIN clause compares each record of the table A with each record of table B to find all pair of records that satisfy the join-condition. When the join-condition are satisfied, column values for each matched pair of record of table A and table B are combined into a returned record. Note that the records on both tables have to match based on the join-condition. If no record on both table A and B matches, the query will return an empty result.

Avoid column ambiguous error in MySQL INNER JOIN

If you join multiple tables that has column with similar name, you have to use table qualifier to refer to column to avoid column ambiguous error. Suppose if table tbl_A and tbl_B has the same column M. In the SELECT statement with MySQL INNER JOIN clause, you have to refer to column M by using the table qualifier as tbl_A.M or tbl_B.M (table_name.column_name).

Another effective way to avoid column ambiguous is by using table alias. For example, you can give A as the table alias of the table tbl_A and refer to the column M as A.M so you don’t have to type again and again the long table name in your SQL statement.

Example of MySQL INNER JOIN clause

Let’s take a look at two tables: products and orderDetails in our sample database.

MySQL INNER JOIN Tables

The products table is the master data table that stores all products. Whenever a product is sold, it is stored in the orderDetails table with other information. The link between products table and orderDetails table is productCode.

Now, if you want to know what product was sold in which order, you can use the MySQL INNER JOIN clause as follows:

1 SELECT A.productCode, A.productName, B.orderNumber
2 FROM products A
3 INNER JOIN orderDetails B on A.productCode = B.productCode;

MySQL INNER JOIN Query Result

There are more returned rows that are not listed on this screenshot

The MySQL INNER JOIN clause compares each row of table products and orderDetails table to find a pair of rows that has the same productCode. If a pair of rows that have the same, the product code, product name and order number are combined into a returned row.

In this tutorial, you have learned how to use MySQL INNER JOIN to select data from multiple tables. You have also learned how to use table qualifier to avoid column ambiguous error in MySQL INNER JOIN clause.

Related posts:

  1. Retrieving Data in a Range Using SQL BETWEEN
  2. How to Use MySQL Limit to Constrain Number of Returned Records
  3. How to Use MySQL LIKE to Select Data Based on Patterns Matching
  4. Using MySQL SELECT Statement to Query Data
  5. Managing Database Index in MySQL

Leave your comment

You must be logged in to post a comment.


Link Exchange Spritual Place for Bhakti | bhaktiguru.com Business Franchise in India | businessfranchiseindia.com Free Directory Submission & Link Submission | ab-directory.com Big Dwarka Business Listing | bigdwarka.com Kolkata Online Business Listing | onlinekolkata.com Patna Online Business Listing | onlinepatna.com Dwarka Online Business Listing | onlinedwarka.com Bihar Online | First Bihar's Portal | bihar-online.com Big Bihar Business Listing | bigbihar.com Dynamic Website Development | dynamicwebsitedeveloper.com Noida Online Business Listing | onlinenoida.com SEO Service Delhi & India | seoservicedelhi.com SEO Experts Delhi & India | seoexpertsdelhi.com Big Patna Business Listing | bigpatna.com Bihar schools Listing Free Schools Ad | biharschools.com Creative Websoft Solutions | Web Development India | creativewebsoft.us Bulk SMS Providers India | bulksmsproviders.net Online cbse Guides | onlinecbseguides.com Big Kolkata Business Listing | bigkolkata.com Handicap Shaadi.com | handicapshaadi.com Handicap Marriage.com | handicapmarriage.com India Home Tutors.com | indiahometutors.com Indian Property Guides.com | indianpropertyguides.com Online Stocks Community.com | onlinestockscommunity.com Free Article Submission | ab-articles.com Secret Dating Online.com | secretdatingonline.com Private DatingOnline.com | privatedatingonline.com Indian businessguides.com | indianbusinessguides.com Software Engineer Delhi | Meenu Khanna Bollywood & Hot Images | hibdy.com Free Computer & Online PHP Training | php2php.com Free Ignou Project & Assignment | ignouinfo.com PHP Programmer India | Web Developer India | bageshsingh.com Creative Websoft Solutions | Web Development IndiaBlog Link Exchange http://www.php2php.com/tutorial-blog/http://bageshsingh.com/bagesh-blog/http://www.bhaktiguru.com/bhaktigurublog/http://www.hibdy.com/desi-masala-blog/