How can we write MySQL query for inner joins with the help of Comma operator?


Writing inner joins with the help of comma operator is the most basic way to combine two tables. As we know that we can also write inner join by using keyword INNER JOIN or synonyms like JOIN. To form an inner join we need to specify a particular condition which is known as join-predicate and while writing inner joins using the comma operator, we use WHERE clause, the only way, to specify the join condition. 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 inner join the above-mentioned tables with comma operator:

mysql> Select * FROM tbl_1,tbl_2 WHERE tbl_1.name = tbl_2.name;
+----+-------+----+-------+
| Id | Name  | Id | Name  |
+----+-------+----+-------+
| 4  | Aarav | A  | Aarav |
+----+-------+----+-------+
1 row in set (0.00 sec)

Updated on: 07-Feb-2020

106 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements