WHERE Customers.CustomerID ¼ Orders.CustomerID) AS 'Number of Orders' FROM Customers ORDER BY Customers.CustomerID Notice that in this case the subquery is a correlated subquery. The subquery is used as a calculated column in the SELECT columnlist. In other words, after the subquery is evaluated, it returns a single value, which is then included in the columnlist. Here’s a general format of the above statement: SELECT CustomerName AS 'Customer Name', SubqueryResult AS 'Number of Orders' FROM Customers ORDER BY Customers.CustomerID Looking Ahead In this chapter, we’ve seen subqueries used in three different ways: as a data source, in selection criteria, and as a calculated column. We’ve also seen examples of both correlated and uncorrelated subqueries. We’ve really only touched on some of the uses (and abuses) of subqueries. What complicates the matter is that many subqueries can be expressed in other ways. Whether or not you choose to utilize subqueries depends on your personal taste and sometimes on the performance of the statement. In general, SELECT statements with sub- queries tend to run more slowly than an equivalent statement without a sub- query. We’ll leave it to more advanced SQL books to discuss the various advantages and disadvantages of using subqueries. Through our use of joins and subqueries, we’ve explored numerous ways to select data from multiple tables. In our next chapter, ‘‘Set Logic,’’ we’re going to look at a way to combine entire queries into a single SQL statement. Thi s is a special type of logic that allows us to merge multiple data sets into a single result. As will be seen, set logic procedures are sometimes necessary in order to display sets of data that are only partially related to each other. As with subqueries, the techniques of set logic provide additional flexibility and logical possibilities for your SQL statements. Looking Ahead 151 This page intentionally left blank chapter 15 Set Logic Keywords Introduced: UNION, UNION ALL, INTERSECT, EXCEPT/MINUS The various joins and subqueries of the past few chapters have dealt with dif- ferent ways of combining data from multiple tables. The end result, however, has been a single SELECT statement. We’re now going to extend the concept of combining tables to the possibility of combining data from entire queries. In other words, we’re going to look at a way to write a single SQL statement that combines more than one SELECT to retrieve data. The concept of combining queries is often referred to as set logic , a term taken from mathematics. Each SELECT query can be referred to as a set of data. The set logic we will examine in this chapter will address four scenarios. Assuming that we have data in SET A and in SET B, here are the possibilities that we want: ■ Data that is in SET A or in SET B ■ Data that is in both SET A and SET B ■ Data that is in SET A, but not in SET B ■ Data that is in SET B, but not in SET A We’re going to start with a look at the first scenario, that data is in SET A or in SET B. As will be seen, this is the most prevalent and important of the set logic possibilities. 153 Using the UNION Operator The UNION operator in SQL is used to handle logic to select data that is either SET A or SET B. We’ll start with an example. Let’s say that we have two tables in our database. The first is an Orders table containi ng data on orders placed by customers. It might look like: OrderID CustomerID OrderDate OrderAmount 1 1 2009-10-13 10 2 2 2009-10-13 8 3 2 2009-12-05 7 4 2 2009-12-15 21 5 3 2009-12-28 11 The second table, named Returns, contains data on merchandise that has been returned by customers. It might look like: ReturnID CustomerID ReturnDate ReturnAmount 1 1 2009-10-23 2 2 2 2009-12-07 7 3 3 2009-12-28 3 It’s important to note that, unlike the Refunds table seen in Chapter 12, this Returns table is not directly related to the Orders table. In other words, returns are not tied to a specific order. In this scenario, a customer might return mer- chandise from multiple orders. We want to create a report of all the orders and returns from one particular customer. We would like the results sorted by either the order date if it’s an order or the return date if it’s a return. Here is a statement that can accomplish this. We’ve inserted a few extra blank lines in this statement, to emphasize the fact that it contains two completely separate SELECTs, combined together with the UNION operator: Chapter 15 ■ Set Logic154 SELECT OrderDate AS 'Date', 'Order' AS 'Type', OrderAmount AS 'Amount' FROM Orders WHERE CustomerID = 2 UNION SELECT ReturnDate AS 'Date', 'Return' AS 'Type', ReturnAmount AS 'Amount' FROM Returns WHERE CustomerID = 2 ORDER BY Date The resulting date is: Date Type Amount 2009-10-13 Order 8 2009-12-05 Order 7 2009-12-07 Return 7 2009-12-15 Order 21 As seen, the UNION operator separates two completely separate SELECT state- ments. There is also an ORDER BY clause at the very end, which applies to the results of both SELECT statements. The general format for the previous state- ment is: SelectStatementOne UNION SelectStatementTwo ORDER BY columnlist In order for the UNION to work, three rules must be followed: ■ All SELECT statements combined together with a UNION must have the same number of columns in the SELECT columnlist. ■ All columns in each SELECT columnlist must be in the same order. Using the UNION Operator 155 . the SELECT columnlist. In other words, after the subquery is evaluated, it returns a single value, which is then included in the columnlist. Here’s a general format of the above statement: SELECT CustomerName. to retrieve data. The concept of combining queries is often referred to as set logic , a term taken from mathematics. Each SELECT query can be referred to as a set of data. The set logic we will. look at the first scenario, that data is in SET A or in SET B. As will be seen, this is the most prevalent and important of the set logic possibilities. 153 Using the UNION Operator The UNION