You can combine multiple queries using the set operators UNION, UNION ALL, INTERSECT, and MINUS. All set operators have equal precedence. If a SQL statement contains multiple set operators, then Oracle Database evaluates them from the left to right unless parentheses explicitly specify another order. The corresponding expressions in the select lists of the component queries of a compound query must match in number and must be in the same data type group (such as numeric or character). If component queries select character data, then the data type of the return values are determined as follows:
If component queries select numeric data, then the data type of the return values is determined by numeric precedence:
In queries using set operators, Oracle does not perform implicit conversion across data type groups. Therefore, if the corresponding expressions of component queries resolve to both character data and numeric data, Oracle returns an error. Examples The following query is valid: SELECT 3 FROM DUAL INTERSECT SELECT 3f FROM DUAL;This is implicitly converted to the following compound query: SELECT TO_BINARY_FLOAT(3) FROM DUAL INTERSECT SELECT 3f FROM DUAL;The following query returns an error: Restrictions on the Set Operators The set operators are subject to the following restrictions:
UNION Example The following statement combines the results of two queries with the UNION operator, which eliminates duplicate selected rows. This statement shows that you must match data type (using the TO_CHAR function) when columns do not exist in one or the other table: SELECT location_id, department_name "Department", TO_CHAR(NULL) "Warehouse" FROM departments UNION SELECT location_id, TO_CHAR(NULL) "Department", warehouse_name FROM warehouses; LOCATION_ID Department Warehouse ----------- ------------------------------ --------------------------- 1400 IT 1400 Southlake, Texas 1500 Shipping 1500 San Francisco 1600 New Jersey 1700 Accounting 1700 Administration 1700 Benefits 1700 Construction 1700 Contracting 1700 Control And Credit ...UNION ALL Example The UNION operator returns only distinct rows that appear in either result, while the UNION ALL operator returns all rows. The UNION ALL operator does not eliminate duplicate selected rows: SELECT product_id FROM order_items UNION SELECT product_id FROM inventories ORDER BY product_id; SELECT location_id FROM locations UNION ALL SELECT location_id FROM departments ORDER BY location_id;A location_id value that appears multiple times in either or both queries (such as '1700') is returned only once by the UNION operator, but multiple times by the UNION ALL operator. INTERSECT Example The following statement combines the results with the INTERSECT operator, which returns only those unique rows returned by both queries: SELECT product_id FROM inventories INTERSECT SELECT product_id FROM order_items ORDER BY product_id;MINUS Example The following statement combines results with the MINUS operator, which returns only unique rows returned by the first query but not by the second: SELECT product_id FROM inventories MINUS SELECT product_id FROM order_items ORDER BY product_id;
This SQL tutorial explains how to use the SQL UNION operator with syntax and examples.
The SQL UNION operator is used to combine the result sets of 2 or more SELECT statements. It removes duplicate rows between the various SELECT statements. Each SELECT statement within the UNION must have the same number of fields in the result sets with similar data types. What is the difference between UNION and UNION ALL?
The syntax for the UNION operator in SQL is: SELECT expression1, expression2, ... expression_n FROM tables [WHERE conditions] UNION SELECT expression1, expression2, ... expression_n FROM tables [WHERE conditions];Parameters or Argumentsexpression1, expression2, expression_n The columns or calculations that you wish to retrieve. tables The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause. WHERE conditions Optional. The conditions that must be met for the records to be selected.Note
Let's look at how to use the SQL UNION operator that returns one field. In this simple example, the field in both SELECT statements will have the same name and data type. For example: SELECT supplier_id FROM suppliers UNION SELECT supplier_id FROM orders ORDER BY supplier_id;In this SQL UNION operator example, if a supplier_id appeared in both the suppliers and orders table, it would appear once in your result set. The UNION operator removes duplicates. If you do not wish to remove duplicates, try using the UNION ALL operator. Now, let's explore this example further will some data. If you had the suppliers table populated with the following records:
And the orders table populated with the following records:
And you executed the following UNION statement: SELECT supplier_id FROM suppliers UNION SELECT supplier_id FROM orders ORDER BY supplier_id;You would get the following results:
As you can see in this example, the UNION has taken all supplier_id values from both the suppliers table as well as the orders table and returned a combined result set. Because the UNION operator removed duplicates between the result sets, the supplier_id of 2000 only appears once, even though it is found in both the suppliers and orders table. If you do not wish to remove duplicates, try using the UNION ALL operator instead.
It is not necessary that the corresponding columns in each SELECT statement have the same name, but they do need to be the same corresponding data types. When you don't have the same column names between the SELECT statements, it gets a bit tricky, especially when you want to order the results of the query using the ORDER BY clause. Let's look at how to use the UNION operator with different column names and order the query results. For example: SELECT supplier_id, supplier_name FROM suppliers WHERE supplier_id > 2000 UNION SELECT company_id, company_name FROM companies WHERE company_id > 1000 ORDER BY 1;In this SQL UNION example, since the column names are different between the two SELECT statements, it is more advantageous to reference the columns in the ORDER BY clause by their position in the result set. In this example, we've sorted the results by supplier_id / company_id in ascending order, as denoted by the ORDER BY 1. The supplier_id / company_id fields are in position #1 in the result set. Now, let's explore this example further with data. If you had the suppliers table populated with the following records:
And the companies table populated with the following records:
And you executed the following UNION statement: SELECT supplier_id, supplier_name FROM suppliers WHERE supplier_id > 2000 UNION SELECT company_id, company_name FROM companies WHERE company_id > 1000 ORDER BY 1;You would get the following results:
First, notice that the record with supplier_id of 3000 only appears once in the result set because the UNION query removed duplicate entries. Second, notice that the column headings in the result set are called supplier_id and supplier_name. This is because these were the column names used in the first SELECT statement in the UNION. If you had wanted to, you could have aliased the columns as follows: SELECT supplier_id AS ID_Value, supplier_name AS Name_Value FROM suppliers WHERE supplier_id > 2000 UNION SELECT company_id AS ID_Value, company_name AS Name_Value FROM companies WHERE company_id > 1000 ORDER BY 1;Now the column headings in the result will be aliased as ID_Value for the first column and Name_Value for the second column.
Question: I need to compare two dates and return the count of a field based on the date values. For example, I have a date field in a table called last updated date. I have to check if trunc(last_updated_date >= trunc(sysdate-13). Answer: Since you are using the COUNT function which is an aggregate function, we'd recommend using the Oracle UNION operator. For example, you could try the following: SELECT a.code AS Code, a.name AS Name, COUNT(b.Ncode) FROM cdmaster a, nmmaster b WHERE a.code = b.code AND a.status = 1 AND b.status = 1 AND b.Ncode <> 'a10' AND TRUNC(last_updated_date) <= TRUNC(sysdate-13) GROUP BY a.code, a.name UNION SELECT a.code AS Code, a.name AS Name, COUNT(b.Ncode) FROM cdmaster a, nmmaster b WHERE a.code = b.code AND a.status = 1 AND b.status = 1 AND b.Ncode <> 'a10' AND TRUNC(last_updated_date) > TRUNC(sysdate-13) GROUP BY a.code, a.name;The Oracle UNION allows you to perform a count based on one set of criteria. TRUNC(last_updated_date) <= TRUNC(sysdate-13)As well as perform a count based on another set of criteria. TRUNC(last_updated_date) > TRUNC(sysdate-13) |