At this point, it is useful to step back and talk about why it was necessary to employ the UNION operator rather than simply join the Orders and Returns tables together in a single SELEC
Trang 1■ All corresponding columns in eachSELECTcolumnlist must have the same,
or compatible, datatypes
With reference to these rules, notice that bothSELECTstatements in the search have three columns Each of the three columns has data in the same order and with the same datatype
When using theUNION, you should use column aliases to give the same column name to all corresponding columns In our example, the first column of the first
SELECT has an original name of OrderDate The first column of the second
SELECThas an original name of ReturnDate To ensure that the first column in the final result has the desired name, both OrderDate and ReturnDate are given a column alias of Date This also allows the column to be referenced in an
ORDER BYcolumnlist.
Also notice that the second column of eachSELECT utilizes literal values We created a calculated column named Type, which has a value of either Order or Return This allows us to tell which table each row comes from
Finally, notice that the ORDER BY clause applies to the final results of both queries combined together This is how it should be, since there would be no point to applying a sort to the individual queries
At this point, it is useful to step back and talk about why it was necessary to employ the UNION operator rather than simply join the Orders and Returns tables together in a single SELECT statement Since both tables have a CustomerID column, why didn’t we simply join the two tables together on this column? The problem with this possibility is that the two tables are really only indirectly related to each Customers can place orders and customers can initiate returns, but there is no direct connection between orders and returns
Additionally, even if there were a direct connection between the two tables, a join would not accomplish what is desired With a proper join, related information can be placed together on the same row In this case, however, we are interested
in showing orders and returns on separate rows TheUNION operator must be used to display data in this manner
In essence, theUNIONallows us to retrieve unrelated or partially related data in a single statement
Chapter 15 ■ Set Logic
156
Trang 2Distinct and Non-Distinct Unions
There are actually two variations of the UNION operator: UNION and UNION
ALL There is only a slight difference between the two The UNION operator
eliminates all duplicate rows TheUNION ALLoperator specifies that all rows are
to be included, even if they are duplicates
TheUNIONoperator eliminates duplicates in a manner similar to theDISTINCT
keyword previously seen Whereas DISTINCT applies to a single SELECT, the
UNION eliminates duplicates in all SELECT statements combined together via
theUNION
In the previous example with the Orders and Returns tables, there was no
pos-sibility of duplication, so it didn’t matter which was used Here’s an example that
illustrates the difference Let’s say that you are only interested in the dates on
which any orders or returns were issued You don’t want to see multiple rows for
the same date The following statement accomplishes this task:
SELECT
OrderDate AS 'Date'
FROM Orders
UNION
SELECT
ReturnDate AS 'Date'
FROM Returns
Order by Date
The resulting data is:
Date
2009-10-13
2009-10-23
2009-12-05
2009-12-07
2009-12-15
2009-12-28
Notice that there is only one occurrence of 2009-12-28 Even though there is one
row with 2009-12-28 in the Orders table and one row with 2009-12-28 in the
Returns table, theUNION operator ensures that the 2009-12-28 date is only
lis-ted once
Trang 3Let’s change the statement, adding aDISTINCTto each individualSELECT, but also specifyingUNION ALL rather thanUNION, as follows:
SELECT
DISTINCT
OrderDate AS 'Date'
FROM Orders
UNION ALL
SELECT
DISTINCT
ReturnDate AS 'Date'
FROM Returns
ORDER BY Date
The output is now:
Date
2009-10-13
2009-10-23
2009-12-05
2009-12-07
2009-12-15
2009-12-28
2009-12-28
TheDISTINCT ensures that each order date or return date is only listed once Even though there are two orders from 2009-10-13, that date is only shown one time However, theUNION ALL allows duplicates between the Orders SELECT
and the Returns SELECT So you can see that 2009-12-28 is listed twice, once from the Orders table and once from the Returns table
Intersecting Queries
TheUNION and UNION ALL operators return data that is in either of the sets specified in the twoSELECTstatements being combined This is like using anOR
operator to combine data from two logical sets
SQL provides an operator calledINTERSECT, which only pulls data that is in both of the two sets being looked at TheINTERSECT is analogous to theAND
operator and handles the second scenario stated at the start of the chapter:
■ Data that is in both SET A and SET B
Chapter 15 ■ Set Logic
158
Trang 4D A T A B A S E D I F F E R E N C E S : M y S Q L
MySQL doesn’t support the INTERSECT operator.
Using the same Orders and Returns tables, let’s say that you want to see dates
on which there are both orders and returns A statement that accomplishes
this is:
SELECT
OrderDate AS 'Date'
FROM Orders
INTERSECT
SELECT
ReturnDate AS 'Date'
FROM Returns
ORDER BY Date
The result is:
Date
2009-12-28
Only one row is shown because this is the only date that appears in both the
Orders and Returns tables
There is one additional variation on the intersect operation, which is provided by
theEXCEPToperator Whereas theINTERSECTreturns data that is in both sets,
theEXCEPTreturns data that is in one set but not the other and handles the third
and fourth scenarios stated at the start of the chapter:
■ Data that is in SET A, but not in SET B
■ Data that is in SET B, but not in SET A
The general format of theEXCEPTis:
SelectStatementOne
EXCEPT
SelectStatementTwo
ORDER BY columnlist
Trang 5This statement will show data that is in SelectStatementOne but not in Select-StatementTwo Here’s an example:
SELECT
OrderDate AS 'Date'
FROM Orders
EXCEPT
SELECT
ReturnDate AS 'Date'
FROM Returns
ORDER BY Date
The result is:
Date
2009-10-13
2009-12-05
2009-12-15
This data shows dates on which orders were placed, but on which no refunds were issued Notice that 2009-12-28 does not appear, since a refund was issued
on that date
D A T A B A S E D I F F E R E N C E S : M y S Q L a n d O r a c l e
MySQL doesn’t support the EXCEPT operator.
The equivalent of the EXCEPT operator in Oracle is MINUS.
Looking Ahead
In this chapter, we’ve seen different ways to combine multiple sets ofSELECT
statements into a single statement The most commonly used operator is the
UNION, which allows you to combine data that is in either of two different sets TheUNIONis analogous to theORoperator TheUNION ALLis a variant of the UNION that allows duplicate rows to be shown Similarly, theINTERCEPT
operator allows that data to be presented if it is in both of the two sets of data being combined The INTERCEPT is analogous to the AND operator Finally, the EXCEPT operator allows for selection of data that is in one set but not in another
Chapter 15 ■ Set Logic
160