-
-
-
- |
-
- Retrieving all data from the left table joined with corresponding data from the right table:
- ```sql
- SELECT table_fields
- FROM left_table LEFT JOIN right_table
- ON right_table.key = left_table.key
- ```
- |
-
-
-
-
- |
-
- Retrieving all data from the right table joined with corresponding data from the left table:
- ```sql
- SELECT table_fields
- FROM left_table RIGHT JOIN right_table
- ON right_table.key = left_table.key
- ```
- |
-
-
-
-
- |
-
- Retrieving data that only belongs to the left table:
- ```sql
- SELECT table_fields
- FROM left_table LEFT JOIN right_table
- ON right_table.key = left_table.key
- WHERE right_table.key IS NULL
- ```
- |
-
-
-
-
- |
-
- Retrieving data that only belongs to the right table:
- ```sql
- SELECT table_fields
- FROM left_table RIGHT JOIN right_table
- ON right_table.key = left_table.key
- WHERE left_table.key IS NULL
- ```
- |
-
-
-
-
- |
-
- Retrieving data that belongs to both the left and right tables:
- ```sql
- SELECT table_fields
- FROM left_table INNER JOIN right_table
- ON right_table.key = left_table.key
- ```
- |
-
-
-
-
- |
-
- Retrieving all data that belongs to both the left and right tables, as well as their inner join:
-
-
+**PostgreSQL**
```sql
-SELECT table_fields
-FROM left_table
- FULL OUTER JOIN right_table
- ON right_table.key = left_table.key
+SELECT Timepair.id "timepair.id", start_pair, end_pair,
+ Schedule.id "schedule.id", date, class, number_pair, teacher, subject, classroom
+FROM Timepair
+ FULL OUTER JOIN Schedule ON Schedule.number_pair = Timepair.id;
```
-
+On the data of this database the result matches the left join — the same 43 rows: here the unmatched rows exist only on the left.
-
+**MySQL**
+
+MySQL does not support `FULL OUTER JOIN`, but the same result can be assembled by hand: take the left join and add the rows of the right table that found no match.
```sql
SELECT table_fields
FROM left_table
-LEFT JOIN right_table
- ON right_table.key = left_table.key
+ LEFT JOIN right_table ON right_table.key = left_table.key
UNION ALL
SELECT table_fields
FROM left_table
-RIGHT JOIN right_table
- ON right_table.key = left_table.key
-WHERE left_table.key IS NULL
-```
-
-
-
- |
-
-
-
-
- |
-
- Retrieving data that does not belong to both the left and right tables simultaneously (reverse INNER JOIN):
-
-
-
-```sql
-SELECT table_fields
-FROM left_table
- FULL OUTER JOIN right_table
- ON right_table.key = left_table.key
-WHERE left_table.key IS NULL
- OR right_table.key IS NULL
+ RIGHT JOIN right_table ON right_table.key = left_table.key
+WHERE left_table.key IS NULL;
```
-
-
-
+The condition in the second part is required: without it the matched rows would appear in the result twice.
-```sql
-SELECT table_fields
-FROM left_table
-LEFT JOIN right_table
- ON right_table.key = left_table.key
-WHERE right_table.key IS NULL
+## All types of table joins
-UNION ALL
+| Join type | Result | Rows in the example |
+| ----------------- | ----------------------------------- | ------------------: |
+| `LEFT JOIN` | All rows of the left table | 4 |
+| `INNER JOIN` | Only matching rows | 3 |
+| `RIGHT JOIN` | All rows of the right table | 4 |
+| `FULL JOIN` | All rows of both tables | 5 |
+| `LEFT ANTI JOIN` | Left table without a match | 1 |
+| `RIGHT ANTI JOIN` | Right table without a match | 1 |
+| `FULL ANTI JOIN` | Everything except the matching rows | 2 |
-SELECT table_fields
-FROM left_table
-RIGHT JOIN right_table
- ON right_table.key = left_table.key
-WHERE left_table.key IS NULL
-```
+Let's check yourself. The left table has 8 rows. How many rows will a `LEFT JOIN` with the right table return?
-
+1. **Correct answer:** Eight or more — it depends on how many matches were found — Every row of the left table is guaranteed to appear in the result, but a row with several matches in the right table produces several rows.
- |
-
-
+2. Exactly eight: a left join returns all rows of the left table — All eight rows do appear in the result, but a row with several matches is multiplied — the result grows beyond eight rows.
-