| Retrieving Data from Multiple Tables with SQL Joins | ||||||||||||||
| Part 3: Outer Joins | ||||||||||||||
Take a moment and review the database tables located on the first page of this article. Notice that we have a driver -- Jack Ryan -- who is located in a city where there are no vehicles. Our vehicle managers would like this information to be included in their query results to ensure that drivers do not sit idly by waiting for a vehicle to arrive. We can use outer joins to include records from one table that have no corresponding record in the joined table. Let's create a list of driver/vehicle pairings that includes records for drivers with no vehicles in their city. We can use the following query: SELECT lastname, firstname, driver.city, tag Notice that the outer join operator "(+)" is included in this query. This operator is placed in the join condition next to the table that is allowed to have NULL values. This query would produce the following results: lastname
firstname city
tag This time our results include the stranded Patrick Ryan and our vehicle management department can now dispatch a vehicle to pick him up. Note that there are other possible ways to accomplish the results seen in this article and syntax may vary slightly from DBMS to DBMS. These examples were designed to work with Oracle databases, so your mileage may vary. Furthermore, as you advance in your knowledge of SQL you’ll discover that there is often more than one way to accomplish a desired result and oftentimes one way is just as good as another. Case in point, it is also possible to specify a join condition in the FROM clause rather than the WHERE clause. For example, we used the following SELECT statement earlier in this article: SELECT lastname, firstname, tag The same query could be rewritten as: SELECT lastname, firstname, tag That's it for this week! Be sure to check back next week for a new exciting article on databases. If you'd like a reminder in your Inbox, subscribe to the About Databases newsletter.
|
||||||||||||||




