Posts

Showing posts from November, 2019

A Visual Guide to T-SQL Set Operators

Image
Following on from my previous post visually showing how T-SQL PIVOT works ( here ), the following presents a visual guide to how set operators are used in T-SQL and what restrictions there are when creating such queries. I found these diagrams very helpful when revising for my MS 70-761 (Querying Data with Transact-SQL) exam.  What are the Set Operators? Results of T-SQL queries are sets, as such set operators can be applied to the results of multiple queries to combine the results in different ways. There are 4 set operators in T-SQL: UNION: the concatenation of results from both sets, excluding duplicates. UNION ALL: the concatenation of results from both sets, including duplicates. INTERSECT: the results that appear in both sets. EXCEPT: think of this set operator as the results that appear in one set minus the results that appear in another set. Basic Set Operator Query Format The basic format for using a set operator is to have multiple select statements (sub qu