Difference Between Inner Join and Left Join

What’s the difference between inner join and left join in MySql?  In relational databases (Such as MySql etc.) concept of join is very important. In this tutorial, I am going to explain inner join and left join with examples.

MySql Interview Questions

MySql Joins?

Joins are used when we need to retrieve data from multiple tables. In relational databases, We need to retrieve data from more than one tables in that case joins is very helpful.

When we use Joins in MySql query then it matches rows in one table with rows in another table based on join-predicate (or condition) and gives you the output that contains columns from both or either tables.

I hope you understand the purpose of join and their usage. Now let’s understand the difference between inner join and left join.

How to find second highest salary in MySql

Difference Between Inner Join and Left Join

To understand the concept of inner join and left join, let’s first create two tables (emp_data and emp_salary). Let’s insert some dummy values.

The first table is created successfully. Now, let’s create a second table emp_salary.

So both tables are created successfully.

Inner Join in MySql

When we use inner join in a query, it selects all rows from both the tables which are common (or match) based upon the join-predicate (or condition).

How to connect MySql database using PHP

Inner join of table A and B gives all the record that is common in both the tables.

Inner Join in MySql

Difference between Inner Join and Left Join



Output

We get common rows between two tables as an output. JOIN is a shorter version of INNER JOIN. There is no difference between join and inner join keyword, they both are equal in performance as well as implementation.

LEFT JOIN in MySql

Left join for tables A and B, gives all records of the left table (which is A) even if the condition does not find any matching record in a second table (which is B). The result is NULL for the right table if there is no match.

Left Join in MySql

Difference Between Inner Join and Left Join



output –

Let’s look at the output, there is no record for id 2 and 3 (smith and Amit) in the emp_salary table. In that case the NULL value is returned.

How to compare NULL value in MySql

Conclusion

I hope after reading this tutorial, you understand the difference between inner join and left join. If you want to add something, please let us know by your comments.

Tagged , , , . Bookmark the permalink.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.