Monday, February 8, 2010

SQL UNION - Introduction and syntax

Explain the SQL UNION statement. Write SQL syntax for the SQL UNION statement along with an example.

SQL UNION: The UNION operator is used to combine the result-set of two or more SELECT statements Tables of both the select statement must have the same number of columns with similar data types. It eliminates duplicates.

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

Example:
SELECT emp_Name FROM Employees_india UNION SELECT emp_Name FROM Employees_USA

What is the difference between UNION and UNION ALL?

UNION command selects distinct and related information from two tables. On the other hand, UNION ALL selects all the values from both the tables.

No comments:

Post a Comment