SQL Joins (JOIN)
SQL join is used to combine rows from two or more tables.
The following diagram shows seven uses related to LEFT JOIN, RIGHT JOIN, INNER JOIN, and OUTER JOIN.
| Type | Description |
|---|---|
| INNER JOIN | Returns records from both tables that satisfy the join condition (intersection). |
| LEFT JOIN | Returns all records from the left table, even if there is no matching record in the right table (retains the left table). |
| RIGHT JOIN | Returns all records from the right table, even if there is no matching record in the left table (retains the right table). |
| FULL OUTER JOIN | Returns the union of two tables, including matched and unmatched records. |
| CROSS JOIN | Returns the Cartesian product of two tables, combining each left-table record with each right-table record. |
| SELF JOIN | Joins a table with itself. |
| NATURAL JOIN | A table joined automatically based on fields with the same name. |
Sample data
Table 1: Customers
| CustomerID | Name |
|---|---|
| 1 | Alice |
| 2 | Bob |
| 3 | Charlie |
Table 2: Orders
| OrderID | CustomerID | Product |
|---|---|---|
| 101 | 1 | Laptop |
| 102 | 2 | Phone |
| 103 | 4 | Tablet |
Comparison of Join Results
| JOIN Type | Result |
|---|---|
| INNER JOIN | Returns matching records from both tables. In the given example, only records with CustomerID 1 and 2 have matches in both tables, so only these records are returned. |
| LEFT JOIN | Returns all records from the left table (Customers), even if there is no matching record in the right table (Orders). For left-table records with no right-table match, the right-table fields in the result will be NULL. In the example, the record with CustomerID 3 has no match in the right table, so its corresponding Product will be NULL. |
| RIGHT JOIN | Returns all records from the right table (Orders), even if there is no matching record in the left table (Customers). For right-table records with no left-table match, the left-table fields in the result will be NULL. In the example, the record with OrderID 103 has no match in the left table, so its corresponding Name will be NULL. |
| FULL OUTER JOIN | Returns all records from both tables, regardless of whether they match. If a table has no matching record, that table's fields will be NULL. In the example, the record with CustomerID 3 and the record with OrderID 103 will appear as NULL in the other table, respectively. |
| CROSS JOIN | Returns the Cartesian product of two tables, that is, each row in the left table combined with each row in the right table. In the example, each customer is combined with each order, producing multiple results. |
| SELF JOIN | A table joined with itself. This is typically used to query interrelated records in a table, such as the relationship between an employee and their manager. |
SQL JOIN
The SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them.
The most common JOIN types:SQL INNER JOIN (Simple JOIN). SQL INNER JOIN returns all rows from multiple tables that satisfy the JOIN condition.
Syntax:
SELECT column1, column2, ...
FROM table1
JOIN table2 ON condition;
Parameter Description:
- column1, column2, ...: The names of the fields to select; there can be multiple fields. If no field names are specified, all fields are selected.
- table1: The first table to join.
- table2: The second table to join.
- condition: The join condition, used to specify how to join.
Demonstration Database
In this tutorial, we will use the RUNOOB sample database.
Below is the data selected from the "Websites" table:
+----+--------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | 淘宝 | https://www.taobao.com/ | 13 | CN |
| 3 | Runoob Tutorial | http://www.runoob.com/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
| 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND |
+----+---------------+---------------------------+-------+---------+
Below is the data from the "access_log" website access log table:
mysql> SELECT * FROM access_log;
+-----+---------+-------+------------+
| aid | site_id | count | date |
+-----+---------+-------+------------+
| 1 | 1 | 45 | 2016-05-10 |
| 2 | 3 | 100 | 2016-05-13 |
| 3 | 1 | 230 | 2016-05-14 |
| 4 | 2 | 10 | 2016-05-14 |
| 5 | 5 | 205 | 2016-05-14 |
| 6 | 4 | 13 | 2016-05-15 |
| 7 | 3 | 220 | 2016-05-15 |
| 8 | 5 | 545 | 2016-05-16 |
| 9 | 3 | 201 | 2016-05-17 |
+-----+---------+-------+------------+
9 rows in set (0.00 sec)
Please note that the "id" column points to the field " in the "access_log" tablesite_id". The two tables above are linked by the "site_id" column.
Then, if we run the following SQL statement (with INNER JOIN):
Example
SELECT Websites.id, Websites.name, access_log.count, access_log.date
FROM Websites
INNER JOIN access_log
ON Websites.id=access_log.site_id;
The result of executing the above SQL is as follows:
Different SQL JOINs
Before we continue with the examples, let's list the different SQL JOIN types you can use:
- INNER JOIN: Return rows if there is at least one match in the tables
- LEFT JOIN: Return all rows from the left table even if there is no match in the right table
- RIGHT JOIN: Return all rows from the right table even if there is no match in the left table
- FULL JOIN: Return rows whenever there is a match in either table
