SQL Joins: Main Tips
- SQL joins are used to combine more than one tables rows.
- SQL INNER JOIN is the most common JOIN. An SQL INNER JOIN returns all multiple tables rows where the join condition is met.
SQL Join: Types
- LEFT JOIN: It returns the left table rows and right table rows which matched.
- RIGHT JOIN: It returns the right table rows, and left table rows which matched.
- INNER JOIN: It returns all rows when there is the same record in BOTH tables.
- FULL OUTER JOIN: It returns all rows when the same record occurs in BOTH tables.
Demo Database
This is a demo example from the "Orders" table in the database:
| ID | Customer_ID | Employee_ID |
|---|---|---|
| 20408 | 2 | 7 |
| 20409 | 2 | 5 |
| 85471 | 1 | 3 |
| 75864 | 5 | 8 |
This is a demo example from the "Developers" table in the database:
| ID | Name | City | Country |
|---|---|---|---|
| 1 | Tom Kurkutis | New York | USA |
| 2 | Ana Fernandez | London | UK |
| 3 | Antonio Indigo | Paris | France |
| 4 | Aarav Kaelin | Delhi | India |
| 5 | Andrew Tumota | Miami | USA |
INNER JOIN: Example
Example
SELECT Customer_orders.ID, Developers.Name
FROM Customer_orders
INNER JOIN Developers ON Customer_orders.employee_id = Developers.ID;
The result table looks like this:
| ID | Name | Date |
|---|---|---|
| 1509 | Tom Kurkutis | 8/18/2017 |
| 1510 | Andrew Tumota | 12/19/2016 |
| 1511 | Andrew Tumota | 01/25/2017 |