SQL Joins
SQL INNER JOIN
Joining Tables with INNER JOIN
INNER JOIN combines tables using the ON clause for matching rows.
What is SQL INNER JOIN?
The SQL INNER JOIN keyword is used to select records that have matching values in both tables. It is the most common type of join and is used when you need to retrieve data from two or more tables that match certain criteria. The INNER JOIN keyword joins the tables based on a related column between them.
How Does INNER JOIN Work?
When you use an INNER JOIN, SQL returns rows where there is a match between the columns in both tables. If no match is found, the row is not returned in the result set. This operation is performed using the ON
clause, which specifies the condition for the join.
Consider the following two tables: Employees and Departments.
To retrieve employee names along with their department names, you can use the following INNER JOIN statement:
This query will return:
- Alice, HR
- Bob, IT
- Charlie, HR
Note that only employees with a matching DepartmentID
in the Departments table are returned.
Advantages of Using INNER JOIN
- Data Integrity: Ensures that only related and matching data from tables are combined, which maintains the integrity of the dataset.
- Performance: INNER JOINs can be optimized by databases, leading to faster query performance compared to other types of joins.
- Simplicity: Provides a straightforward way to retrieve related data from multiple tables without the complexity of outer joins.
SQL Joins
- INNER JOIN
- LEFT JOIN
- RIGHT JOIN
- FULL JOIN
- CROSS JOIN
- Self Join