An INNER JOIN in SQL is a method for combining rows from two or more tables based on a related column between them. The join condition is specified using the ON keyword, indicating which columns in each table should have matching values for the rows to be included in the result set.
Syntax
SELECT *
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;Example
Let’s consider two tables, employees and departments
Image scaled to 90%
Now, let’s perform an INNER JOIN to retrieve information about employees and their corresponding departments:
Database Exercise
Database Schema:
-- Database schema would be rendered hereExercise Script:
-- Exercise script would be rendered hereAvailable actions: Execute
Result
Image scaled to 50%
In this example, the INNER JOIN is based on the equality of the department_id columns in both tables. The result set includes only the rows where there is a match between the department_id values in the employees and departments tables. As a result, we get a cohesive view of employees and their respective departments.