Overview of SQL UNION

This article will provide a deep dive into the SQL UNION operator, describing its many uses along with examples and explore some common questions like the differences between UNION vs UNION ALL.
To address real-world data requirements, we may need to combine result sets from multiple data sources so that we could do data analysis or create new datasets. The datasets may be identical but there are chances that they reference different tables. Is there a way to combine the data in a single query? Are Set Operators a viable option? Let’s get started and see how some of the existing operators can be used to help us address these common challenges.

In this article, we’ll review:

  1. What a Set operator is
  2. UNION vs UNION all and how they work
  3. Discuss the rules for using UNION vs UNION ALL
  4. SQL Operator Syntax
  5. How to use simple SQL UNION clause in the select statement
  6. How to use SQL UNION with the queries that have the WHERE clause
  7. How to use the SELECT INTO clause with UNION
  8. How to use SQL UNION with the queries that have a WHERE clause and order by clause
  9. How to use SQL UNION and SQL Pivot
  10. How to use SQL UNION with GROUP and HAVING clauses

 

Continue reading…

SQL Union 

 

Advertisements

About Prashanth Jayaram

DB Technologist, Author, Blogger, Service Delivery Manager at CTS, Automation Expert, Technet WIKI Ninja, MVB and Powershell Geek My Profile: https://social.technet.microsoft.com/profile/prashanth jayaram/ http://www.sqlshack.com/author/prashanth/ http://codingsight.com/author/prashanthjayaram/ https://www.red-gate.com/simple-talk/author/prashanthjayaram/ http://www.sqlservercentral.com/blogs/powersql-by-prashanth-jayaram/ Connect Me: Twitter @prashantjayaram GMAIL powershellsql@gmail.com The articles are published in: http://www.ssas-info.com/analysis-services-articles/ http://db-pub.com/ http://www.sswug.org/sswugresearch/community/
This entry was posted in T-SQL and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s