2015-11-26 13:15:04 +00:00
|
|
|
---
|
2017-10-29 16:03:38 +00:00
|
|
|
title: SQL joins
|
2018-12-06 22:15:40 +00:00
|
|
|
category: Databases
|
2020-07-04 13:33:09 +00:00
|
|
|
updated: 2018-12-06
|
2017-10-29 16:03:38 +00:00
|
|
|
weight: -1
|
2015-11-26 13:15:04 +00:00
|
|
|
---
|
|
|
|
|
2017-10-29 16:03:38 +00:00
|
|
|
### Example
|
|
|
|
|
2015-11-26 13:15:04 +00:00
|
|
|
```
|
2017-10-29 16:05:56 +00:00
|
|
|
SELECT * FROM order_items \
|
|
|
|
LEFT OUTER JOIN orders \
|
|
|
|
ON order_items.order_id = orders.id
|
2015-11-26 13:15:04 +00:00
|
|
|
```
|
2017-10-29 16:03:38 +00:00
|
|
|
{: .-wrap}
|
|
|
|
|
2017-10-29 16:05:56 +00:00
|
|
|
Joins are typically added to `SELECT` statements to add more columns and records.
|
|
|
|
|
2017-10-29 16:03:38 +00:00
|
|
|
### Diagram
|
2015-11-26 13:15:04 +00:00
|
|
|
|
2017-10-29 16:05:56 +00:00
|
|
|
```
|
|
|
|
SELECT * FROM `A` INNER JOIN `B`
|
|
|
|
```
|
|
|
|
{: .-setup}
|
|
|
|
|
2015-11-26 13:15:04 +00:00
|
|
|
```
|
2017-10-29 16:03:38 +00:00
|
|
|
┌────────┐
|
|
|
|
│ A ┌───┼────┐
|
|
|
|
│ │ ∩ │ │
|
|
|
|
└────┼───┘ B │
|
|
|
|
└────────┘
|
2015-11-26 13:15:04 +00:00
|
|
|
```
|
2017-10-29 16:05:56 +00:00
|
|
|
{: .-box-chars.-setup}
|
2015-11-26 13:15:04 +00:00
|
|
|
|
2017-10-29 16:03:38 +00:00
|
|
|
| Join | What |
|
|
|
|
| ---- | ---- |
|
|
|
|
| Inner join | `∩` |
|
|
|
|
| Left outer join | `A` + `∩` |
|
|
|
|
| Right outer join | `∩` + `B` |
|
|
|
|
| Full outer join | `A` + `∩` + `B` |
|