SQL INNER JOIN

What is INNER JOIN?

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.

Syntax of INNER JOIN

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.

INNER JOIN Example

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.

Use Cases of INNER JOIN

INNER JOIN is useful in various scenarios, such as:

  • Combining data from different tables to create comprehensive reports.
  • Fetching data from related entities, such as customers and their orders.
  • Analyzing relationships between datasets to derive insights.

Practice Exercise

Write your own INNER JOIN queries using the example tables Employees and Departments. Try adding different columns to your SELECT statement!