SQL Tutorial

SQL Tutorial SQL Introduction SQL Syntax SQL Data Types SQL OPERATORS SQL COMMANDS SQL Queries What are single row and multiple row subqueries? SQL Union Clause

SQL Database

SQL Create Database SQL DROP Database SQL SELECT Database

SQL Table

SQL TABLE SQL CREATE TABLE SQL COPY TABLE SQL ALTER TABLE SQL DELETE SQL TRUNCATE TABLE SQL DROP TABLE SQL UPDATE TABLE SQL INSERT TABLE

SQL SELECT

SQL SELECT Statement SQL SELECT WHERE Clause SQL SELECT IN Operator SQL BETWEEN Operator SQL SELECT BETWEEN Operator SQL SELECT AND Operator SQL SELECT OR Operator SQL SELECT LIKE Operator SQL SELECT DISTINCT SQL SELECT SUM SQL SELECT MAX SQL SELECT MIN SQL SELECT AVG

SQL Clause

SQL WHERE Clause SQL GROUP BY CLAUSE SQL ORDER BY Clause SQL HAVING Clause

SQL INSERT

SQL INSERT Statement SQL INSERT INTO Statement SQL INSERT INTO Values SQL INSERT INTO SELECT SQL Insert multiple rows

SQL JOIN

SQL JOIN SQL Inner Join SQL Left Join SQL Right Join SQL Full Join SQL CROSS Join

SQL OPERATOR

SQL Comparison SQL LOGICAL Operator SQL Cast Operator SQL Arithmetic

Difference

SQL vs NOSQL WHERE vs HAVING DELETE vs DROP GROUP BY vs ORDER BY DROP vs TRUNCATE SQL IN vs SQL EXISTS Difference between Delete, Drop and Truncate in SQL

MISC

SQL SubQuery SQL CASE Commit and Rollback in SQL Pattern Matching in SQL DDL Commands in SQL DML Commands in SQL Types of SQL Commands SQL COUNT SQL Primary Key SQL FOREIGN KEY SET Operators in SQL Check Constraint in SQL SQL EXCEPT SQL VIEW SQL WHERE Statement SQL CRUD Operation Where Condition in SQL TCL Commands in SQL Types of SQL JOINS SQL Nth Highest Salary SQL NOT OPERATOR SQL UNION ALL SQL INTERSECT SQL Data Definition Language SQL Data Manipulation Language SQL Data Control Language SQL CONSTRAINTS SQL Aggregate Operators SQL KEYS Codd’s Rules in SQL What is SQL Injection? Trigger In SQL SQL WHERE Multiple Conditions Truncate function in SQL SQL Formatter WEB SQL SQL Auto Increment Save Point in SQL space() function in SQL SQL Aggregate Functions SQL Topological Sorting SQL Injection SQL Cloning Tables SQL Aliases SQL Handling Duplicate Update Query in SQL Grant Command in SQL SQL SET Keyword SQL Order BY LIMIT SQL Order BY RANDOM

How To

How to use the BETWEEN operator in SQL How To Use INNER JOIN In SQL How to use LIKE in SQL How to use HAVING Clause in SQL How to use GROUP BY Clause in SQL How To Remove Duplicates In SQL How To Delete A Row In SQL How to add column in table in SQL ? How to drop a column in SQL? How to create a database in SQL? How to use COUNT in SQL? How to Create Temporary Table in SQL? How to Add Foreign Key in SQL? How to Add Comments in SQL? How To Use Group By Clause In SQL How To Use Having Clause In SQL How To Delete Column In Table How To Compare Date In SQL How index works in SQL How to calculate age from Date of Birth in SQL How to Rename Column name in SQL How to get current year in SQL server 2012? User Input in PL/SQL

SQL Union Clause

The SQL UNION clause merges the results of several select queries into a single result set. Unique values are produced using the UNION clause. Use the UNION ALL clause to produce duplicate data values.

Note:

  1. In this instance, the number of columns and the order of the columns in each SELECT query within the UNION statement must match.
  2. The data type of each column must also be consistent in all SELECT queries. For instance, if column 1 of select query 1 is varchar, then column 1 of select query 2 must also be varchar.

Syntax:

SELECT column1, column2, column3,….

FROM table1

WHERE condition

UNION

SELECT column1, column2, column3,…

FROM table2

WHERE  condition

Examples of UNION operators:

Let's now create three tables.

  1. doctors
  2. inpatient
  3. outpatient

Use the DDL command "create" to create the table and the DML command "insert" to add values.

1. Creating “doctors” table:

create table doctors(doc_id int primary key, doc_name varchar(25), doc_dept varchar(25), doc_age int);

Inserting values into “doctors” table:

insert into doctors values(1001,"james","pediatrician ",45);

insert into doctors values(1002,"michael","pulmonologist",39);

insert into doctors values(1003,"william","dermatologist",42);

insert into doctors values(1004,"jack","cardiologist",48);

insert into doctors values(1005,"daniel","general surgeon",34);
SQL Union Clause

2. Creating an "inpatient" table:

create table inpatient(in_id int primary key, in_name varchar(25), in_age int, in_gender varchar(10), in_doc int, foreign key(in_doc) references doctors(doc_id));

Inserting values into the "inpatient" table:

insert into inpatient values(1101, "joseph",20, "M",1003);

insert into inpatient values(1102, "elizabeth",13, "F",1001);

insert into inpatient values(1103, "george",42, "M",1004);

insert into inpatient values(1104, "john",08, "M",1001);

insert into inpatient values(1105, "emily",38, "F",1002);
SQL Union Clause

3. Creating an "outpatient" table:

create table outpatient(out_id int primary key, out_name varchar(25), out_age int, out_gender varchar(10), out_doc int, foreign key(out_doc) references doctors(doc_id));

Inserting values into the "outpatient" table:

insert into outpatient values(1201, "robert",25, "M",1003);

insert into outpatient values(1202, "lily",12, "F",1001);

insert into outpatient values(1203, "martin",56, "M",1004);

insert into outpatient values(1204, "charles",65, "M",1002);

insert into outpatient values(1205, "emma",07, "F",1001);
SQL Union Clause

Here are some SQL queries that use the UNION operator:

1. The following SQL query returns the distinct doctor IDs from the tables "inpatient" and "outpatient".

select i.in_doc from inpatient i union select o.out_doc from outpatient o;
SQL Union Clause

2. The following SQL query returns the inpatient and outpatient names from the tables “inpatient” and “outpatient”.

select i.in_name as name

from inpatient i

union

select o.out_name

from outpatient o;
SQL Union Clause

3. The following SQL query returns the patient names and ages (both inpatient and outpatient) from the tables "inpatient" and "outpatient".

select i.in_name as name, i.in_age as age

from inpatient i

union

select o.out_name, out_age

from outpatient o;
SQL Union Clause

4. The following SQL query returns the female patient names and ages (both inpatients and outpatients) from the tables "inpatient" and "outpatient".

select i.in_name as name, i.in_age as age

from inpatient i

where in_gender="F"

union

select o.out_name, o.out_age

from outpatient o

where out_gender="F";
SQL Union Clause

5. The following SQL query returns the patient names ( both inpatients and outpatients) who are consulting a pediatrician from tables "inpatient" and "outpatient".

select i.in_name as name

from inpatient i

where i.in_doc in(select d.doc_id from doctors d where doc_dept="pediatrician ")

union

select o.out_name

from outpatient o

where o.out_doc in(select d.doc_id from doctors d where doc_dept="pediatrician ");\
SQL Union Clause

6. The following SQL query returns the inpatient names consulting pediatricians and dermatologists from the table "inpatient".

select i.in_name as name

from inpatient i

where i.in_doc in(select d.doc_id from doctors d where doc_dept="pediatrician ")

union

select i.in_name

from inpatient i

where i.in_doc in(select d.doc_id from doctors d where doc_dept="dermatologist");
SQL Union Clause

7. The following SQL query returns the patient names (both inpatients and outpatients) and ages of patients whose age is less than 20 years and who are consulting a pediatrician from tables "inpatient" and "outpatient".

select i.in_name, i.in_age

from inpatient i

where i.in_age<20 and i.in_doc in(select doc_id from doctors where doc_dept="pediatrician ")

union

select o.out_name,o.out_age

from outpatient o

where o.out_age<20 and o.out_doc in(select doc_id from doctors where doc_dept="pediatrician ");
SQL Union Clause

8. The following SQL query returns the names and ages of inpatients whose age is less than 20 years and who are consulting pediatricians and dermatologists from table "inpatient".

select i.in_name as name, i.in_age as age

from inpatient i

where i.in_age<20 and i.in_doc in(select d.doc_id from doctors d where doc_dept="pediatrician ")

union

select i.in_name, i.in_age

from inpatient i

where i.in_age<20 and i.in_doc in(select d.doc_id from doctors d where doc_dept="dermatologist");
SQL Union Clause

9. The following SQL query returns the male patient names and IDs (both inpatients and outpatients) who are consulting a cardiologist from tables "inpatient" and "outpatient". 

select i.in_id, i.in_name

from inpatient i

where i.in_gender="M" and i.in_doc in(select d.doc_id from doctors d where doc_dept="cardiologist")

union

select o.out_id, o.out_name

from outpatient o

where o.out_gender="M" and o.out_doc in(select d.doc_id from doctors d where doc_dept="cardiologist");
SQL Union Clause

10. The following SQL query returns patient names (both inpatients and outpatients), patient ids, consulting doctor ids, and the age of patients whose age is greater than 30 years and who is consulting a cardiologist from tables "inpatient" and "outpatient".

select i.in_id as id, i.in_name as name, i.in_doc as doc, i.in_age as age

from inpatient i

where i.in_age>30 and i.in_doc in(select d.doc_id from doctors d where doc_dept="cardiologist")

union

select o.out_id, o.out_name, o.out_doc, o.out_age

from outpatient o

where o.out_age>30 and o.out_doc in(select d.doc_id from doctors d where doc_dept="cardiologist");
SQL Union Clause

Example of UNION ALL operator:

The following SQL query returns all doctor IDs from the tables "inpatient" and "outpatient".

select i.in_doc from inpatient i union all select o.out_doc from outpatient o;
SQL Union Clause