15 February 2012 By Praveen Macherla 421 views No Comment
0 Flares Twitter 0 Facebook 0 Google+ 0 0 Flares ×

UNION is used to combine the result from multiple SELECT statements into a single result set.

The column names from the first SELECT statement are used as the column names for the results returned. Selected columns listed in corresponding positions of each SELECT statement should have the same data type. (For example, the first column selected by the first statement should have the same type as the first column selected by the other statements.)

The union syntax is as follows.

SELECT statement
SELECT statement

By default the MySQL UNION removes all duplicate rows from the result set even if you don’t explicit using DISTINCT after the keyword UNION.

If you use UNION ALL explicitly, the duplicate rows remain in the result set. You only use this in the cases that you want to keep duplicate rows or you are sure that there are no duplicate rows in the result set.


(SELECT ssn, fname FROM employee)
(SELECT sid, first_name FROM student);

Tags: ,