SQL Inner Join
The INNER JOIN keyword in SQL is used to retrieve rows from two or more tables based on a related column between them. It returns only the rows where there is a match in both tables.
Syntax
The basic syntax for an INNER JOIN is:
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
Example
Consider two tables: Employees
and Departments
. To retrieve a list of employees along with their respective department names, you can use the following query:
SELECT Employees.name, Departments.department_name
FROM Employees
INNER JOIN Departments
ON Employees.department_id = Departments.id;
Explanation
- SELECT columns: Specifies the columns to be retrieved.
- FROM table1: Indicates the primary table from which to retrieve data.
- INNER JOIN table2: Specifies the second table to join with the primary table.
- ON table1.column = table2.column: Defines the condition for the join, i.e., the common column between the two tables.