Union Operator in SQLite

SQLite Union Operator To combine the result set of two or more tables, the SQLite UNION Operator is used with the SELECT statement. The result, however, includes only the unique rows and all duplicate rows are eliminated. It is mandatory to have the same number of fields in the result set in each SELECT statement.

Syntax:

SELECT expression1, expression2, ... expression_n  
FROM tables  
WHERE conditions
UNION  
SELECT expression1, expression2, ... expression_n  
FROM tables  
WHERE conditions;   

Example: STUDENTS Table:

ID NAME SUBJECT
1 Tom German
2 Jerry Spanish
3 Bruno English

TEACHERS Table:

ID NAME SALARY SUBJECT
1 Jim 10000 Geology
2 John 20000 Geology
3 Watson 15000 Physics
4 Holmes 25000 Chemistry
5 Tony 30000 Physics
SELECT SUBJECT FROM STUDENTS  
UNION  
SELECT SUBJECT FROM TEACHERS;

Output:

SUBJECT
German
Spanish
English
Geology
French
Chemistry
Physics

Explanation: In the above example, a union of the SUBJECT columns of the STUDENTS and the TEACHERS table is fetched.

Please follow and like us:
Content Protection by DMCA.com