INNER JOIN is a type of join that returns records from two tables where there is a match between the specified columns. It is the most common type of join used in SQL.
SELECT column1, column2, ...
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name;
In this syntax, table1 and table2 are the tables you want to join, and column_name is the column that relates the two tables.
Here’s an example of using INNER JOIN to retrieve data from two related tables:
SELECT Employees.FirstName, Employees.LastName, Departments.DepartmentName
FROM Employees
INNER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
This query selects the FirstName and LastName of employees along with their DepartmentName, where the DepartmentID matches in both tables.
INNER JOIN is useful in various scenarios, such as:
Write your own INNER JOIN queries using the example tables Employees and Departments. Try adding different columns to your SELECT statement!