A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.snowflake.com/en/sql-reference/operators-query below:

Website Navigation


Set operators | Snowflake Documentation

Set operators

Set operators combine the intermediate results of multiple query blocks into a single result set.

General syntax
[ ( ] <query> [ ) ]
{
  INTERSECT |
  { MINUS | EXCEPT } |
  UNION [ { DISTINCT | ALL } ] [ BY NAME ]
}
[ ( ] <query> [ ) ]
[ ORDER BY ... ]
[ LIMIT ... ]

Copy

General usage notes Sample tables for examples

Some of the examples in this topic use the following sample tables. Both tables have a postal code column. One table records the postal code of each sales office, and the other records the postal code of each customer.

CREATE OR REPLACE TABLE sales_office_postal_example(
  office_name VARCHAR,
  postal_code VARCHAR);

INSERT INTO sales_office_postal_example VALUES ('sales1', '94061');
INSERT INTO sales_office_postal_example VALUES ('sales2', '94070');
INSERT INTO sales_office_postal_example VALUES ('sales3', '98116');
INSERT INTO sales_office_postal_example VALUES ('sales4', '98005');

CREATE OR REPLACE TABLE customer_postal_example(
  customer VARCHAR,
  postal_code VARCHAR);

INSERT INTO customer_postal_example VALUES ('customer1', '94066');
INSERT INTO customer_postal_example VALUES ('customer2', '94061');
INSERT INTO customer_postal_example VALUES ('customer3', '98444');
INSERT INTO customer_postal_example VALUES ('customer4', '98005');

Copy

INTERSECT

Returns rows from one query’s result set which also appear in another query’s result set, with duplicate elimination.

Syntax
[ ( ] <query> [ ) ]
INTERSECT
[ ( ] <query> [ ) ]

Copy

INTERSECT operator examples

To find the postal codes that are in both the sales_office_postal_example table and the customer_postal_example table, query the sample tables:

SELECT postal_code FROM sales_office_postal_example
INTERSECT
SELECT postal_code FROM customer_postal_example
ORDER BY postal_code;

Copy

+-------------+
| POSTAL_CODE |
|-------------|
| 94061       |
| 98005       |
+-------------+
MINUS , EXCEPT

Returns the rows returned by the first query that aren’t also returned by the second query.

The MINUS and EXCEPT keywords have the same meaning and can be used interchangeably.

Syntax
[ ( ] <query> [ ) ]
MINUS
[ ( ] <query> [ ) ]

[ ( ] <query> [ ) ]
EXCEPT
[ ( ] <query> [ ) ]

Copy

MINUS operator examples

Query the sample tables to find the postal codes in the sales_office_postal_example table that aren’t also in the customer_postal_example table:

SELECT postal_code FROM sales_office_postal_example
MINUS
SELECT postal_code FROM customer_postal_example
ORDER BY postal_code;

Copy

+-------------+
| POSTAL_CODE |
|-------------|
| 94070       |
| 98116       |
+-------------+

Query the sample tables to find the postal codes in the customer_postal_example table that aren’t also in the sales_office_postal_example table:

SELECT postal_code FROM customer_postal_example
MINUS
SELECT postal_code FROM sales_office_postal_example
ORDER BY postal_code;

Copy

+-------------+
| POSTAL_CODE |
|-------------|
| 94066       |
| 98444       |
+-------------+
UNION [ { DISTINCT | ALL } ] [ BY NAME ]

Combines the result sets from two queries:

The default is UNION DISTINCT (that is, combine rows by column position with duplicate elimination). The DISTINCT keyword is optional. The DISTINCT keyword and the ALL keyword are mutually exclusive.

Use UNION or UNION ALL when the column positions match in the tables that you are combining. Use UNION BY NAME or UNION ALL BY NAME for the following use cases:

Syntax
[ ( ] <query> [ ) ]
UNION [ { DISTINCT | ALL } ] [ BY NAME ]
[ ( ] <query> [ ) ]

Copy

Usage notes for the BY NAME clause

In addition to the general usage notes, the following usage notes apply to UNION BY NAME and UNION ALL BY NAME:

UNION operator examples

The following examples use the UNION operator:

Combine the results from two queries by column position

To combine the result sets by column position from two queries on the sample tables, use the UNION operator:

SELECT office_name office_or_customer, postal_code FROM sales_office_postal_example
UNION
SELECT customer, postal_code FROM customer_postal_example
ORDER BY postal_code;

Copy

+--------------------+-------------+
| OFFICE_OR_CUSTOMER | POSTAL_CODE |
|--------------------+-------------|
| sales1             | 94061       |
| customer2          | 94061       |
| customer1          | 94066       |
| sales2             | 94070       |
| sales4             | 98005       |
| customer4          | 98005       |
| sales3             | 98116       |
| customer3          | 98444       |
+--------------------+-------------+
Combine the results from two queries by column name

Create two tables with differing column order and insert data:

CREATE OR REPLACE TABLE union_demo_column_order1 (
  a INTEGER,
  b VARCHAR);

INSERT INTO union_demo_column_order1 VALUES
  (1, 'one'),
  (2, 'two'),
  (3, 'three');

CREATE OR REPLACE TABLE union_demo_column_order2 (
  B VARCHAR,
  A INTEGER);

INSERT INTO union_demo_column_order2 VALUES
  ('three', 3),
  ('four', 4);

Copy

To combine the result sets by column name from two queries, use the UNION BY NAME operator:

SELECT * FROM union_demo_column_order1
UNION BY NAME
SELECT * FROM union_demo_column_order2
ORDER BY a;

Copy

+---+-------+
| A | B     |
|---+-------|
| 1 | one   |
| 2 | two   |
| 3 | three |
| 4 | four  |
+---+-------+

The output shows that the query eliminated the duplicate row (with 3 in column A and three in column B).

To combine the tables without duplicate elimination, use the UNION ALL BY NAME operator:

SELECT * FROM union_demo_column_order1
UNION ALL BY NAME
SELECT * FROM union_demo_column_order2
ORDER BY a;

Copy

+---+-------+
| A | B     |
|---+-------|
| 1 | one   |
| 2 | two   |
| 3 | three |
| 3 | three |
| 4 | four  |
+---+-------+

Notice that the cases of the column names don’t match in the two tables. The column names are lowercase in the union_demo_column_order1 table and uppercase in the union_demo_column_order2 table. If you run a query with quotation marks around the column names, an error is returned because the matching of quoted identifiers is case-sensitive. For example, the following query places quotation marks around the column names:

SELECT 'a', 'b' FROM union_demo_column_order1
UNION ALL BY NAME
SELECT 'B', 'A' FROM union_demo_column_order2
ORDER BY a;

Copy

000904 (42000): SQL compilation error: error line 4 at position 9
invalid identifier 'A'
Use an alias to combine the results from two queries with different column names

When you use the UNION BY NAME operator to combine the result sets by column name from two queries on the sample tables, the rows in the result set have NULL values because the column names don’t match:

SELECT office_name, postal_code FROM sales_office_postal_example
UNION BY NAME
SELECT customer, postal_code FROM customer_postal_example
ORDER BY postal_code;

Copy

+-------------+-------------+-----------+
| OFFICE_NAME | POSTAL_CODE | CUSTOMER  |
|-------------+-------------+-----------|
| sales1      | 94061       | NULL      |
| NULL        | 94061       | customer2 |
| NULL        | 94066       | customer1 |
| sales2      | 94070       | NULL      |
| sales4      | 98005       | NULL      |
| NULL        | 98005       | customer4 |
| sales3      | 98116       | NULL      |
| NULL        | 98444       | customer3 |
+-------------+-------------+-----------+

The output shows that columns with different identifiers aren’t combined and that rows have NULL values for columns that are in one table but not the other. The postal_code column is in both tables, so there are no NULL values in the output for the postal_code column.

The following query uses the alias office_or_customer so that columns with different names have the same name for the duration of the query:

SELECT office_name AS office_or_customer, postal_code FROM sales_office_postal_example
UNION BY NAME
SELECT customer AS office_or_customer, postal_code FROM customer_postal_example
ORDER BY postal_code;

Copy

+--------------------+-------------+
| OFFICE_OR_CUSTOMER | POSTAL_CODE |
|--------------------+-------------|
| sales1             | 94061       |
| customer2          | 94061       |
| customer1          | 94066       |
| sales2             | 94070       |
| sales4             | 98005       |
| customer4          | 98005       |
| sales3             | 98116       |
| customer3          | 98444       |
+--------------------+-------------+
Use the UNION operator and cast mismatched data types

This example demonstrates a potential issue with using the UNION operator when data types don’t match, then provides the solution.

Start by creating the tables and inserting some data:

CREATE OR REPLACE TABLE union_test1 (v VARCHAR);
CREATE OR REPLACE TABLE union_test2 (i INTEGER);

INSERT INTO union_test1 (v) VALUES ('Smith, Jane');
INSERT INTO union_test2 (i) VALUES (42);

Copy

Execute a UNION by column position operation with different data types (a VARCHAR value in union_test1 and an INTEGER value in union_test2):

SELECT v FROM union_test1
UNION
SELECT i FROM union_test2;

Copy

This query returns an error:

100038 (22018): Numeric value 'Smith, Jane' is not recognized

Now use explicit casting to convert the inputs to a compatible type:

SELECT v::VARCHAR FROM union_test1
UNION
SELECT i::VARCHAR FROM union_test2;

Copy

+-------------+
| V::VARCHAR  |
|-------------|
| Smith, Jane |
| 42          |
+-------------+

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4