Joining Tables with INNER JOIN
2 mins read

Joining Tables with INNER JOIN

When working with databases, it is common to have multiple tables with related data. To retrieve data from multiple tables, we use the SQL JOIN statement. One of the most commonly used JOINs is the INNER JOIN. In this article, we’ll explore what INNER JOIN is and how to use it with examples.

What is INNER JOIN?

INNER JOIN is a type of JOIN that returns only the rows where there is a match in both tables being joined. If there’s no match, the row is not returned. This is useful when you want to combine rows from two or more tables and select records that have matching values in both tables.

How Does INNER JOIN Work?

The INNER JOIN keyword selects records that have matching values in both tables. Here’s a basic syntax of INNER JOIN:

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

Code Example:

Let’s consider two tables: Customers and Orders. We want to retrieve a list of customers and their corresponding order details. Here’s how the INNER JOIN can be implemented:

SELECT Customers.CustomerID, Customers.Name, Orders.OrderID, Orders.OrderDate
FROM Customers
INNER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID;

In this example, we are selecting the CustomerID and Name from the Customers table and OrderID and OrderDate from the Orders table. The INNER JOIN clause specifies that we only want rows where the CustomerID matches in both the Customers and Orders tables.

Things to Remember:

  • The INNER JOIN keyword is often interchangeable with JOIN.
  • INNER JOIN will filter out records that don’t have matching values in both tables.
  • Multiple INNER JOINS can be used to join more than two tables.
  • The ON clause specifies the matching column that links the tables.

INNER JOIN is a powerful way to extract related data from multiple tables. It ensures that you only get the rows where there’s a match found in two datasets. Using our example of Customers and Orders, INNER JOIN enables us to obtain a consolidated view of customer details along with their specific orders. Now that you’ve learned about INNER JOIN, you can apply this knowledge to effectively query your databases and bring your data relation skills to the next level.

Leave a Reply

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