SQL UNION CLAUSE AND UNION ALL CLAUSE – SQL TUTORIAL

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

Get our latest blog posts delivered to your inbox

Subscribe and get popular blog posts about software testing industry.

Rajkumar

Leave a Comment

Share via
Copy link
Powered by Social Snap