INNER

INNER JOIN is a type of join operation in SQL used to combine rows from two or more tables based on related columns between them. Only the matching rows between the tables are returned.

Example

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

Output

+-----------------+---------+
| CustomerName | OrderID |
+-----------------+---------+
| Alfreds | 10308 |
| Ana Trujillo | 10365 |
| Antonio Moreno | 10355 |
| Antonio Moreno | 10383 |
| Around the Horn | 10355 |
| Berglunds snabbköp | 10384 |
+-----------------+---------+

Explanation

The SQL INNER JOIN keyword selects records that have matching values in both tables. In the example provided, it retrieves a list of customer names and their corresponding order IDs from the ‘Customers’ and ‘Orders’ tables where the ‘CustomerID’ in both tables are the same.

Example

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

Output

| CustomerName | OrderID |
|--------------|---------|
| Alfreds | 10308 |
| Ana Trujillo | 10309 |
| Antonio | 10310 |

Explanation

The INNER JOIN keyword in SQL is used to combine rows from two or more tables, based on a related column between them. In this case, it is combining the ‘Customers’ and ‘Orders’ tables where the ‘CustomerID’ matches in both.

Example

SELECT Orders.OrderId, Customers.CustomerName
FROM Orders
INNER JOIN Customers
ON Orders.CustomerId = Customers.CustomerId;

Output

OrderId | CustomerName
---------|-----------------
101 | John Doe
102 | Jane Smith
103 | Michael Johnson

Explanation

This example uses the INNER JOIN clause to retrieve records that have matching values in both the Orders and Customers tables. The ON section specifies the fields that should be matched between these two tables. The result is a list of OrderIds along with their respective CustomerNames.

Example

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

Output

| OrderID | CustomerName |
|---------|--------------|
| 10308 | Ana Trujillo |
| 10365 | Antonio Moreno|
| 10383 | Around the Horn|
|... | ... |

Explanation

The INNER JOIN keyword selects records that have matching values in both tables, ‘Orders’ and ‘Customers’. This query returns a table that contains the ‘OrderID’ from the ‘Orders’ table and ‘CustomerName’ from the ‘Customers’ table for all orders where the customer ID values in the ‘Orders’ and ‘Customers’ tables are the same.

Example

CREATE TABLE Orders (
OrderID int,
CustomerID int,
OrderDate date
);
INSERT INTO Orders (OrderID, CustomerID, OrderDate)
VALUES (1, 3, '2017-11-17'),
(2, 1, '2018-10-23'),
(3, 2, '2020-06-19');
CREATE TABLE Customers (
CustomerID int,
CustomerName string
);
INSERT INTO Customers (CustomerID, CustomerName)
VALUES (1, 'Google'),
(2, 'Microsoft'),
(3, 'Apple');
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;

Output

OrderID | CustomerName | OrderDate
--------|--------------|------------
1 | Apple | 2017-11-17
2 | Google | 2018-10-23
3 | Microsoft | 2020-06-19

Explanation

The example provided demonstrates an INNER JOIN operation between two tables, Orders and Customers. The INNER JOIN keyword selects records that have matching values in both tables. The matching criterion here is the CustomerID. As a result, the output contains the OrderID, CustomerName, and OrderDate for only those orders for which a matching customer record exists.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.