You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Set operators are used to combine two input relations into a single one. Spark SQL supports three types of set operators:
23
-
-`EXCEPT` or `MINUS`
24
-
-`INTERSECT`
25
-
-`UNION`
25
+
26
+
-`EXCEPT` or `MINUS`
27
+
-`INTERSECT`
28
+
-`UNION`
26
29
27
30
Note that input relations must have the same number of columns and compatible data types for the respective columns.
28
31
29
32
### EXCEPT
33
+
30
34
`EXCEPT` and `EXCEPT ALL` return the rows that are found in one relation but not the other. `EXCEPT` (alternatively, `EXCEPT DISTINCT`) takes only distinct rows while `EXCEPT ALL` does not remove duplicates from the result rows. Note that `MINUS` is an alias for `EXCEPT`.
`INTERSECT` and `INTERSECT ALL` return the rows that are found in both relations. `INTERSECT` (alternatively, `INTERSECT DISTINCT`) takes only distinct rows while `INTERSECT ALL` does not remove duplicates from the result rows.
`UNION` and `UNION ALL` return the rows that are found in either relation. `UNION` (alternatively, `UNION DISTINCT`) takes only distinct rows while `UNION ALL` does not remove duplicates from the result rows.
47
-
48
-
#### Syntax
49
-
{% highlight sql %}
50
-
[ ( ] relation [ ) ] UNION [ ALL | DISTINCT ][ ( ] relation [ ) ]
51
-
{% endhighlight %}
52
-
53
-
### Examples
54
-
{% highlight sql %}
55
-
-- Use number1 and number2 tables to demonstrate set operators.
45
+
-- Use number1 and number2 tables to demonstrate set operators in this page.
56
46
SELECT * FROM number1;
57
-
+---+
58
-
| c|
59
-
+---+
60
-
| 3|
61
-
| 1|
62
-
| 2|
63
-
| 2|
64
-
| 3|
65
-
| 4|
66
-
+---+
67
-
47
+
+---+
48
+
| c|
49
+
+---+
50
+
| 3|
51
+
| 1|
52
+
| 2|
53
+
| 2|
54
+
| 3|
55
+
| 4|
56
+
+---+
57
+
68
58
SELECT * FROM number2;
69
-
+---+
70
-
| c|
71
-
+---+
72
-
| 5|
73
-
| 1|
74
-
| 2|
75
-
| 2|
76
-
+---+
59
+
+---+
60
+
| c|
61
+
+---+
62
+
| 5|
63
+
| 1|
64
+
| 2|
65
+
| 2|
66
+
+---+
77
67
78
68
SELECT c FROM number1 EXCEPT SELECT c FROM number2;
79
-
+---+
80
-
| c|
81
-
+---+
82
-
| 3|
83
-
| 4|
84
-
+---+
69
+
+---+
70
+
| c|
71
+
+---+
72
+
| 3|
73
+
| 4|
74
+
+---+
85
75
86
76
SELECT c FROM number1 MINUS SELECT c FROM number2;
87
-
+---+
88
-
| c|
89
-
+---+
90
-
| 3|
91
-
| 4|
92
-
+---+
77
+
+---+
78
+
| c|
79
+
+---+
80
+
| 3|
81
+
| 4|
82
+
+---+
93
83
94
84
SELECT c FROM number1 EXCEPT ALL (SELECT c FROM number2);
95
-
+---+
96
-
| c|
97
-
+---+
98
-
| 3|
99
-
| 3|
100
-
| 4|
101
-
+---+
85
+
+---+
86
+
| c|
87
+
+---+
88
+
| 3|
89
+
| 3|
90
+
| 4|
91
+
+---+
102
92
103
93
SELECT c FROM number1 MINUS ALL (SELECT c FROM number2);
104
-
+---+
105
-
| c|
106
-
+---+
107
-
| 3|
108
-
| 3|
109
-
| 4|
110
-
+---+
94
+
+---+
95
+
| c|
96
+
+---+
97
+
| 3|
98
+
| 3|
99
+
| 4|
100
+
+---+
101
+
{% endhighlight %}
111
102
103
+
### INTERSECT
104
+
105
+
`INTERSECT` and `INTERSECT ALL` return the rows that are found in both relations. `INTERSECT` (alternatively, `INTERSECT DISTINCT`) takes only distinct rows while `INTERSECT ALL` does not remove duplicates from the result rows.
(SELECT c FROM number1) INTERSECT (SELECT c FROM number2);
113
-
+---+
114
-
| c|
115
-
+---+
116
-
| 1|
117
-
| 2|
118
-
+---+
117
+
+---+
118
+
| c|
119
+
+---+
120
+
| 1|
121
+
| 2|
122
+
+---+
119
123
120
124
(SELECT c FROM number1) INTERSECT DISTINCT (SELECT c FROM number2);
121
-
+---+
122
-
| c|
123
-
+---+
124
-
| 1|
125
-
| 2|
126
-
+---+
125
+
+---+
126
+
| c|
127
+
+---+
128
+
| 1|
129
+
| 2|
130
+
+---+
127
131
128
132
(SELECT c FROM number1) INTERSECT ALL (SELECT c FROM number2);
129
-
+---+
130
-
| c|
131
-
+---+
132
-
| 1|
133
-
| 2|
134
-
| 2|
135
-
+---+
133
+
+---+
134
+
| c|
135
+
+---+
136
+
| 1|
137
+
| 2|
138
+
| 2|
139
+
+---+
140
+
{% endhighlight %}
141
+
142
+
### UNION
136
143
144
+
`UNION` and `UNION ALL` return the rows that are found in either relation. `UNION` (alternatively, `UNION DISTINCT`) takes only distinct rows while `UNION ALL` does not remove duplicates from the result rows.
145
+
146
+
#### Syntax
147
+
148
+
{% highlight sql %}
149
+
[ ( ] relation [ ) ] UNION [ ALL | DISTINCT ][ ( ] relation [ ) ]
150
+
{% endhighlight %}
151
+
152
+
### Examples
153
+
154
+
{% highlight sql %}
137
155
(SELECT c FROM number1) UNION (SELECT c FROM number2);
138
-
+---+
139
-
| c|
140
-
+---+
141
-
| 1|
142
-
| 3|
143
-
| 5|
144
-
| 4|
145
-
| 2|
146
-
+---+
156
+
+---+
157
+
| c|
158
+
+---+
159
+
| 1|
160
+
| 3|
161
+
| 5|
162
+
| 4|
163
+
| 2|
164
+
+---+
147
165
148
166
(SELECT c FROM number1) UNION DISTINCT (SELECT c FROM number2);
149
-
+---+
150
-
| c|
151
-
+---+
152
-
| 1|
153
-
| 3|
154
-
| 5|
155
-
| 4|
156
-
| 2|
157
-
+---+
167
+
+---+
168
+
| c|
169
+
+---+
170
+
| 1|
171
+
| 3|
172
+
| 5|
173
+
| 4|
174
+
| 2|
175
+
+---+
158
176
159
177
SELECT c FROM number1 UNION ALL (SELECT c FROM number2);
0 commit comments