5 min read โ€“ Mar 10, 2023

In SQL, the INNER JOIN keyword is used to combine rows from two or more tables based on a related column between them. The result of the JOIN operation is a new table, called a result set, that contains only the rows where the related column values match in both tables.

The INNER JOIN keyword is one of the most commonly used JOIN operations in SQL. It is used to retrieve data from related tables in a database, which allows for more complex queries and more efficient data retrieval.

In this article, we will explore the INNER JOIN keyword in SQL by using the Northwind database in SQL Server. We will explain the syntax of the INNER JOIN keyword and provide examples of how it can be used to retrieve data from related tables in the database. We will use examples from the Northwind database, a sample database that was created by Microsoft to showcase the features of SQL Server. It contains data for a fictional company which sells various products to customers around the world.

Using INNER JOIN to Retrieve Data from Related Tables

There are different syntaxes for writing an INNER JOIN statement in SQL. Here are 2 common ones:

Explicit JOIN syntax:

SELECT *
FROM Orders
INNER JOIN OrderDetails
ON Orders.OrderID = Orders.OrderID;

In this syntax, the tables being joined are listed after the FROM keyword, and the INNER JOIN keyword is used to specify that an inner join is being performed. The ON keyword is used to specify the join condition.

Implicit JOIN syntax:

SELECT *
FROM Orders o, OrderDetails od
WHERE o.OrderID = od.OrderID;

In this syntax, the tables being joined are listed after the FROM keyword, but instead of using the INNER JOIN keyword, a comma is used to separate the tables. The join condition is specified in the WHERE clause.

The USING Keyword:

SELECT *
FROM table1
INNER JOIN table2
USING (column);

In this syntax, the columns being used for the join condition are specified using the USING keyword. This can be useful when the join condition involves only one column that has the same name in both tables. If the columns have different names, the ON keyword must be used instead. Important NoteUSING is not supported SQL Server syntax, it is ANSI SQL syntax.

Explicit vs Implicit syntax: Which one to Use?

Explicit syntax, which uses the JOIN keyword and ON clause to specify the join condition, can be more readable and easier to understand, especially when joining multiple tables. Itโ€™s also more explicit in terms of how the tables are being joined, which can be helpful when debugging and troubleshooting errors.

On the other hand, implicit syntax, which uses a comma-separated list of tables in the FROM clause and specifies the join condition in the WHERE clause, can be faster to write and is often used for quick, ad-hoc queries.

However, itโ€™s worth noting that implicit syntax can be less clear and more error-prone, especially when joining multiple tables or when the join conditions are more complex.

In general, itโ€™s a good practice to use explicit syntax for more complex queries or queries that will be maintained over time, and to use implicit syntax for quick, one-time queries or for queries where the join condition is simple and clear.

INNER is the Default JOIN

When you use the JOIN keyword without specifying whether itโ€™s an INNER JOIN, LEFT JOIN, RIGHT JOIN, or FULL OUTER JOIN, it defaults to an INNER JOIN.

So, the following two queries are equivalent:

SELECT *
FROM table1
INNER JOIN table2 ON table1.column = table2.column;
SELECT *
FROM table1
JOIN table2 ON table1.column = table2.column;

In both cases, an INNER JOIN is being performed. However, itโ€™s good practice to explicitly specify the type of join being used for clarity and to avoid confusion, especially in complex queries.

Maximum Number of JOINs

There is no hard limit on the number of tables that can be joined using INNER JOIN, but it can become increasingly difficult to manage and understand as the number of tables increases.

In practice, itโ€™s common to join 2โ€“3 tables using INNER JOINs. If you need to join more than 3 tables, itโ€™s often better to break down the query into smaller subqueries, or use common tables expressions, and join them together using nested subqueries or temporary tables.

When joining multiple tables, itโ€™s important to use meaningful aliases for the tables to avoid confusion and make the query easier to read. Itโ€™s also important to pay attention to the relationships between the tables to ensure that the join conditions are correct and will return the desired results.

Suppose we have three tables: customersorders, and OrderDetails. The customers table contains information about customers, including their customer ID, name, and email. The orders table contains information about their orders, including the order ID, customer ID, and order date. The OrderDetailstable contains information about the items in each order, including the order ID, product ID, quantity, and unit price.

We can use INNER JOIN to combine these three tables based on their shared columns, which are the customer ID and order ID. The resulting table will contain all the information from all three tables, but only for the orders that have order details.

SELECT c.ContactName, 
o.OrderDate,
od.Quantity,
od.UnitPrice
FROM Customers c
INNER JOIN Orders o ON c.CustomerID = o.CustomerID
INNER JOIN OrderDetails od ON o.OrderID = od.OrderID;

This query will retrieve the customer name, order date, quantity, and unit price for each order that has order details in the OrderDetails table.

Conclusion

the INNER JOIN keyword is a powerful tool in SQL that allows you to combine data from two or more tables based on a common column.

When using INNER JOIN, itโ€™s important to specify the join condition accurately to avoid unexpected results. You can use various syntaxes to write an INNER JOIN statement, such as explicit JOIN syntax, or implicit JOIN syntax.

While the syntax used is largely a matter of personal preference and readability, itโ€™s important to use a clear and consistent style when writing SQL queries, especially for more complex queries that may need to be maintained over time.

Overall, INNER JOIN is a fundamental concept in SQL and is essential for anyone working with relational databases. By mastering INNER JOIN and other SQL join types, you can gain a deeper understanding of how databases work and how to manipulate and analyze large datasets.


Leave a Reply

Your email address will not be published. Required fields are marked *