Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / database / SQL-Server

SQL Server and Set Operations

5.00/5 (2 votes)
30 Dec 2017MIT4 min read 7.8K  
SQL set operations

In this puzzle, we’re going to talk about SQL set operations. Set operations allow us to compare rows from two or more tables to arrive at a result. For several classes of problems, it is much easier to use a set operation, than join data.

Solving puzzles is a great way to learn SQL. Nothing beats practicing what you’ve learned. Once you have figured out the puzzle, post your answer in the comments so we can all learn from one another.

SQL Set Operations Puzzle – Part I

Given two lists of numbers: A and B:

  1. Select all odd numbers ordered from highest to lowest
  2. Select all numbers from A that aren’t in B
  3. Select all numbers common to A and B

*** Don’t use joins or subqueries to answer these questions.

Answer

Before we get into the answers, I think it is important for you to know the difference between a join and set operation.

Though both concepts are used to combine data from multiple tables, join combine columns from separate tables; whereas, set operations combine rows from separate tables.

For instance, union is used to combine rows from two or more tables into a single result. It is difficult to do this using JOIN.

Today to answer the question, we are going to use UNION, EXCEPT, and INTERSECT respectively. Below is a visual summary of these operations:

Set Operations - Visual Guide Union, Intersect, Except

If you’re not familiar with set operations, I would recommend reading this article on UNION, INTERSECT, and EXCEPT.

Let’s answer the first question.

Use a Set Operation to Select All Odd Numbers Ordered from Highest to Lowest

To do this, we’ll combine the results of two queries, one from table A the other from B. In both cases, we want only odd numbers. These are numbers, that when divided by 2, have a remainder of one.

Once we have a combined set, we’ll order the results.

Here are the tools we’ll need to use:

  • % – This is the modulus operator we'll use to get the remainder when we divide number by 2. Numbers with a remainder of 1 are odd.
  • UNION – This set operator allows us to combine rows from one result set with another if each result has the same number of columns and datatypes.
  • ORDER BY DESC – We use the ORDER BY clause to sort values.

The query to get odd number from table A is:

SQL
SELECT   Number
FROM     @A
WHERE    Number % 2 = 1

The modulo operator (%) may seem strange, but is it really handy!

We repeat the same query for table B and combine them using UNION ALL and ORDER the results.

SQL
SELECT   Number
FROM     @A
WHERE    Number % 2 = 1
UNION ALL
SELECT   Number
FROM     @B
WHERE    Number % 2 = 1
ORDER BY Number DESC

Notice that I used UNION ALL. This returns every row for the union-ed results, even duplicate values. The above query returns 20 rows, below are some of the results:

Set Operators - UNION ALL

If I ran the query without ALL, then only 15 rows are returned; try it!

If you want to learn more about the UNION operator, I would recommend this article on UNIONS.

Use a Set Operator to Select All Numbers From A that aren’t in B

To answer this question, we’ll use the EXCEPT operator. It will limit our result to only those rows that are exclusive to one table.

Set Operators - Except Operator

For our purposes, think of table A as the orange table, and B as the blue. When we do A EXCEPT B, we are going to return the orange crescent. This represents rows exclusive to A.

Here is the query to do so:

SQL
SELECT   Number
FROM     @A
EXCEPT
SELECT   Number
FROM     @B

In this example, we retrieve all rows from A, they are then compared to all rows from B, and only those rows found in A, but Not B are returned in the result:

Set Operators - Except Operator REsults.

I’m sure some of you were thinking of other solutions, such as those that use a JOIN or Subquery.

Here is the solution as a subquery:

SQL
SELECT   Number
FROM     @A
WHERE    Number NOT IN (SELECT Number
                          FROM @B)

I show you this, so you can understand that there are many ways to solve the same problem!

Use a Set Operator to Select All Numbers Common to A and B

To find all numbers common between two results, we can use the INTERSECT operator. These are the numbers which “overlap” in our diagram.

Though this was the last question in the puzzle, I think it is the easiest to answer!

SQL
SELECT   Number
FROM     @A
INTERSECT
SELECT   Number
FROM     @B

Can you think of another way of showing numbers common to both tables? What about an INNER JOIN?

Here is another way to get the same result:

SQL
SELECT   A.Number
FROM     @A A
         INNER JOIN @B B
         ON A.Number = B.Number

So how did you answer the puzzle questions? I would really like to know. Put your answers in the comments!

Also, if you have any ideas for more puzzles, let me know.

License

This article, along with any associated source code and files, is licensed under The MIT License