Questions And Answers

More Tutorials


Combining SELECT statements with UNION

You can combine the results of two identically structured queries with the UNION keyword.

For example, if you wanted a list of all contact info from two separate tables, authors and editors, for instance, you could use the UNION keyword like so:

select name, email, phone_number
from authors
select name, email, phone_number
from editors

Using union by itself will strip out duplicates. If you needed to keep duplicates in your query, you could use the ALL keyword like so: UNION ALL.

Combining data with dierent columns

SELECT name, caption as title, year, pages FROM books
SELECT name, title, year, 0 as pages FROM movies

When combining 2 record sets with different columns then emulate the missing ones with default values.


If you need to sort the results of a UNION, use this pattern:

( SELECT ... )
( SELECT ... )

Without the parentheses, the final ORDER BY would belong to the last SELECT.


In this page (written and validated by ) you learned about MYSQL UNION . What's Next? If you are interested in completing MYSQL tutorial, your next topic will be learning about: MYSQL Arithmetic.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.