The syntax for the statement in Oracle is: SELECT C.CustomerID AS 'Cust ID', C.FirstName AS 'First Name', C.LastName AS 'Last Name', O.OrderID AS 'Order ID', O.Quantity AS 'Qty', O.Price
Trang 1The results are:
Notice that we’re using theASkeyword to specify both column and table aliases
It should also be mentioned that theASkeyword is completely optional All of the AS keywords can be removed from theSELECT, and the statement would still be valid and return the same results However, I recommend the use of the
ASkeyword for the sake of clarity
D A T A B A S E D I F F E R E N C E S : O r a c l e
As mentioned in Chapter 3, table aliases are specified in Oracle without the AS keyword The syntax for the statement in Oracle is:
SELECT
C.CustomerID AS 'Cust ID',
C.FirstName AS 'First Name',
C.LastName AS 'Last Name',
O.OrderID AS 'Order ID',
O.Quantity AS 'Qty',
O.PricePerItem AS 'Price'
FROM Customers C
INNER JOIN Orders O
ON C.CustomerID ¼ O.CustomerID;
Looking Ahead
The ability to join tables together in query is an essential feature of SQL Rela-tional databases would be of little use without joins This chapter focused on the formulation of the inner join The inner join brings back data for which there is a match between both tables being joined We also talked about an alternate way of specifying the inner join and the usefulness of specifying table aliases
In our next chapter, ‘‘Combining Tables with an Outer Join,’’ we will turn to another important type of join, the outer join As mentioned, inner joins only Chapter 11 ■ Combining Tables with an Inner Join
116
Trang 2allow us to view data when there is a match between the tables being joined So, if
you have a customer with no orders, you won’t see any customer information
when doing an inner join between a Customers and an Orders table The outer
join will allow you to view customer information even if there are no orders for a
customer In other words, the outer join lets us see data that we would not
otherwise be able to obtain with an inner join
Looking Ahead 117
Trang 3This page intentionally left blank
Trang 4chapter 12
Combining Tables
with an Outer Join
Keywords Introduced: LEFT JOIN ,
RIGHT JOIN , FULL JOIN
We now advance from inner to outer joins The main restriction of inner joins is that they require a match in all tables being joined to show any results If you’re joining a Customers table to an Orders table, no data for the customer is shown if that customer hasn’t yet placed an order This may seem like a relatively unim-portant problem, but it often becomes more significant with different types of data Let’s say, for example, that we have an Orders table and a Refunds table The Refunds table is related to the Orders table by an OrderID In other words, all refunds are tied to a specific order The refund can’t exist unless the order exists The problem arises when you want to see both orders and refunds in a single query If you join these two tables with an inner join, you won’t see any orders if refunds were never issued against that order Presumably, this will be the majority of your orders In contrast, the outer join allows you to view orders even if they don’t have a matching refund, and it is therefore an essential tech-nique to understand and use
The Outer Join
All the joins seen in the last chapter were inner joins Since inner joins are the most common join type, SQL specifies these as a default, so you can specify an inner join using only the keywordJOIN It isn’t necessary to stateINNER JOIN
119
Trang 5In contrast to inner joins, there are three types of outer joins: LEFT OUTER JOIN,RIGHT OUTER JOIN,andFULL OUTER JOIN These can be referred to
as simply: LEFT JOIN, RIGHT JOIN, and FULL JOIN In this case, the word
OUTER isn’t necessary To summarize, my recommendation is to refer to the four join types as:
This keeps the syntax consistent and easy to remember
In our discussion of outer joins, we’re going to utilize three tables in our examples First, there will be a Customers table with information about each customer Second, there will be an Orders table with data on each order placed Finally, we will add a Refunds table with information about any refunds that have been issued to customers
Figure 12.1 shows how these three tables are connected
Figure 12.1
Entity-relationship diagram.
In contrast to the figure seen in the last chapter, the lines connecting the tables are now shown as arrows You can see an arrow drawn from the CustomerID field of the Customers table to the CustomerID field of the Orders table This arrow indicates that the link between the Customers and Orders tables is possibly one-sided in the sense that there may not be any orders for any given customer Additionally, there may be multiple orders for a single customer Similarly, the arrow drawn between the Orders and Refunds tables indicates that there may not
be any refunds for any given order, and that there may be multiple refunds for an order
Chapter 12 ■ Combining Tables with an Outer Join
120