Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (55.6k points)

Can anyone explain Union and Union all in SQL?

1 Answer

0 votes
by (119k points)

Union command returns the distinct values after combining the result-sets of the SELECT statements

Suppose you have two tables Table A and Table B as follows:

Table t1

Table t2

SELECT v1

FROM t1

UNION

SELECT v2

FROM t2;

If you run this above query, you will get the following output

 

Union All command return all the values (allows duplicate values) after combining the result-set of the SELECT statements.

SELECT v1

FROM t1

UNION ALL

SELECT v2

FROM t2;

If you run this query to join tables using UNION ALL, you will get the following output:

Compare both the results set for queries using UNION and UNION ALL operators. You can observe UNION returns only distinct values whereas UNION ALL allows duplicate values also.

You can go through SQL Interview Questions  blog by Intellipaat to understand more about Union and Union All operators in SQL.

Related questions

+3 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Apr 28, 2020 in SQL by Sudhir_1997 (55.6k points)
0 votes
1 answer
asked May 4, 2020 in SQL by Sudhir_1997 (55.6k points)
0 votes
1 answer
asked Jul 12, 2019 in SQL by Tech4ever (20.3k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...