FREE BOOK

Chapter 7 - Thinking in Sets

Posted by Addison Wesley Free Book | C# Language August 09, 2009
In this chapter, you will learn What Is a Set, Anyway,Operations on Sets, Intersection, Difference, Union and SQL Set Operations.

Combining Sets: UNION

One more problem about bicycles and helmets, then we'll pedal on to the next chapter. Let's say you're trying to solve this request, which looks simple enough on the surface.

"Show me the orders that contain either a bike or a helmet."

Translation Select the distinct order numbers from the order details table
where the product number is in the list of bike and helmet
product numbers
Clean Up Select the distinct order numbers from the order details table
where the product number is in the list of bike and helmet
product numbers
SQL SELECT DISTINCT OrderNumber
FROM Order_Details
WHERE ProductNumber IN (1, 2, 6, 10, 11, 25, 26)

Actually, that works just fine! So why use a UNION to solve this problem? The truth is, you probably would not. However, if we make the problem more complicated, a UNION would be useful.

"List the customers who ordered a bicycle together with the vendors who provide bicycles."

Unfortunately, answering this request involves creating a couple of queries using JOIN operations, then using UNION to get the final result. Because we haven't shown you how to do a JOIN yet,we'll save solving this problem for Chapter 10. Gives you something to look forward to, doesn't it? Let's get back to the "bicycles or helmets"problem and solve it with a UNION. If you visualize orders with bicycles and orders with helmets as two distinct sets, then you'll find it easier to understand the problem. Figure 7-11 shows you one possible relationship between the two sets of orders.



Figure 7-11 Orders for bicycles or helmets

Seeing "either,""or," or "together" in your request suggests that you'll need to break the solution into separate sets of data and then link the two sets with a UNION. This particular request can be broken into two parts.

"Show me the orders that contain a bike."

Translation Select the distinct order numbers from the order details table where the product number is in the list of bike product numbers
Clean Up Select the distinct order numbers from the order details table where the product number is in the list of bike product numbers
SQL SELECT DISTINCT OrderNumber
FROM Order_Details
WHERE ProductNumber IN (1, 2, 6, 11)

"Show me the orders that contain a helmet."

Translation Select the distinct order numbers from the order details table where the product number is in the list of helmet product
numbers
Clean Up Select the distinct order numbers from the order details table where the product number is in the list of helmet product numbers
SQL SELECT DISTINCT OrderNumber
FROM Order_Details
WHERE ProductNumber IN (10, 25, 26)

Now you're ready to get the final solution by using-you guessed it-a union of the two sets. Figure 7-12 shows the SQL syntax diagram that handles this problem.



Figure 7-12 Linking two SELECT statements with UNION

You can now take the two parts of your request and link them with a UNION operator to get the correct answer.

SQL SELECT DISTINCT OrderNumber
FROM Order_Details
WHERE ProductNumber IN (1, 2, 6, 11)
UNION
SELECT DISTINCT OrderNumber
FROM Order_Details
WHERE ProductNumber IN (10, 25, 26)

The good news is that most commercial implementations of SQL support the UNION operator. As is perhaps obvious from the examples, a UNION might be doing it the hard way when you want to get an "either-or" result from a single table. UNION is most useful for compiling a list from several similarly structured but different tables.We'll explore UNION in much more detail in Chapter 10.

Total Pages : 7 34567

comments