MySQL Tutorial
The INNER JOIN keyword selects records that have matching values in both tables.
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
We've a table named Students and Teachers in our database that contains the following records:
ROLL_NO | STU_NAME | SUBJECT |
---|---|---|
1 | Will | C++ |
2 | SAM | Python |
3 | Rani | Node.js | 4 | Rim | Java |
5 | Micheal | JAVA |
6 | Lara | PHP |
TECH_NO | TECH_NAME | SUBJECT | ROLL_NO |
---|---|---|---|
20 | Kayla | PHP | 6 |
21 | Albert | Java | 4 |
21 | Albert | Java | 5 |
22 | Helen | Node.js | 3 | 23 | Anne | Python | 2 |
24 | Jaime | C++ | 1 |
Then, we can create the following MySQL statement (that contains an INNER JOIN), that selects records that have matching values in both tables:
SELECT Teachers.TECH_NO, Teachers.NAME, Students.ROLL_NO
FROM Teachers
INNER JOIN Students ON Teachers.TECH_NO=Students.ROLL_NO;
Now, after executing the above MySQL statement,
TECH_NO | NAME | ROLL_NO |
---|---|---|
20 | Kayla | 6 |
21 | Albert | 2 |
21 | Albert | 5 |
22 | Helen | 3 | 23 | Anne | 2 |
24 | Jaime | 1 |