SQL UNION is used to combine the result sets of two or more SELECT Statements, provided they satisfy the below conditions:

  • Each SELECT statement within UNION should have the equal number of columns

  • The columns must also have similar data types

  • The columns in each SELECT statement must also be in the same order

SYNTAX

142.png

Where,

  • UNION : KEYWORD used to combine the result set of both the tables

 

Difference between UNION AND UNION ALL

  • UNION removes duplicate rows.

  • UNION ALL does not remove duplicate rows.

SYNTAX

143.png
 

EXAMPLE

Let’s consider a Demo Database with Table Individual and Address

144.png

Figure 37.1. Individual TABLE

145.png

Figure 37.2. Address TABLE

UNION QUERY

146.png

Figure 37.3. UNION OUTPUT

 

UNION ALL QUERY

147.png

Figure 37.4. UNION ALL OUTPUT

 
 

PRACTICE YOURSELF

148.png