How can we write MySQL query for inner joins with the help of keyword JOIN?


MySQL query for inner joins can also be written with the help of keyword JOIN. To understand it, we are taking the example of two tables named tbl_1 and tbl_2 which are having following data:

mysql> Select * from tbl_1;
+----+--------+
| Id | Name   |
+----+--------+
| 1  | Gaurav |
| 2  | Rahul  |
| 3  | Raman  |
| 4  | Aarav  |
+----+--------+
4 rows in set (0.00 sec)

mysql> Select * from tbl_2;
+----+---------+
| Id | Name    |
+----+---------+
| A  | Aarav   |
| B  | Mohan   |
| C  | Jai     |
| D  | Harshit |
+----+---------+
4 rows in set (0.00 sec)

Now, the query below will use keyword JOIN to inner join the tables:

mysql> Select tbl_1.id,tbl_2.id FROM tbl_1 JOIN tbl_2 ON tbl_1.name = tbl_2.name;
+----+----+
| id | id |
+----+----+
| 4  | A  |
+----+----+
1 row in set (0.00 sec)

In the query above the join-predicate i.e., the conditions are written after ON keyword.

Updated on: 07-Feb-2020

59 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements