Mastering SQL Inner Join: A Comprehensive Guide With Practical Examples

SQL (Structured Query Language) is the standard language for managing relational databases. It is a powerful tool that allows us to store, manipulate, and retrieve data efficiently. One of the most commonly used operations in SQL is the JOIN operation, which allows us to combine data from two or more tables into a single result set.

The INNER JOIN clause is one of the most frequently used types of JOIN in SQL. It combines rows from two or more tables based on a related column between them. The INNER JOIN returns only the rows that have matching values in both tables, i.e., the intersection of the two tables. 

The following picture illustrates the Venn diagram of INNER JOIN. The shaded area in the diagram represents the common values shared between the two tables.

sql-inner-join
SQL Inner Join

In this article, We will discuss the SQL INNER JOIN clause in detail with multiple examples.

SQL INNER JOIN syntax

The basic syntax of the SQL Inner Join clause is as follows:

SELECT column_name(s)
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name;

In the above syntax, the SELECT statement specifies the columns we want to retrieve from the tables. The FROM statement is used to specify the tables from which we want to retrieve data.
The INNER JOIN clause combines the rows from the specified tables based on the matching column(s) between them.

Suppose we have two tables:

  •  Employees
  •  Departments

The Employees table contains the following columns: ‘EmployeeID’, ‘FirstName’, ‘LastName’, and ‘DepartmentID’. The Departments table contains the following columns: ‘DepartmentID’, ‘DepartmentName’.

Employees Table:

EmployeeIDFirstNameLastNameDepartmentID
1ShekhAli1
2RakeshSingh2
3RomanChug1
4AliceJohnson3
5CharlieBrown2

Departments Table:

DepartmentIDDepartmentNameProjectID
1Sales1
2Marketing2
3Finance3

The below query joins two tables: Employees and Departments, using the INNER JOIN clause. The ON clause ensures that only the rows with matching DepartmentIDs in both tables will be included in the result set.

The SELECT statement selects three columns: FirstName and LastName from the Employees table and DepartmentName from the Departments table.

SELECT Employees.FirstName, Employees.LastName, Departments.DepartmentName
FROM Employees
INNER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;

Result:

FirstNameLastNameDepartmentName
ShekhAliSales
RakeshSinghMarketing
RomanChugSales
AliceJohnsonFinance
CharlieBrownMarketing

SQL INNER JOIN With WHERE Clause

Suppose we want to retrieve the names of all employees who work in the ‘Sales’ department. We can use the following query:

SELECT Employees.FirstName, Employees.LastName, Departments.DepartmentName
FROM Employees
INNER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID
WHERE Departments.DepartmentName = 'Sales';

The above query joins the Employees and Departments tables using the INNER JOIN clause on the ‘DepartmentID’ column. It then uses the WHERE clause to filter the results to include only those rows where the ‘DepartmentName’ is ‘Sales’.

The output of this query will be:

FirstNameLastNameDepartmentName
ShekhAliSales
RomanChugSales

INNER JOIN With GROUP BY Clause

Suppose we want to retrieve the total number of employees in each department. We can use the following query:

SELECT Departments.DepartmentName, COUNT(*) as TotalEmployees
FROM Employees
INNER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID
GROUP BY Departments.DepartmentName;

The above query joins the Employees and Departments tables using the INNER JOIN clause on the DepartmentID column. It then uses the GROUP BY clause to group the results by the DepartmentName column. Finally, it uses the COUNT(*) function to count the total number of employees in each department.

The output of this query will be:

DepartmentNameTotalEmployees
Sales2
Marketing2
Finance1

Using SQL INNER JOIN With 3 Tables

Suppose we have three tables: Students, Courses, and Enrollments. 

The Students table contains information about students, including their student IDs and names. 

The Courses table contains course information, including the course IDs and names. 

The Enrollments table contains information about which students are enrolled in which courses, including the student IDs and course IDs.

Let’s first create these tables:

Students:

StudentIDName
1Krishna
2Sweta
3Charlie
4David

Courses:

CourseIDCourseName
1Math
2Science
3History
4English

Enrollments:

StudentIDCourseID
11
12
22
23
31
33

To retrieve a list of all students and the courses they are enrolled in, we can use the following query:

SELECT Students.StudentID, Students.Name, Courses.CourseName
FROM Students
INNER JOIN Enrollments ON Students.StudentID = Enrollments.StudentID
INNER JOIN Courses ON Enrollments.CourseID = Courses.CourseID;

The output of the above query will be:

StudentIDNameCourseName
1KrishnaMath
1KrishnaScience
2SwetaScience
2SwetaHistory
3CharlieMath
3CharlieHistory

This table shows all the students and the courses they are enrolled in. For example, Krishna is enrolled in Math and Science, Sweta is in Science and History, and Charlie is in Math and History.

Using Aliases with SQL INNER JOIN

Aliases provide a way to give a table or column a different name to make the SQL statement easier to read and understand. Here is an example of using aliases with SQL INNER JOIN:

SELECT emp.FirstName, emp.LastName, dep.DepartmentName
FROM Employees AS emp
INNER JOIN Departments AS dep ON emp.DepartmentID = dep.DepartmentID;

Using Subqueries with SQL INNER JOIN 

A subquery is a query that is nested inside another query. We can use SQL INNER JOIN with subqueries to perform more complex data. 

Here is an example of using a subquery with SQL INNER JOIN:

SELECT emp.FirstName, emp.LastName, dep.DepartmentName
FROM Employees AS emp
INNER JOIN (SELECT * FROM Departments WHERE DepartmentID IN (1, 2, 3)) AS dep ON emp.DepartmentID = dep.DepartmentID;

This query selects only the employees who work in the departments with IDs 1, 2, or 3.

Using UNION with SQL INNER JOIN 

UNION combines the result sets of two or more SELECT statements into a single result set. SQL INNER JOIN can be used with UNION to combine data from multiple tables. 

Here is an example of using UNION with SQL INNER JOIN:

SELECT FirstName, LastName, DepartmentName FROM Employees
INNER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID
UNION
SELECT FirstName, LastName, 'No Department' AS DepartmentName FROM Employees
WHERE DepartmentID IS NULL;

This query selects all employees and their department names and includes employees who are not assigned to any department with a default “No Department” value.

Using CROSS JOIN with SQL INNER JOIN

CROSS JOIN returns the Cartesian product of two tables, which returns all possible combinations of rows from both tables. SQL INNER JOIN can be used with CROSS JOIN to join all rows from one table to all rows in another table. 

Here is an example of using CROSS JOIN with SQL INNER JOIN:

SELECT emp.FirstName, emp.LastName, dep.DepartmentName, ord.OrderDate
FROM Employees AS emp
CROSS JOIN Departments AS dep
INNER JOIN Orders AS ord ON emp.EmployeeID = ord.EmployeeID AND dep.DepartmentID = ord.DepartmentID;

FAQs

Q: What is the difference between INNER JOIN and OUTER JOIN?

INNER JOIN returns only the matching rows between two tables based on a specified condition, while OUTER JOIN returns all rows from one table and matching rows from the other table(s) and fills in NULL values for non-matching rows.

Q: Can INNER JOIN be used to join more than two tables?

Yes, INNER JOIN can be used to join multiple tables in a single query. The syntax would involve multiple JOIN statements with appropriate conditions.

Q: What happens if the condition in the INNER JOIN clause is not satisfied by any row in either table?

If the condition in the INNER JOIN clause is not satisfied by any row in either table, then no rows will be returned in the result set.

Q: Can you use non-equality operators such as <, >, <=, or >= in the INNER JOIN condition?

Yes, you can use non-equality operators in the INNER JOIN condition as long as they make sense for the data types being compared.

Q: What is the performance impact of using INNER JOIN on large tables?

The performance impact of using INNER JOIN on large tables depends on various factors, such as the size of the tables, the indexing, and the server resources. Generally, INNER JOIN can be more efficient than using subqueries or temporary tables, but optimizing the queries and indexing for the best performance is important.

Q: Can you join tables from different databases or servers in INNER JOIN?

Yes, you can use INNER JOIN to join tables from different databases or servers as long as you have appropriate permissions and the necessary connectivity. You may need to prefix the table names with the database or server name in the query.

What is SQL Inner Join?

SQL Inner Join is a join in relational database management systems that combines data from two or more tables based on a common column. Inner join returns only the rows from both tables that have matching values in the specified columns and filters out any rows that do not have a matching value in both tables.

You might want to read this too:

Shekh Ali
5 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments