Tải bản đầy đủ (.pdf) (5 trang)

The Language of SQL- P35 ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (95.53 KB, 5 trang )

■ All corresponding columns in each SELECT columnlist must have the same,
or compatible, datatypes.
With reference to these rules, notice that both
SELECT statements in the search
have three columns. Each of the three columns has data in the same order and
with the same datatype.
When using the
UNION, 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
SELECT has 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 BY columnlist.
Also notice that the second column of each
SELECT 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 indi vidual 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 tha t 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. The
UNION operator must be
used to display data in this manner.
In essence, the
UNION allows us to retrieve unrelated or partially related data in a
single statement.
Chapter 15

Set Logic156
Distinct and Non-Distinct Unions
There are actuall y 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. The
UNION ALL operator specifies that all rows are
to be included, even if they are duplicates.
The
UNION operator eliminates duplicates in a manner similar to the DISTINCT
keyword previously seen. Whereas DISTINCT applies to a single SELECT, the
UNION eliminates duplicates in all SELECT statements combined together via
the
UNION.
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, the
UNION operato r ensures that the 2009-12-28 date is only lis-
ted once.
Distinct and Non-Distinct Unions 157
Let’s change the statement, adding a DISTINCT to each individual SELECT, but
also specifying
UNION ALL rather than UNION, as follows:
SELECT
DISTINCT
OrderDate AS 'Date'
FROM Orders
UNION ALL

SELECT
DISTINCT
ReturnDate AS 'Date'
FROM Returns
ORDER BY Date
The outpu t is now:
Date
2009-10-13
2009-10-23
2009-12-05
2009-12-07
2009-12-15
2009-12-28
2009-12-28
The DISTINCT 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, the
UNION 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
The UNION and UNION ALL operators return data that is in either of the sets
specified in the two
SELECT statements being combined. This is like using an OR
operator to combine data from two logical sets.
SQL provides an operator called
INTERSECT, which only pulls data that is in
both of the two sets being looked at. The
INTERSECT is analogous to the AND
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 Logic158
DATABASE DIFFERENCES: MySQL
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
the
EXCEPT operator. Whereas the INTERSECT returns data that is in both sets,
the
EXCEPT returns 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 the
EXCEPT is:
SelectStatementOne
EXCEPT
SelectStatementTwo
ORDER BY columnlist
Intersecting Queries 159
This 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.
DATABASE DIFFERENCES: MySQL and Oracle
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 of SELECT
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. The
UNION is analogous to the OR operator. The UNION ALL is a variant of
the
UNION that allows duplicate rows to be shown. Similarly, the INTERCEPT
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 Logic160

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×