Oracle UNION ALL

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

In Oracle, the UNION ALL operator is used to combine the result sets of 2 or more SELECT statements. It is different from UNION operator in a way that it does not remove duplicate rows between the various SELECT statements. It returns all of the rows.

Each SELECT statement within the UNION ALL must have the same number of fields in the result sets with similar data types.

Difference between UNION and UNION ALL operators

UNION operator removes duplicate rows while UNION ALL operator does not remove duplicate rows.

Syntax

  1. SELECT expression1, expression2, ... expression_n  
  2. FROM table1  
  3. WHERE conditions  
  4. UNION ALL  
  5. SELECT expression1, expression2, ... expression_n  
  6. FROM table2  
  7. WHERE conditions;   

Parameters

1) expression1, expression2, expression_n: It specifies the columns that you want to retrieve. Number of expressions must be same in both SELECT statements.

2) table1, table2: It specifies the table name that you want to retrieve records from.

3) conditions: It specifies the conditions that must be fulfilled for the records to be selected.

Oracle UNION ALL Operator Example

  1. SELECT supplier_id  
  2. FROM suppliers  
  3. UNION ALL  
  4. SELECT supplier_id  
  5. FROM order_details;  

The above example will return the supplier_id multiple times in the result set if the same value appeared in both the supplier_id and order_details table.

Output

Oracle Union All 1