0% found this document useful (0 votes)
98 views

SQL UNION Operator

The SQL UNION operator combines the results of two or more SELECT statements and returns only distinct values by default. It can be used to combine columns from multiple tables as long as the number of columns and data types are the same in each SELECT statement. The UNION operator differs from JOINs in that it combines the results of tables rather than matching records between tables.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

SQL UNION Operator

The SQL UNION operator combines the results of two or more SELECT statements and returns only distinct values by default. It can be used to combine columns from multiple tables as long as the number of columns and data types are the same in each SELECT statement. The UNION operator differs from JOINs in that it combines the results of tables rather than matching records between tables.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

SQL UNION Operator

SQL UNION operator is used to combine the results of two or more SELECT statements.
Each SELECT statement within the UNION must have the same number of columns and all
columns should have same order with similar data types. Union operator is
different JOINs which is also used to combine results of two or more tables.
 

Syntax
SELECT column1, column2, column3,…columnN FROM tableName1
UNION
SELECT column1, column2, column3,…columnN FROM tableName2
Here is our first table Student.
We have second table as NewStudents.

Now, we will use UNION operator to combine records of both


the tables Student & NewStudents.
So, here is the output.

SELECT *FROM Student


UNION
SELECT *FROM NewStudents

Similarly, you can combine specific columns columns from two tables as given below.

SELECT RollNumber, FirstName, Country, Fees FROM Student


UNION
SELECT RollNumber, FirstName, Country, Fees FROM NewStudents
Note: SQL UNION operator returns only distinct values by default. To allow
all duplicate values, you can use UNION ALL operator.

You might also like