Course Content
What is Oracle
0/1
Oracle Tables
0/1
CREATE TABLE AS in Oracle
0/1
ALTER TABLE In Oracle
0/1
DROP TABLE In Oracle
0/1
TRUNCATE TABLE In Oracle
0/1
QUERIES In Oracle
0/1
INSERT Query In Oracle
0/1
SELECT Query In Oracle
0/1
UPDATE Query In Oracle
0/1
DELETE Query In Oracle
0/1
INSERT ALL In Oracle
0/1
GROUP BY Clause In Oracle
0/1
HAVING Clause In Oracle
0/1
ORDER BY Clause In Oracle
0/1
MINUS In Oracle
0/1
INTERSECT Operator In Oracle
0/1
TRIGGER In Oracle
0/1
AFTER TRIGGER In Oracle
0/1
BEFORE TRIGGER In Oracle
0/1
ENABLE TRIGGER In Oracle
0/1
DISABLE TRIGGER In Oracle
0/1
DROP TRIGGER In Oracle
0/1
CURSOR In Oracle
0/1
VIEW In Oracle
0/1
DISTINCT Clause In Oracle
0/1
PROCEDURE In Oracle
0/1
UNION In Oracle
0/1
UNION ALL In Oracle
0/1
OUTER JOIN In Oracle
0/1
INNER JOIN In Oracle
0/1
CROSS JOIN In Oracle
0/1
EQUI JOIN In Oracle
0/1
SEMI JOIN In Oracle
0/1
SELF JOIN In Oracle
0/1
ANTI JOIN In Oracle
0/1
ROWNUM In Oracle
0/1
SQLERRM Function In Oracle
0/1
SQLCODE Function In Oracle
0/1
USERENV Function In Oracle
0/1
ACOS Function In Oracle
0/1
ADD_MONTHS Function In Oracle
0/1
ASCII Function In Oracle
0/1
ASCIISTR Function In Oracle
0/1
ASIN Function In Oracle
0/1
ATAN Function In Oracle
0/1
ATAN2 Function In Oracle
0/1
AVG Function In Oracle
0/1
BFILENAME Function In Oracle
0/1
ABS Function In Oracle
0/1
BIN_TO_NUM Function In Oracle
0/1
BITAND Function In Oracle
0/1
CARDINALITY Function In Oracle
0/1
CASE Function In Oracle
0/1
CAST Function In Oracle
0/1
CEIL Function In Oracle
0/1
CHARTOROWID Function In Oracle
0/1
CHR Function In Oracle
0/1
COALESCE Function In Oracle
0/1
COMPOSE Function In Oracle
0/1
CONCAT With Function In Oracle
0/1
CONCAT Function In Oracle
0/1
CONVERT Function In Oracle
0/1
CORR Function In Oracle
0/1
COS Function In Oracle
0/1
COSH Function In Oracle
0/1
COUNT Function In Oracle
0/1
COVAR_POP Function In Oracle
0/1
COVAR_SAMP Function In Oracle
0/1
CUME_DIST Function In Oracle
0/1
CURRENT_DATE In Oracle
0/1
CURRENT_TIMESTAMP Function In Oracle
0/1
DBTIMEZONE Function In Oracle
0/1
DECODE Function In Oracle
0/1
DECOMPOSE Function In Oracle
0/1
DENSE_RANK Function In Oracle
0/1
DUMP Function In Oracle
0/1
EMPTY_BLOB Fuction In Oracle
0/1
EMPTY_CLOB Function In Oracle
0/1
EXP Function In Oracle
0/1
EXTRACT Function In Oracle
0/1
FIRST_VALUE Function In Oracle
0/1
FLOOR Function In Oracle
0/1
FROM_TZ Function In Oracle
0/1
GLOBAL TEMP TABLES In Oracle
0/1
GREATEST Function In Oracle
0/1
GROUP_ID Function In Oracle
0/1
HEXTORAW Function In Oracle
0/1
INITCAP Function In Oracle
0/1
INSTR Function In Oracle
0/1
INSTR2 Function In Oracle
0/1
VARIANCE Function In Oracle
0/1
VAR_SAMP Function In Oracle
0/1
VAR_POP Function In Oracle
0/1
USER Function In Oracle
0/1
UID Function In Oracle
0/1
TZ_OFFSET Function In Oracle
0/1
TRUNC (Numbers) Function In Oracle
0/1
TRUNC (Dates) Function In Oracle
0/1
TO_YMINTERVAL Function In Oracle
0/1
TO_TIMESTAMP_TZ Function In Oracle
0/1
TO_TIMESTAMP Function In Oracle
0/1
TO_SINGLE_BYTE Function In Oracle
0/1
TO_NUMBER Function In Oracle
0/1
TO_NCLOB Function In Oracle
0/1
TO_MULTI_BYTE Function In Oracle
0/1
TO_LOB Function In Oracle
0/1
TO_DSINTERVAL Function In Oracle
0/1
TO_DATE Function In Oracle
0/1
TO_CLOB Function In Oracle
0/1
TO_CHAR Function In Oracle
0/1
TANH Function In Oracle
0/1
TAN Function In Oracle
0/1
SYSTIMESTAMP Function In Oracle
0/1
SYSDATE Function In Oracle
0/1
SYS_CONTEXT Function In Oracle
0/1
SQRT Function In Oracle
0/1
SESSIONTIMEZONE Function In Oracle
0/1
LOG Function In Oracle
0/1
LAST_DAY Function In Oracle
0/1
LEAST Function In Oracle
0/1
LENGTH Function In Oracle
0/1
LENGTH2 Function In Oracle
0/1
INSTR4 Function In Oracle
0/1
INSTRB Function In Oracle
0/1
INSTRC Function In Oracle
0/1
LAG Function In Oracle
0/1
LAST_VALUE Function In Oracle
0/1
LEAD Function In Oracle
0/1
LENGTH4 Function In Oracle
0/1
LENGTHB Function In Oracle
0/1
LENGTHC Function In Oracle
0/1
LISTAGG Function In Oracle
0/1
LN Function In Oracle
0/1
LNNVL Function In Oracle
0/1
LOCAL TEMP TABLES In Oracle
0/1
LOCALTIMESTAMP Function In Oracle
0/1
MAX Function In Oracle
0/1
MEDIAN Function In Oracle
0/1
MIN Function In Oracle
0/1
MOD Function In Oracle
0/1
MONTHS_BETWEEN Function In Oracle
0/1
NANVL Function In Oracle
0/1
NEW_TIME Function In Oracle
0/1
NEXT_DAY Function In Oracle
0/1
NULLIF Function In Oracle
0/1
NUMTODSINTERVAL Function In Oracle
0/1
NUMTOYMINTERVAL Function In Oracle
0/1
NVL Function In Oracle
0/1
NVL2 Function In Oracle
0/1
POWER Function In Oracle
0/1
STDDEV Function In Oracle
0/1
NTH_VALUE Function In Oracle
0/1
SINH Function In Oracle
0/1
SIN Function In Oracle
0/1
SIGN Function In Oracle
0/1
ROUND (Numbers) Function In Oracle
0/1
FROM Clause In Oracle
0/1
FUNCTION In Oracle
0/1
SUM Function In Oracle
0/1
ROUND (Dates) Function In Oracle
0/1
REMAINDER Function In Oracle
0/1
JOINS In Oracle
0/1
RANK Function In Oracle
0/1
RAWTOHEX Function In Oracle
0/1
REGEXP_COUNT Function In Oracle
0/1
Oracle Tutorial
About Lesson

UNION In Oracle

ORACLE UNION
To combine the output sets of two or more Oracle SELECT statements, the Oracle UNION operator is used. During the combining process, the UNION operator removes the duplicate rows between the SELECT statements’ results.

There are however two mandatory conditions for using the UNION operator in Oracle.

  • Each SELECT statement must have the same number of expressions.
  • Each corresponding expression in the different SELECT statement should be of the same data type.

Syntax:

SELECT expr_1, expr_2, ... expr_n  
FROM table1  
WHERE conditions  
UNION  
SELECT expr_1, expr_2, ... expr_n  
FROM table2  
WHERE conditions;   

Parameters:
expr_1, expr_2, … expr_n: It is used to specify the columns of the table which needs to be retrieved.
table1, table2: It is used to specify the name of the tables from which the records need to be retrieved.
conditions: It is used to specify the conditions to be strictly followed for selection.

Example 1: Fetching single field from two tables.
Students Table:

STUDENT_ID STUDENT_NAME STUDENT_AGE
1 Joy 10
2 Smiley 13
3 Happy 11
4 James 13
5 Bond 10

Teachers Table:

TEACHER_ID TEACHER_NAME TEACHER_AGE
101 James 30
102 Bond 25
103 Smith 40

Query:

SELECT student_id  as ID
FROM students
UNION  
SELECT teacher_id  as ID
FROM teachers;

Output:

ID
1
2
3
4
5
101
102
103

Explanation:
The ‘students’ and the ‘teachers’ are already existing tables. After the UNION, a combination of student_id and teacher_id would appear, both being the columns of the same data type but of different tables. During this process, the duplicate sets will be removed.

Example 2: Fetching multiple fields from two tables.
Students Table:

STUDENT_ID STUDENT_NAME STUDENT_AGE
1 Joy 10
2 Smiley 13
3 Happy 11
4 James 13
5 Bond 10

Teachers Table:

TEACHER_ID TEACHER_NAME TEACHER_AGE
101 James 30
102 Bond 25
103 Smith 40

Query:

SELECT student_id as ID, student_age as AGE
FROM students
UNION  
SELECT teacher_id as ID, teacher_age as AGE
FROM teachers;

Output:

ID AGE
1 10
2 13
3 11
4 13
5 10
101 30
102 25
103 40

Explanation:
The ‘students’ and the ‘teachers’ are already existing tables. After the UNION, a group of the combination of student_id and teacher_id would appear along with another group of the combination of student_age and teacher_age. The corresponding column of the two tables is of the same data type. During this process, the duplicate sets will be removed from the corresponding columns’ result.

Example 3: Fetching multiple fields from two tables with conditions.
Students Table:

STUDENT_ID STUDENT_NAME STUDENT_AGE
1 Joy 10
2 Smiley 13
3 Happy 11
4 James 13
5 Bond 10

Teachers Table:

TEACHER_ID TEACHER_NAME TEACHER_AGE
101 James 30
102 Bond 25
103 Smith 40

Query:

SELECT student_id as ID, student_age as AGE
FROM students
WHERE student_id > 2  
UNION  
SELECT teacher_id as ID, teacher_age as AGE
FROM teachers
WHERE teacher_age >= 30;

Output:

ID AGE
3 11
4 13
5 10
101 30
103 40

Explanation:
The ‘students’ and the ‘teachers’ are already existing tables. After the UNION, a group of the combination of student_id and teacher_id would appear along with another group of the combination of student_age and teacher_age. The corresponding column of the two tables is of the same data type. During this process, the duplicate sets will be removed from the corresponding columns’ result. Here, the Union of the teacher_id column will be done only for the rows of the table ‘students’ with student_id greater than 2 and the Union of the student_age column will be done only for the rows of the table ‘teachers’ with teacher_age greater than 30.

Example 4: Fetching multiple fields from two tables with conditions and using ORDER BY clause.
Students Table:

STUDENT_ID STUDENT_NAME AGE
1 Joy 10
2 Smiley 13
3 Happy 11
4 James 13
5 Bond 10

Teachers Table:

TEACHER_ID TEACHER_NAME AGE
101 James 30
102 Bond 25
103 Smith 40

Query:

SELECT student_name as NAME, age
FROM students 
UNION  
SELECT teacher_name as NAME, age
FROM teachers
WHERE age >= 30 
ORDER BY age;

Output:

NAME AGE
Joy 10
Bond 10
Happy 11
James 13
Smiley 13
Tom 30

Explanation:
The ‘students’ and the ‘teachers’ are already existing tables. After the UNION, a group of the combination of student_id and teacher_id would appear along with another group of the combination of student_age and teacher_age. The corresponding column of the two tables is of the same data type. During this process, the duplicate sets will be removed from the corresponding columns’ result. Here, the Union of the student_age column will be done only for the rows of the table ‘teachers’ with age greater than 30, while there is no condition for the union of teacher_name and student_name. The result will be finally available in the sorted order in the ascending sequence of the ages.

 
 
error: Content is protected !!