MySQL – Inner Join
MySQL Inner Join hides other rows and columns from the results and returns only those that match the specified condition. There is no need to specify the Inner Join keyword with the query since MySQL assumes it as a default join.
In order to understand it, we can look at the following visual representation showing only the matching results from table 1 and table 2.
Syntax:
SELECT columns FROM table1 INNER JOIN table2 ON condition1 INNER JOIN table3 ON condition2 ...;
Using this syntax, we must first select the column list, then specify which table will be joined to the main table, appear in the Inner Join (table1, table2), and finally provide the condition after the ON keyword. In the Join condition, the matching rows between the two tables specified in the Inner clause are returned.
Example
The first step will be to create two tables, “employees” and “dept_emp,” containing the following information:
employees
dept_emp:
SELECT employees.last_name, employees.first_name FROM employees INNER JOIN dept_emp ON employees.emp_no = dept_emp.emp_no;
MySQL Inner Join using Operators
Inner joins in MySQL are compatible with many operators, including greater than (>), less than (<), equal (=), not equal (=), etc.
Example:
SELECT first_name, last_name, salary FROM employees INNER JOIN dept_emp WHERE salary>1000 and salary<2000;