SQL UNION CLAUSE AND UNION ALL CLAUSE – SQL TUTORIAL | Software Testing Material

SQL UNION Clause:

SQL UNION operator combines the result of two or more SELECT statements.

The SQL UNION operator selects only distinct values by default.

Syntax:

Example:

In the below image, you could find two three SQL statements. First two statements returns the records from tables PLAYERS and SCOREBOARD individually. In the third query, we have used UNION clause with the first two queries. UNION clause combines the first two queries and returns only the distinct values. In the result you could see only the values which are not duplicated.

SQL Union Clause

SQL UNION ALL Clause :

SQL UNION operator combines the result of two or more SELECT statements. If we want to include the duplicate values too then we could use UNION ALL operator.

Syntax:

Example:

In the below image, you could find two three SQL statements. First two statements returns the records from tables PLAYERS and SCOREBOARD individually. In the third query, we have used UNION ALL clause with the first two queries. UNION ALL clause combines the first two queries and returns the values from both the tables which includes duplicate values too (if any).

SQL Union All

In the next tutorial, we will see ORDER BY keyword in SQL

Check out the complete SQL Tutorial by clicking on below link:

SQL Tutorial – Complete

Rajkumar SM is a founder of SoftwareTestingMaterial. He is a certified Software Test Engineer by profession and blogger & youtuber by choice. He has an extensive experience in the field of Software Testing. He writes here about Software Testing which includes both Manual and Automation Testing. He loves to be with his wife and cute little kid 'Freedom'.

Leave a Comment

Share via
Copy link
Powered by Social Snap