Skip to content

Commit 4cf0cf0

Browse files
authored
User Guide: Add documentation for JOIN syntax (#3130)
* Documentation for JOIN syntax * revert Cargo.lock * prettier
1 parent 15a9a4b commit 4cf0cf0

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

docs/source/user-guide/sql/select.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ DataFusion supports the following syntax for queries:
2828
[ [WITH](#with-clause) with_query [, ...] ] <br/>
2929
[SELECT](#select-clause) [ ALL | DISTINCT ] select_expr [, ...] <br/>
3030
[ [FROM](#from-clause) from_item [, ...] ] <br/>
31+
[ [JOIN](#join-clause) join_item [, ...] ] <br/>
3132
[ [WHERE](#where-clause) condition ] <br/>
3233
[ [GROUP BY](#group-by-clause) grouping_element [, ...] ] <br/>
3334
[ [HAVING](#having-clause) condition] <br/>
@@ -77,6 +78,92 @@ Example:
7778
SELECT a FROM table WHERE a > 10
7879
```
7980

81+
## JOIN clause
82+
83+
DataFusion supports `INNER JOIN`, `LEFT OUTER JOIN`, `RIGHT OUTER JOIN`, `FULL OUTER JOIN`, and `CROSS JOIN`.
84+
85+
The following examples are based on this table:
86+
87+
```sql
88+
select * from x;
89+
+----------+----------+
90+
| column_1 | column_2 |
91+
+----------+----------+
92+
| 1 | 2 |
93+
+----------+----------+
94+
```
95+
96+
### INNER JOIN
97+
98+
The keywords `JOIN` or `INNER JOIN` define a join that only shows rows where there is a match in both tables.
99+
100+
```sql
101+
select * from x inner join x y ON x.column_1 = y.column_1;
102+
+----------+----------+----------+----------+
103+
| column_1 | column_2 | column_1 | column_2 |
104+
+----------+----------+----------+----------+
105+
| 1 | 2 | 1 | 2 |
106+
+----------+----------+----------+----------+
107+
```
108+
109+
### LEFT OUTER JOIN
110+
111+
The keywords `LEFT JOIN` or `LEFT OUTER JOIN` define a join that includes all rows from the left table even if there
112+
is not a match in the right table. When there is no match, null values are produced for the right side of the join.
113+
114+
```sql
115+
select * from x left join x y ON x.column_1 = y.column_2;
116+
+----------+----------+----------+----------+
117+
| column_1 | column_2 | column_1 | column_2 |
118+
+----------+----------+----------+----------+
119+
| 1 | 2 | | |
120+
+----------+----------+----------+----------+
121+
```
122+
123+
### RIGHT OUTER JOIN
124+
125+
The keywords `RIGHT JOIN` or `RIGHT OUTER JOIN` define a join that includes all rows from the right table even if there
126+
is not a match in the left table. When there is no match, null values are produced for the left side of the join.
127+
128+
```sql
129+
select * from x right join x y ON x.column_1 = y.column_2;
130+
+----------+----------+----------+----------+
131+
| column_1 | column_2 | column_1 | column_2 |
132+
+----------+----------+----------+----------+
133+
| | | 1 | 2 |
134+
+----------+----------+----------+----------+
135+
```
136+
137+
### FULL OUTER JOIN
138+
139+
The keywords `FULL JOIN` or `FULL OUTER JOIN` define a join that is effectively a union of a `LEFT OUTER JOIN` and
140+
`RIGHT OUTER JOIN`. It will show all rows from the left and right side of the join and will produce null values on
141+
either side of the join where there is not a match.
142+
143+
```sql
144+
select * from x full outer join x y ON x.column_1 = y.column_2;
145+
+----------+----------+----------+----------+
146+
| column_1 | column_2 | column_1 | column_2 |
147+
+----------+----------+----------+----------+
148+
| 1 | 2 | | |
149+
| | | 1 | 2 |
150+
+----------+----------+----------+----------+
151+
```
152+
153+
### CROSS JOIN
154+
155+
A cross join produces a cartesian product that matches every row in the left side of the join with every row in the
156+
right side of the join.
157+
158+
```sql
159+
select * from x cross join x y;
160+
+----------+----------+----------+----------+
161+
| column_1 | column_2 | column_1 | column_2 |
162+
+----------+----------+----------+----------+
163+
| 1 | 2 | 1 | 2 |
164+
+----------+----------+----------+----------+
165+
```
166+
80167
## GROUP BY clause
81168

82169
Example:

0 commit comments

Comments
 (0)