INNER JOIN
SQL INNER JOIN: Comprehensive Explanation
The INNER JOIN is one of the most commonly used join types in SQL. It retrieves records that have matching values in both tables being joined. Essentially, it creates a new result table by combining columns from both tables based on a related column (called the "join condition").
Syntax
table1andtable2: The tables you are joining.ON: Specifies the condition to match rows fromtable1andtable2.column_name: The column(s) used to establish the relationship between the two tables.
How it Works
Only rows that satisfy the
ONcondition are included in the result.If no match is found, those rows are excluded from the output.
Example Scenario
Suppose we have two tables:
1. Customers Table
CustomerID | Name | Country |
|---|---|---|
1 | Alice | USA |
2 | Bob | Canada |
3 | Charlie | UK |
2. Orders Table
OrderID | CustomerID | Product |
|---|---|---|
101 | 1 | Laptop |
102 | 2 | Tablet |
103 | 1 | Smartphone |
104 | 4 | Monitor |
Example 1: Joining Two Tables
Query to find all customers who placed orders:
Result:
CustomerID | Name | Product |
|---|---|---|
1 | Alice | Laptop |
1 | Alice | Smartphone |
2 | Bob | Tablet |
Explanation:
Only rows with matching
CustomerIDin both tables are included.The row for
CustomerID = 3fromCustomersis excluded because Charlie hasn't placed any orders.The row for
CustomerID = 4inOrdersis excluded because no matching customer exists.
Example 2: Using Aliases for Better Readability
You can use table aliases to simplify the query:
This produces the same result as above.
Example 3: Filtering with INNER JOIN
Add a WHERE clause to filter the results further. For instance, find orders placed by customers from the USA:
Result:
Name | Product |
|---|---|
Alice | Laptop |
Alice | Smartphone |
Example 4: Multiple Conditions in the JOIN
If you need to join tables on more than one condition:
This query ensures that only orders placed by customers from the USA are joined.
Example 5: INNER JOIN on Multiple Tables
You can join more than two tables in one query. Suppose we have another table, Products:
Products Table
ProductID | Product | Price |
|---|---|---|
1 | Laptop | 1000 |
2 | Smartphone | 700 |
3 | Tablet | 500 |
Join all three tables:
Result:
Name | Product | Price |
|---|---|---|
Alice | Laptop | 1000 |
Alice | Smartphone | 700 |
Bob | Tablet | 500 |
Key Points
Default Behavior: If no matching rows exist in either table, those rows are excluded from the result.
Efficiency:
INNER JOINcan be optimized using indexes on the join columns.Aliases: Use table aliases for readability in queries involving multiple tables.