TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Oracle Union

Oracle Union Query for beginners and professionals with examples on insert, select, update, delete, table, view, join, key, functions, procedures, indexes, cursor etc.

<< Back to ORACLE

Oracle UNION Operator

In Oracle, UNION operator is used to combine the result sets of two or more Oracle SELECT statements. It combines the both SELECT statement and removes duplicate rows between them./p>

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

Syntax

SELECT expression1, expression2, ... expression_n
FROM table1
WHERE conditions
UNION
SELECT expression1, expression2, ... expression_n
FROM table2
WHERE conditions; 

Parameters

1) expression1, expression2, ... expression_n: It specifies the columns that you want to retrieve.

2) table1, table2: it specifies the tables from where you retrieve the records.

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

Note: The number of expressions must be same in both of the SELECT statements.

Oracle UNION Example: (Fetch single field)

SELECT supplier_id
FROM suppliers
UNION
SELECT supplier_id
FROM order_details

output

Oracle Union

In this example, supplier_id is defined in both of the table "suppliers" and "order_details". After the UNION, it would appear once in the result set because Oracle UNION operator removes duplicate sets.

Note: If you don't want to remove duplicates, use Oracle UNION ALL operator.

Oracle UNION Example: (Using ORDER BY)

The Oracle UNION operator can be used with ORDER BY clause to orders the results of the query.

SELECT supplier_id, supplier_name
FROM suppliers
WHERE supplier_id <= 20
UNION
SELECT s_id, s_name
FROM shopkeepers
WHERE s_name = 'dhirubhai'
ORDER BY 1;

Output

Oracle Union 2

In the above example, result is sorted by supplier_name/s_name in ascending order, as denoted by ORDER BY 1.

Next TopicOracle UNION ALL




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf