Summary: In this tutorial, you will learn how to use MySQL UNION statement to combine two or more result sets from multiple SQL SELECT statements into a single result set.

Like SQL standard, MySQL UNION allows you to combine two or more result sets from multiple tables together. The syntax of using MySQL UNION is as follows:

1 SELECT statement
2 UNION [DISTINCT | ALL]
3 SELECT statement
4 UNION [DISTINCT | ALL]
5
6

In order to use UNION statement, there are some rules you need to follow:

  • The number of columns in each SELECT statement has to be the same .
  • The data type of the column in the column list of the SELECT statement must be the same or at least convertible.

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 is no duplicate rows in the result set.

Let’s practice with couples of examples with MySQL UNION to get a better understanding.

Suppose you want to combine customers and employees infomation into one result set, you use the following query:

1 SELECT customerNumber id, contactLastname name
2 FROM customers
3 UNION
4 SELECT employeeNumber id,firstname name
5 FROM employees

Here is the excerpt of the output:

    id  name
------  ---------------
   103  Schmitt
   112  King
   114  Ferguson
   119  Labrune
   121  Bergulfsen
   124  Nelson
   125  Piestrzeniewicz
   128  Keitel
   129  Murphy
   131  Lee

When using ORDER BY to sort the result with UNION, you have to use it in the last SQL SELECT statement. It would be the best to parenthesize all the SELECT statements and place ORDER BY at the end.

Suppose you want to sort the combination of employees and customers in the query above by name and ID in ascending order.

1 (SELECT customerNumber id,contactLastname name
2 FROM customers)
3 UNION
4 (SELECT employeeNumber id,firstname name
5 FROM employees)
6 ORDER BY name,id

What will be displayed in the output if we don’t use alias for each column in the SELECT statements? MySQL will use the column names of the first SELECT statement as the label of the output.

1 (SELECT customerNumber, contactLastname
2 FROM customers)
3 UNION
4 (SELECT employeeNumber, firstname
5 FROM employees)
6 ORDER BY contactLastname, customerNumber

MySQL also provides you another option to sort the result set based on column position in the ORDER BY clause as the following query:

view sourceprint?
1 (SELECT customerNumber, contactLastname
2 FROM customers)
3 UNION
4 (SELECT employeeNumber,firstname
5 FROM employees)
6 ORDER BY 2, 1

Related posts:

  1. Using MySQL SELECT Statement to Query Data
  2. How to Use MySQL Distinct to Eliminate Duplicate Rows
  3. How to Use MySQL LIKE to Select Data Based on Patterns Matching
  4. Working with Database Table – Part I
  5. How to Use MySQL Limit to Constrain Number of Returned Records

Leave your comment

You must be logged in to post a comment.


Link Exchange Spritual Place for Bhakti | bhaktiguru.com Business Franchise in India | businessfranchiseindia.com Free Directory Submission & Link Submission | ab-directory.com Big Dwarka Business Listing | bigdwarka.com Kolkata Online Business Listing | onlinekolkata.com Patna Online Business Listing | onlinepatna.com Dwarka Online Business Listing | onlinedwarka.com Bihar Online | First Bihar's Portal | bihar-online.com Big Bihar Business Listing | bigbihar.com Dynamic Website Development | dynamicwebsitedeveloper.com Noida Online Business Listing | onlinenoida.com SEO Service Delhi & India | seoservicedelhi.com SEO Experts Delhi & India | seoexpertsdelhi.com Big Patna Business Listing | bigpatna.com Bihar schools Listing Free Schools Ad | biharschools.com Creative Websoft Solutions | Web Development India | creativewebsoft.us Bulk SMS Providers India | bulksmsproviders.net Online cbse Guides | onlinecbseguides.com Big Kolkata Business Listing | bigkolkata.com Handicap Shaadi.com | handicapshaadi.com Handicap Marriage.com | handicapmarriage.com India Home Tutors.com | indiahometutors.com Indian Property Guides.com | indianpropertyguides.com Online Stocks Community.com | onlinestockscommunity.com Free Article Submission | ab-articles.com Secret Dating Online.com | secretdatingonline.com Private DatingOnline.com | privatedatingonline.com Indian businessguides.com | indianbusinessguides.com Software Engineer Delhi | Meenu Khanna Bollywood & Hot Images | hibdy.com Free Computer & Online PHP Training | php2php.com Free Ignou Project & Assignment | ignouinfo.com PHP Programmer India | Web Developer India | bageshsingh.com Creative Websoft Solutions | Web Development IndiaBlog Link Exchange http://www.php2php.com/tutorial-blog/http://bageshsingh.com/bagesh-blog/http://www.bhaktiguru.com/bhaktigurublog/http://www.hibdy.com/desi-masala-blog/