Junior
What is INNER JOIN in SQL?
sobes.tech AI
Answer from AI
INNER JOIN combines rows from two or more tables based on a related column between them. Only rows with matching values in the specified columns of both tables are included in the result set.
Visually, this can be represented as the intersection of two sets.
Syntax:
SELECT column_name(s)
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name;
Example:
Suppose there are two tables: Customers and Orders.
Table Customers:
| CustomerID | CustomerName |
|---|---|
| 1 | Ivan |
| 2 | Maria |
| 3 | Petr |
Table Orders:
| OrderID | CustomerID | OrderDate |
|---|---|---|
| 101 | 1 | 2023-10-26 |
| 102 | 2 | 2023-10-26 |
| 103 | 1 | 2023-10-27 |
| 104 | 4 | 2023-10-27 |
INNER JOIN query:
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
Result:
| CustomerName | OrderID |
|---|---|
| Ivan | 101 |
| Maria | 102 |
| Ivan | 103 |
The row with CustomerID 3 from the Customers table did not appear in the result because there are no corresponding orders in the Orders table. The row with OrderID 104 from the Orders table did not appear because there is no matching customer in the Customers table.
INNER JOIN is the most common type of JOIN.