SQL INNER JOIN

SQL INNER JOIN

This is a detailed tutorial of SQL INNER JOIN. Learn to select the common records in two tables on the basis of a common column with INNER JOIN.

Use of SQL INNER JOIN

SQL INNER JOIN is used to find records in two or more tables that have matching values for a particular column. In other words, INNER JOIN will select the records among tables that have matching values for selected columns. It is like the intersection of tables. In case we’re applying INNER JOIN on two tables, then the records so fetched will have the matching column value in both tables. This concept could be a bit tricky for beginners, but go through the examples and you’ll be all clear about it.

SQL INNER JOIN Representation

INNER JOIN Syntax

In the above syntax of INNER Join, we’re using a SQL SELECT Statement to fetch the common records from table_1 and table_2 on the basis of the matching column column_x that is present in both of the tables. Therefore, this will fetch all the records that are common in both tables and have the same value for column_x.

Examples

We’ll use two demo database tables to run the INNER JOIN query. These two tables are named employees and salaries. One table contains the employee data and the salaries table includes their monthly salaries. The two tables have the column emp_no in common, which will be used for INNER JOINING these tables.

Have a look at both of these tables and observe their values carefully.

Employees Table
Database Table employees
Salaries Table
Database Table salaries

In the second table salaries, I’ve deleted the entries with emp_no 10001 and 10003 for a purpose, which I’ll tell you after executing the INNER JOIN query.

JOINING Two Tables With INNER JOIN

The following query will fetch the emp_no, first_name, last_name and salary of the employees in different durations.

Note. In the SELECT statement, I’ve specified the column names along with the table names. You may or may not require to do so. It depends upon the uniqueness of the columns in the two tables. If all the columns in both the tables have different spellings and are unique, you need not define the column name(s) with the table names, but if a column has the same name in both the tables, you must specify that column name explicitly with the table name.

As in our case, the column names in both of the tables are unique except the joining column emp_no, we can select the columns without defining the table names as written in the following query.

Even the above query as the emp_no column is in both tables, so we’ve to explicitly define the column name as written the above query.

Now, observe the screenshot of the result set of this query carefully.

SQL INNER JOIN Example

You can clearly see the output contains the salary and employee data from both the tables. But it does not contain the records with emp_no 10001 and 10003 because these entries are not presented in the second table named salaries. So, that’s the reason why I deleted those entries from the salaries table. Therefore, only those records will be fetched for which the JOINING column has matching values in both the tables.

INNER Joining Three Tables

You can JOIN as many tables as you want to with INNER JOIN. The following query is joining three different tables with a common column emp_no.

I hope you find this guide useful. If so, do share it with others who are willing to learn SQL. If you have any questions related to this article, feel free to ask them in the comments section.

Also, don’t forget to Subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *