0001 ---
0002 layout: global
0003 title: Set Operators
0004 displayTitle: Set Operators
0005 license: |
0006 Licensed to the Apache Software Foundation (ASF) under one or more
0007 contributor license agreements. See the NOTICE file distributed with
0008 this work for additional information regarding copyright ownership.
0009 The ASF licenses this file to You under the Apache License, Version 2.0
0010 (the "License"); you may not use this file except in compliance with
0011 the License. You may obtain a copy of the License at
0012
0013 http://www.apache.org/licenses/LICENSE-2.0
0014
0015 Unless required by applicable law or agreed to in writing, software
0016 distributed under the License is distributed on an "AS IS" BASIS,
0017 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0018 See the License for the specific language governing permissions and
0019 limitations under the License.
0020 ---
0021
0022 ### Description
0023
0024 Set operators are used to combine two input relations into a single one. Spark SQL supports three types of set operators:
0025
0026 - `EXCEPT` or `MINUS`
0027 - `INTERSECT`
0028 - `UNION`
0029
0030 Note that input relations must have the same number of columns and compatible data types for the respective columns.
0031
0032 ### EXCEPT
0033
0034 `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`.
0035
0036 #### Syntax
0037
0038 ```sql
0039 [ ( ] relation [ ) ] EXCEPT | MINUS [ ALL | DISTINCT ] [ ( ] relation [ ) ]
0040 ```
0041
0042 #### Examples
0043
0044 ```sql
0045 -- Use number1 and number2 tables to demonstrate set operators in this page.
0046 SELECT * FROM number1;
0047 +---+
0048 | c|
0049 +---+
0050 | 3|
0051 | 1|
0052 | 2|
0053 | 2|
0054 | 3|
0055 | 4|
0056 +---+
0057
0058 SELECT * FROM number2;
0059 +---+
0060 | c|
0061 +---+
0062 | 5|
0063 | 1|
0064 | 2|
0065 | 2|
0066 +---+
0067
0068 SELECT c FROM number1 EXCEPT SELECT c FROM number2;
0069 +---+
0070 | c|
0071 +---+
0072 | 3|
0073 | 4|
0074 +---+
0075
0076 SELECT c FROM number1 MINUS SELECT c FROM number2;
0077 +---+
0078 | c|
0079 +---+
0080 | 3|
0081 | 4|
0082 +---+
0083
0084 SELECT c FROM number1 EXCEPT ALL (SELECT c FROM number2);
0085 +---+
0086 | c|
0087 +---+
0088 | 3|
0089 | 3|
0090 | 4|
0091 +---+
0092
0093 SELECT c FROM number1 MINUS ALL (SELECT c FROM number2);
0094 +---+
0095 | c|
0096 +---+
0097 | 3|
0098 | 3|
0099 | 4|
0100 +---+
0101 ```
0102
0103 ### INTERSECT
0104
0105 `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.
0106
0107 #### Syntax
0108
0109 ```sql
0110 [ ( ] relation [ ) ] INTERSECT [ ALL | DISTINCT ] [ ( ] relation [ ) ]
0111 ```
0112
0113 #### Examples
0114
0115 ```sql
0116 (SELECT c FROM number1) INTERSECT (SELECT c FROM number2);
0117 +---+
0118 | c|
0119 +---+
0120 | 1|
0121 | 2|
0122 +---+
0123
0124 (SELECT c FROM number1) INTERSECT DISTINCT (SELECT c FROM number2);
0125 +---+
0126 | c|
0127 +---+
0128 | 1|
0129 | 2|
0130 +---+
0131
0132 (SELECT c FROM number1) INTERSECT ALL (SELECT c FROM number2);
0133 +---+
0134 | c|
0135 +---+
0136 | 1|
0137 | 2|
0138 | 2|
0139 +---+
0140 ```
0141
0142 ### UNION
0143
0144 `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.
0145
0146 #### Syntax
0147
0148 ```sql
0149 [ ( ] relation [ ) ] UNION [ ALL | DISTINCT ] [ ( ] relation [ ) ]
0150 ```
0151
0152 ### Examples
0153
0154 ```sql
0155 (SELECT c FROM number1) UNION (SELECT c FROM number2);
0156 +---+
0157 | c|
0158 +---+
0159 | 1|
0160 | 3|
0161 | 5|
0162 | 4|
0163 | 2|
0164 +---+
0165
0166 (SELECT c FROM number1) UNION DISTINCT (SELECT c FROM number2);
0167 +---+
0168 | c|
0169 +---+
0170 | 1|
0171 | 3|
0172 | 5|
0173 | 4|
0174 | 2|
0175 +---+
0176
0177 SELECT c FROM number1 UNION ALL (SELECT c FROM number2);
0178 +---+
0179 | c|
0180 +---+
0181 | 3|
0182 | 1|
0183 | 2|
0184 | 2|
0185 | 3|
0186 | 4|
0187 | 5|
0188 | 1|
0189 | 2|
0190 | 2|
0191 +---+
0192 ```
0193
0194 ### Related Statements
0195
0196 * [SELECT Statement](sql-ref-syntax-qry-select.html)