SQL: UNION Query
The SQL UNION query allows you to combine the result sets of 2 or more SQL SELECT statements. It removes duplicate rows between the various SELECT statements.
Each SQL SELECT statement within the UNION query must have the same number of fields in the result sets with similar data types.
The syntax for the SQL UNION query is:
select field1, field2, . field_n from tables
UNION
select field1, field2, . field_n from tables;
SQL UNION Query - Returns single field example
The following is an example of the SQL UNION query that returns one field from multiple SELECT statements (and both fields have the same data type):
select supplier_id from suppliers
UNION
select supplier_id from orders;
In this SQL UNION query example, if a supplier_id appeared in both the suppliers and orders table, it would appear once in your result set. The SQL UNION query removes duplicates. If you do notwish to remove duplicates, try using the SQL UNION ALL query.
SQL: UNION ALL Query
The SQL UNION ALL query allows you to combine the result sets of 2 or more SELECT statements. It returns all rows from the query (even if the row exists in more than one of the SELECT statements).
Each SQL SELECT statement within the SQL UNION ALL query must have the same number of fields in the result sets with similar data types.
The syntax for the SQL UNION ALL query is:
select field1, field2, ... field_n from tables
UNION ALL
select field1, field2, ... field_n from tables;
SQL UNION ALL Query - Returns single field example
The following is an example of the SQL UNION ALL query that returns one field from multiple SELECT statements (and both fields have the same data type):
select supplier_id from suppliers
UNION ALL
select supplier_id from orders;
This SQL UNION ALL query would return a supplier_id multiple times in your result set if the supplier_id appeared in both the suppliers and orders table. The SQL UNION ALL query does not remove duplicates. If you wish to remove duplicates, try using the SQL UNION query.
SQL: INTERSECT Query
The SQL INTERSECT query allows you to return the results of 2 or more "select" queries. However, it only returns the rows selected by all queries. If a record exists in one query and not in the other, it will be omitted from the INTERSECT results.
Each SQL statement within the SQL INTERSECT query must have the same number of fields in the result sets with similar data types.
The syntax for the SQL INTERSECT query is:
select field1, field2, . field_n from tables
INTERSECT
select field1, field2, . field_n from tables;
SQL INTERSECT Query - Single field example
The following is an example of an SQL INTERSECT query that has one field with the same data type:
select supplier_id from suppliers
INTERSECT
select supplier_id from orders;
In this SQL INTERSECT query example, if a supplier_id appeared in both the suppliers and orders table, it would appear in your result set.
SQL: MINUS Query
The SQL MINUS query returns all rows in the first SQL SELECT statement that are not returned in the second SQL SELECT statement.
Each SQL SELECT statement within the SQL MINUS query must have the same number of fields in the result sets with similar data types.
The syntax for the SQL MINUS query is:
select field1, field2, ... field_n from tables
MINUS
select field1, field2, ... field_n from tables;
SQL MINUS Query - Single field example
The following is an example of an SQL MINUS query that has one field with the same data type:
select supplier_id from suppliers
MINUS
select supplier_id from orders;
This SQL Minus query example returns all supplier_id values that are in the suppliers table and not in the orders table. What this means is that if a supplier_id value existed in the suppliers table and also existed in the orders table, the supplier_id value would not appear in this result set.