Inner Join
(INNER) JOIN Select records that have matching values in both tables.
SQL INNER JOIN Syntax:
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
Example:
Lets use the “Customer” and “Orders” tables to perform an INNER JOIN based on CustomerID.
CustomerID | FirstName | LastName | Address | City | State | zip |
---|---|---|---|---|---|---|
1 | Rob | Johnson | 1 York Rd | New York | NY | 10013 |
2 | Jessica | Simmons | 2 High St | Maimi | FL | 33101 |
3 | Tracy | Baker | 3 River Dr | Chicago | IL | 60633 |
4 | Tod | Williams | 4 Cabin Ln | Denver | CO | 80210 |
5 | Tammy | Wilson | 5 Peach St | Seattle | WA | 98121 |
6 | Mike | Jones | 6 Rice Rd | Los Angeles | CA | 90009 |
OrderID | ProductID | EmployeeID | OrderDate | CustomerID |
---|---|---|---|---|
10 | 2 | 1 | 9/9/2009 | 2 |
11 | 3 | 2 | 1/9/2009 | 3 |
12 | 4 | 1 | 3/9/2009 | 4 |
13 | 1 | 3 | 9/9/2010 | 5 |
14 | 3 | 1 | 9/30/2009 | 3 |
15 | 5 | 2 | 6/12/2009 | 1 |
16 | 3 | 4 | 4/9/2009 | 6 |
SELECT
C.CustomerID,
C.FirstName,
C.LastName,
O.OrderID,
O.CustomerID
FROM Customers as C
INNER JOIN Orders as O
ON C.CustomerID=O.CustomerID
Results:
CustomerID | FirstName | LastName | OrderID | CustomerID |
---|---|---|---|---|
1 | Rob | Johnson | 15 | 1 |
2 | Jessica | Simmons | 10 | 2 |
3 | Tracy | Baker | 11 | 3 |
4 | Tod | Williams | 12 | 4 |
5 | Tammy | Wilson | 13 | 5 |
6 | Mike | Jones | 16 | 6 |
SQL Tutorial