INNER
Example
Section titled “Example”SELECT Customers.CustomerName, Orders.OrderIDFROM CustomersINNER JOIN OrdersON Customers.CustomerID=Orders.CustomerID;Output
Section titled “Output”+-----------------+---------+| CustomerName | OrderID |+-----------------+---------+| Alfreds | 10308 || Ana Trujillo | 10365 || Antonio Moreno | 10355 || Antonio Moreno | 10383 || Around the Horn | 10355 || Berglunds snabbköp | 10384 |+-----------------+---------+Explanation
Section titled “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
Section titled “Example”SELECT Customers.CustomerName, Orders.OrderIDFROM CustomersINNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;Output
Section titled “Output”| CustomerName | OrderID ||--------------|---------|| Alfreds | 10308 || Ana Trujillo | 10309 || Antonio | 10310 |Explanation
Section titled “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
Section titled “Example”SELECT Orders.OrderId, Customers.CustomerNameFROM OrdersINNER JOIN CustomersON Orders.CustomerId = Customers.CustomerId;Output
Section titled “Output”OrderId | CustomerName---------|-----------------101 | John Doe102 | Jane Smith103 | Michael JohnsonExplanation
Section titled “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
Section titled “Example”SELECT Orders.OrderID, Customers.CustomerNameFROM OrdersINNER JOIN CustomersON Orders.CustomerID=Customers.CustomerID;Output
Section titled “Output”| OrderID | CustomerName ||---------|--------------|| 10308 | Ana Trujillo || 10365 | Antonio Moreno|| 10383 | Around the Horn||... | ... |Explanation
Section titled “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
Section titled “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.OrderDateFROM OrdersINNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;Output
Section titled “Output”OrderID | CustomerName | OrderDate--------|--------------|------------1 | Apple | 2017-11-172 | Google | 2018-10-233 | Microsoft | 2020-06-19Explanation
Section titled “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.