Sunday, September 4, 2011

Difference between UNION and UNION ALL

Both UNION and UNION ALL statements can be used in SELECT clause to retrieve a set of records which is in a given column in both tables.

For example, consider the following example:

















Here, you can see that there are two tables with a column which is of same data type, StudentName and EmployeeName.

Say we want to find out both the students and employees in this organization, we can use UNION clause and it will return a distinct set of student and employees. If a person is a student as well as an employee, his/her name will appear only once.

But if you use UNION ALL, that person's name will appear twice.

See the results below:

No comments:

Post a Comment