SITE SEARCH

SITE LINKS

    • SQL Basic
  • SQL HOME
  • SQL Intro
  • SQL Syntax
  • SQL Select
  • SQL Distinct
  • SQLWhere
  • SQL And & Or
  • SQL Order By
  • SQL Insert
  • SQL Update
  • SQL Delete

 

  • SQL Advanced
  • SQL Top
  • SQL Like
  • SQL Wildcards
  • SQL In
  • SQL Between
  • SQL Alias
  • SQL Joins
  • SQL Inner Join
  • SQL Left Join
  • SQL Right Join
  • SQL Full Join
  • SQL Union
  • SQL Select Into
  • SQL Create DB
  • SQL Create Table
  • SQL Constraints
  • SQL Not Null
  • SQL Unique
  • SQL Primary Key
  • SQL Foreign Key
  • SQL Check
  • SQL Default
  • SQL Create Index
  • SQL Drop
  • SQL Alter
  • SQL Increment
  • SQL Views
  • SQL Dates
  • SQL Nulls
  • SQL isnull()
  • SQL Data Types

  • SQL functions
  • SQL Functions
  • SQL avg()
  • SQL count()
  • SQL first()
  • SQL last()
  • SQL max()
  • SQL min()
  • SQL sum()
  • SQL Group By
  • SQL Having
  • SQL ucase()
  • SQL lcase()
  • SQL mid()
  • SQL len()
  • SQL round()
  • SQL now()
  • SQL format()

 

  • SQL Quick Ref
  • SQL Hosting
  • SQL Summary

Home Html Css Javascript Php Asp .Net Sql Xml
user login New user? Register Here | Forgot Password?

SQL UNION Operator

Prev
Next

The SQL UNION operator combines two or more SELECT statements.

The SQL UNION Operator

The UNION operator is used to combine the result-set of two or more SELECT statements.

Notice that each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types. Also, the columns in each SELECT statement must be in the same order.

SQL UNION Syntax

SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2

PS: In some databases RIGHT JOIN is called RIGHT OUTER JOIN.

Note: The UNION operator selects only distinct values by default. To allow duplicate values, use UNION ALL.

SQL UNION ALL Syntax

SELECT column_name(s) FROM table_name1
UNION ALL
SELECT column_name(s) FROM table_name2

PS: The column names in the result-set of a UNION are always equal to the column names in the first SELECT statement in the UNION.

SQL UNION Example

Look at the following tables:

"Employees_Norway":

E_ID E_Name
01 Hansen,Ola
02 Svendson, Tove
03 Svendson, Stephen
04 Pettersen, Kari

"Employees_USA":

E_ID E_Name
01 Turner, Sally
02 Kent, Clark
03 Svendson, Stephen
04 Scott, Stephen

Now we want to list all the different employees in Norway and USA.

We use the following SELECT statement:

SELECT E_Name FROM Employees_Norway
UNION
SELECT E_Name FROM Employees_USA

The result-set will look like this:

E_Name
Hansen,Ola
Svendson, Tove
Svendson, Stephen
Pettersen, Kari
Turner, Sally
Kent, Clark
Scott, Stephen

Note: This command cannot be used to list all employees in Norway and USA. In the example above we have two employees with equal names, and only one of them will be listed. The UNION command selects only distinct values.

SQL UNION ALL Example

Now we want to list all employees in Norway and USA:

SELECT E_Name FROM Employees_Norway
UNION ALL
SELECT E_Name FROM Employees_USA

Result

E_Name
Hansen,Ola
Svendson, Tove
Svendson, Stephen
Pettersen, Kari
Turner, Sally
Kent, Clark
Svendson, Stephen
Scott, Stephen

Prev
Next
Top
© 2010 Copyrighted. Yavum.com™