23rd Jun 2017 Updated: 4th Dec 2018 6 minutes read SQL Set Operators: Union, Union All, Minus, Intersect Marek Pankowski sql set operators Table of Contents The Sample Tables The UNION Set Operator The UNION ALL Set Operator The MINUS Set Operator The INTERSECT set Operator Minus, Union, Intersection in SQL: Practical Tips Ever heard terms such as union and intersection in SQL? They're examples of set operators, and they come in handy when you need to combine information from multiple tables or queries. In this article, we'll take a closer look at them. SQL queries let us choose the most important bits from large amounts of information. Of course, we can't expect that all necessary data will be stored in one table. Let's say we want to present every aspect of some key data group in one results table (e.g. making a report with the names of every customer served by every department of a large company). These records may be found in many different tables, so we need set operators such as union and intersection in SQL to merge them into one table or to find common elements. During such operations, we take two or more results from SELECT statements and create a new table with the collected data. We do this using a SQL set operator. Operators like MINUS, UNION or INTERSECT are widely used in SQL queries. Let's see how they work. The Sample Tables Suppose we have a very simple database that stores information about books and movies. It has only two tables, BOOKS and MOVIES, which contain book and movie titles (respectively) and an ID number. As you look at these tables, notice that one title appears in both: Table: BOOKS ID Title 1 The Witcher 2 Harry Potter 3 Nineteen Eighty-Four 4 The Great Gatsby Table: MOVIES ID Title 1 Iron Man 2 Harry Potter 3 Dr Strange 4 Matrix The UNION Set Operator What if we wanted to make one table from all the content in the BOOKS and MOVIES tables? This is a perfect time to use UNION set operator. UNION merges the results of two SELECT statements. Important: UNION statements only return UNIQUE values. Below, you'll see a Venn diagram representing this operation and the code that will make it happen: SELECT * FROM BOOKS UNION SELECT * FROM MOVIES Here is the result: ID Title 1 The Witcher 2 Harry Potter 3 Nineteen Eighty-Four 4 The Great Gatsby 5 Iron Man 6 Dr Strange 7 Matrix All the book and movie titles are now in one table. Note that "Harry Potter" – an item which appears in both tables – is shown only once in the results. Like we mentioned earlier, the UNION set operator does not return duplicate values. The UNION ALL Set Operator You've probably guessed that UNION ALL is very similar to UNION, but with one exception: UNION ALL returns all data from all tables, no matter if it is a duplicate or not. Let's do the same operation as in the UNION example and see what we get: SELECT * FROM BOOKS UNION ALL SELECT * FROM MOVIES Result: ID Title 1 The Witcher 2 Harry Potter 3 Nineteen Eighty-Four 4 The Great Gatsby 5 Iron Man 6 Harry Potter 7 Dr Strange 8 Matrix This time, the little wizard appears twice in the results table. Now we know the main difference between these two operators. But when should we use UNION and when UNION ALL? Firstly, know that there is a huge difference in efficiency between them. Let's say we need to merge two query results that each contain 10,000 elements. UNION will eliminate any duplicates and sort all the elements in the results table. This sorting process takes a lot of time and works with a large number of elements. In sum, UNION can be four times slower than UNION ALL, which doesn't eliminate duplicates and doesn't sort the data. If we don't care about duplicates and we want to work fast, UNION ALL will be the perfect solution. But if we know that having unique elements is our main goal, then UNION will be much more helpful. The MINUS Set Operator MINUS is a little bit different. Let's say we want to see only book titles that are not also movie titles. We need to "minus" everything from the BOOKS table that is also in the MOVIES table. The MINUS set operator is designed for this type of task. SELECT * FROM BOOKS MINUS SELECT * FROM MOVIES The result: ID Title 1 The Witcher 2 Nineteen Eighty-Four 3 The Great Gatsby Now "Harry Potter" doesn't appear in the results table; it's the title of a book and a movie. Thanks to the MINUS set operator we are able to see only those titles that occur in the first table and are not present in the second. By the way, some databases use the keyword EXCEPT instead of MINUS. Don't worry – the function and results are exactly the same. The INTERSECT set Operator OK, so we know how to add and subtract some elements using the UNION and MINUS operators. But what should we do if we need to know what two queries have in common? This is the main role of the INTERSECT operator. Let's see how it works. SELECT * FROM BOOKS INTERSECT SELECT * FROM MOVIES And the result: ID Title 1 Harry Potter You know by now that "Harry Potter" (and only "Harry Potter") is present in both tables. So it is the only element in the results table. Minus, Union, Intersection in SQL: Practical Tips There are a few things to remember about minus, union and intersection in SQL: If the column names or aliases being compared are different, the result column will be called after the column in the first SELECT query. You can use either query results or tables with set operators. The columns being compared must be the same type and of equal number. The results table will always have more higher-precision columns. The MINUS, UNION and INTERSECT operators will always sort the returned results; UNION ALL will not. If we want a certain sort order or type, we can always use an ORDER BY at the end of the query. But keep in mind that this will sort the whole query! We can't use ORDER BY before a SQL set operator or try to sort every SELECT separately. We should also mention that we can use set operators such as minus, union and intersection in SQL in all types of queries. We don't need to select all of the records from both tables; we can work on the results of existing queries. It's amazing what we can do with set operators like minus, union and intersection in SQL. Thanks to them, we can easily find and present interesting data. Get some hands-on experience in working with SQL set operators and other functions with LearnSQL.com's SQL Basics course. Start new exciting data journey the proper way! Tags: sql set operators