<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>union - Techyaz.com</title>
	<atom:link href="https://techyaz.com/tag/union/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>Tips, Tutorials and How-to Topics</description>
	<lastBuildDate>Thu, 22 Jul 2021 08:28:54 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8</generator>

<image>
	<url>https://techyaz.com/wp-content/uploads/2017/11/cropped-Site-icon-150x150.png</url>
	<title>union - Techyaz.com</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Difference between UNION and UNION ALL T-SQL Operators</title>
		<link>https://techyaz.com/sql-server/t-sql/difference-between-union-and-union-all-operators/</link>
					<comments>https://techyaz.com/sql-server/t-sql/difference-between-union-and-union-all-operators/#respond</comments>
		
		<dc:creator><![CDATA[Manvendra Deo Singh]]></dc:creator>
		<pubDate>Wed, 29 Nov 2017 09:55:36 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[T-SQL Operators]]></category>
		<category><![CDATA[union]]></category>
		<guid isPermaLink="false">http://techyaz.com/?p=1269</guid>

					<description><![CDATA[<p>If you want to combine output from multiple tables or queries into one result set, you can do this using an operator known as UNION. A UNION is used to extract rows based on some conditions specified whereas UNION ALL&#46;&#46;&#46;</p>
<p>The post <a href="https://techyaz.com/sql-server/t-sql/difference-between-union-and-union-all-operators/">Difference between UNION and UNION ALL T-SQL Operators</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>If you want to combine output from multiple tables or queries into one result set, you can do this using an operator known as UNION. A UNION is used to extract rows based on some conditions specified whereas UNION ALL will extract all rows from both tables. Here, i will discuss difference between UNION and UNION ALL operators ( UNION vs UNION ALL ) in this article along with their use cases.</p>
<h3><span style="color: #333399;">UNION</span></h3>
<p>The UNION operator allows us to combine the results of two or more SELECT statements into a single result set and removes duplicate rows from that result set. Basically, it is performing a DISTINCT operation across all columns in the result set. The UNION operator is different from using joins that combine columns from two tables.</p>
<p>The following are basic rules for combining the result sets of two queries by using UNION:</p>
<ul>
<li>The number and the order of the columns must be the same in all queries.</li>
<li>The data types must be compatible.</li>
<li>Corresponding result set columns in the individual statements that are being combined with UNION must occur in the same order, because UNION compares the columns one-to-one in the order given in the individual queries.</li>
<li>The column names in the result from UNION are taken from the first individual query in the UNION statement.</li>
</ul>
<p>Below example shows how can you use UNION to combine multiple outputs into one result set.</p>
<pre><strong><span style="color: #0000ff;">SELECT * FROM Table1
<span style="color: #008000;">UNION</span>
SELECT * FROM Table2
<span style="color: #008000;">UNION</span>
SELECT * FROM Table3
</span></strong></pre>
<p>If tables would not have same structure or same data types, you will get below error while using UNION:</p>
<p><img fetchpriority="high" decoding="async" class="aligncenter wp-image-1272 size-full" src="http://techyaz.com/wp-content/uploads/2017/11/Union-Error.jpg" alt="error 205" width="799" height="157" srcset="https://techyaz.com/wp-content/uploads/2017/11/Union-Error.jpg 799w, https://techyaz.com/wp-content/uploads/2017/11/Union-Error-300x59.jpg 300w, https://techyaz.com/wp-content/uploads/2017/11/Union-Error-768x151.jpg 768w" sizes="(max-width: 799px) 100vw, 799px" /></p>
<div>
<p><em><span style="color: #ff0000;">Msg 205, Level 16, State 1, Line 1</span></em><br />
<em><span style="color: #ff0000;"> All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.</span></em></p>
</div>
<h3><span style="color: #333399;">UNION ALL</span></h3>
<p>UNION ALL operator also works like UNION operator that is used to combine multiple result sets into one result set but duplicate rows will not be removed from the result set using UNION ALL unlike the result set of UNION operator that gives distinct rows.</p>
<pre><span style="color: #0000ff;"><strong>SELECT * FROM Table1
<span style="color: #008000;">UNION ALL</span>
SELECT * FROM Table2
<span style="color: #008000;">UNION ALL</span>
SELECT * FROM Table3
</strong></span></pre>
<h3><span style="color: #333399;">Difference Between UNION and UNION ALL ( UNION vs UNION ALL )</span></h3>
<p>Please find important points if we compare the difference between UNION vs UNION ALL result sets and their uses.</p>
<ul>
<li>The UNION operator removes duplicate rows from the result set and gives us distinct rows whereas UNION ALL operator does not remove duplicate rows and give all outputs in the result set.</li>
<li>UNION ALL operator works faster than UNION operator because it does not remove duplicate rows. UNION operator performs distinct sort to remove duplicate rows so its performance is little slower than UNION ALL.</li>
<li>Operator UNION ALL works with all data type columns whereas UNION operator does not work with a column that has a TEXT data type and it will throw error during execution.</li>
</ul>
<h3><span style="color: #333399;">Guidelines to use UNION  and UNION ALL</span></h3>
<p>I have discussed difference between UNION and UNION ALL in above section. Here i will explain below guidelines you should consider while using UNION and UNION ALL operators with other T-SQL statements. Below rules are also apply to EXCEPT and INTERSECT operator:</p>
<ul>
<li>The first query can contain an INTO clause that creates a table to hold the final result set. Only the first query can use an INTO clause. If the INTO clause appears anywhere else, SQL Server displays an error message.</li>
<li>ORDER BY is allowed only at the end of the statement. It cannot be used within the individual queries that make up the statement.</li>
<li>GROUP BY and HAVING clauses can be used only within individual queries; they cannot be used to affect the final result set.</li>
<li>UNION, EXCEPT and INTERSECT can be used within an INSERT statement.</li>
<li>The FOR BROWSE clause cannot be used in statements that involve the UNION, EXCEPT and INTERSECT operators.</li>
</ul>
<p><em><span style="color: #800000;"><strong>Related Articles:</strong></span></em></p>
<ul>
<li><strong><a href="https://techyaz.com/sql-server/how-to-protect-my-stored-procedure-code/" target="_blank" rel="noopener">How to Protect Stored Procedure Code?</a></strong></li>
<li><strong><a href="https://techyaz.com/sql-server/create-logon-trigger-restrict-sysadmin-logins-connect-sql-server-given-time-interval/" target="_blank" rel="noopener">Create a Logon Trigger to Restrict sysadmin logins to Connect to SQL Server during a given time Interval</a></strong></li>
<li><strong><a href="https://techyaz.com/sql-server/get-row-counts-tables-database/" target="_blank" rel="noopener">How to get Total Row Count of all tables of a database</a></strong></li>
</ul>
<p>I hope you like this article. Please follow our <a href="https://www.facebook.com/Techyaz/">Facebook</a> page and <a href="https://twitter.com/Tech_yaz">Twitter </a>handle to get latest updates.</p>
<p>The post <a href="https://techyaz.com/sql-server/t-sql/difference-between-union-and-union-all-operators/">Difference between UNION and UNION ALL T-SQL Operators</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techyaz.com/sql-server/t-sql/difference-between-union-and-union-all-operators/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
