{"id":5617,"date":"2013-05-08T00:00:42","date_gmt":"2013-05-08T00:00:42","guid":{"rendered":"http:\/\/craftydba.com\/?p=5617"},"modified":"2016-04-23T00:44:15","modified_gmt":"2016-04-23T00:44:15","slug":"set-operators","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=5617","title":{"rendered":"Set Operators"},"content":{"rendered":"<p>I am going to promote my series of very short articles or tidbits on Transaction SQL Operators. An operator is a symbol specifying an action that is performed on one or more expressions. <\/p>\n<p>I will exploring the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ff848745.aspx\">Set Operators<\/a> today.  A <a href=\"http:\/\/en.wikipedia.org\/wiki\/Venn_diagram\">Venn diagram<\/a> is used to express different operations between sets.  The above image represents the four possible operations given two sets, A and B, and three operators.  We will get into the operators supported by Microsoft Transaction SQL shortly. <\/p>\n<p>To demonstrate these operators, we need a two temporary tables to query against.  The TSQL code below will create temporary tables A and B.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"setup play table\">\r\n--\r\n-- ~ Setup Play Table ~\r\n--\r\n\r\n-- Drop existing table [A]\r\nIF EXISTS \r\n(\r\n    SELECT * \r\n    FROM tempdb.sys.objects T \r\n    WHERE T.TYPE = 'U' AND T.name LIKE '#A%'\r\n) \r\nDROP TABLE #A;\r\nGO\r\n\r\n-- Make temp table [A]\r\nCREATE TABLE #A (NUM INT PRIMARY KEY);\r\nGO\r\n\r\n-- Add data to table [A]\r\nINSERT INTO #A VALUES (1), (2), (3), (4), (5), (6); \r\nGO\r\n\r\n-- Drop existing table [B]\r\nIF EXISTS \r\n(\r\n    SELECT * \r\n    FROM tempdb.sys.objects T \r\n    WHERE T.TYPE = 'U' AND T.name LIKE '#B%'\r\n) \r\nDROP TABLE #B;\r\nGO\r\n\r\n-- Make temp table [B]\r\nCREATE TABLE #B (NUM INT PRIMARY KEY);\r\nGO\r\n\r\n-- Add data to table [B]\r\nINSERT INTO #B VALUES (5), (6), (7), (8), (9), (10); \r\nGO\r\n\r\n<\/pre>\n<\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms180026.aspx\">UNION<\/a> operator combines the results of two or more queries into a single result set that includes all the rows that belong to all queries in the union.  By default, the operator removes any duplicates.  Using set notation, the <span style=\"color: #dd0000;\">A &#8746; B<\/span> notation is the union of both data sets in the above diagram.<\/p>\n<p>To use this operator, the number and order of the columns have to be the same as well as the data types of the columns must be compatible.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"union operator\">\r\n-- ~ UNION ~ no duplicates\r\nSELECT * FROM #A\r\nUNION\r\nSELECT * FROM #B\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\nNUM\r\n-----------\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n<\/pre>\n<\/p>\n<p>Using the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms180026.aspx\">ALL<\/a> clause of the UNION operator shows the all rows including the duplicates.  Please note that the order of the rows in the result set is not guaranteed.  In our example, both values 5 and 6 are duplicated in the output list.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"union all operator\">\r\n-- ~ UNION ALL ~ show duplicates\r\nSELECT * FROM #A\r\nUNION ALL\r\nSELECT * FROM #B\r\n<\/span><\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\nNUM\r\n-----------\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n<\/pre>\n<\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms188055.aspx\">EXCEPT<\/a> operator returns any distinct values from the left query that are not also found on the right query.  Using set notation, <span style=\"color: #dd0000;\">A &#8211; B<\/span> is the EXCEPT operation of set A in regards to B.  Again, the number and order of the columns have to be the same as well as the data types of the columns must be compatible.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"except operator = (a-b)\">\r\n-- ~ EXCEPT ~ ones that show just in A\r\nSELECT * FROM #A\r\nEXCEPT \r\nSELECT * FROM #B\r\n<\/span><\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\nNUM\r\n-----------\r\n1\r\n2\r\n3\r\n4\r\n<\/pre>\n<\/p>\n<p>Since this operator is order specific, the set notion <span style=\"color: #dd0000;\">B &#8211; A<\/span> is the EXCEPT operation of set B in regards to A.  Please see the image above for a visual representation using set theory.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"except operator = (b-a)\">\r\n-- ~ EXCEPT ~ ones that show just in B\r\nSELECT * FROM #B\r\nEXCEPT \r\nSELECT * FROM #A\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\nNUM\r\n-----------\r\n7\r\n8\r\n9\r\n10\r\n<\/pre>\n<\/p>\n<p>Last but not least, the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms188055.aspx\">INTERSECT <\/a>operator returns any distinct values in common from both the left and right queries.  <\/p>\n<p>Using set notation, <span style=\"color: #dd0000;\">A &#8745; B<\/span> is the INTERSECT operation between sets A and B.  As usual, the number and order of the columns have to be the same as well as the data types of the columns must be compatible.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"intersect operator\">\r\n-- ~ INTERSECT ~ ones that show in both A & B\r\nSELECT * FROM #A\r\nINTERSECT\r\nSELECT * FROM #B\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\nNUM\r\n-----------\r\n5\r\n6\r\n<\/pre>\n<\/p>\n<p>To wrap-up this article, set operators are a great way to combine two similar query outputs using the UNION, EXCEPTION or INTERSECTION as combinatorial rules.  Next time, I will be researching the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms190301.aspx\">String Operators<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am going to promote my series of very short articles or tidbits on Transaction SQL Operators. An operator is a symbol specifying an action that is performed on one or more expressions. I will exploring the Set Operators today. A Venn diagram is used to express different operations between sets. The above image represents the four possible operations given two sets, A and B, and three operators. We will get into the operators supported by Microsoft Transaction SQL shortly. To demonstrate these operators, we need a two temporary tables&hellip;<\/p>\n","protected":false},"author":1,"featured_media":5618,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[814],"tags":[111,31,90,91,15,845,28,29,88],"class_list":["post-5617","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-very-short-articles","tag-all","tag-database-developer","tag-except","tag-intersect","tag-john-f-miner-iii","tag-set-operator","tag-sql-server","tag-tsql","tag-union"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5617","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5617"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5617\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/media\/5618"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}