Collection Contents Index Joins involving derived tables CHAPTER 7.  Using Subqueries pdf/chap6.pdf

User's Guide
   PART 1. Working with Databases
     CHAPTER 6. Joins: Retrieving Data from Several Tables       

Transact-SQL outer joins


Joins that include all rows, regardless of whether or not they match the join condition, are called outer joins. Adaptive Server Anywhere supports both left and right outer joins via the LEFT OUTER and RIGHT OUTER keywords. For compatibility with Adaptive Server Enterprise, Anywhere supports the Transact-SQL-language counterparts of these keywords.

In the Transact-SQL dialect, joins are accomplished by separating table names with commas in the FROM clause. The join conditions appear in the WHERE clause, rather than in the ON phrase. Special conditional operators indicate the type of join.

Top of page  Transact-SQL left-outer joins

The left outer join operator, *=, selects all rows from the left hand table that meet the statement's restrictions. The right hand table generates values if there is a match on the join condition. Otherwise, the second table generates null values.

For example, the following left outer join lists all customers and finds their order dates (if any):

SELECT fname, lname, order_date
FROM customer, sales_order
WHERE customer.id *= sales_order.cust_id
ORDER BY order_date

Preserved and null-supplying tables 

A table is either a preserved or a null-supplying table for an outer join. If the join operator is *=, the second table is the null-supplying table; if the join operator is =*, the first table is the null-supplying table.

You can compare a column from the inner table to a constant as well as using it in the outer join. For example, you can use the following statement to find information about customers in California.

SELECT fname, lname, order_date
FROM customer, sales_order
WHERE customer.state = 'CA'
   AND customer.id *= sales_order.cust_id
ORDER BY order_date

However, the inner table in an outer join cannot also participate in a regular join clause.

Bit columns    
Since bit columns do not permit null values, a value of 0 appears in an outer join when there is no match for a bit column that is in the inner table.

Top of page  Transact-SQL right-outer joins

The right outer join, =*, selects all rows from the second table that meet the statement's restrictions. The first table generates values if there is a match on the join condition. Otherwise, the first table generates null values.

The right outer join is specified with the comparison operator =*, which indicates that all the rows in the second table are to be included in the results, regardless of whether there is matching data in the first table.

Substituting this operator in the outer join query shown earlier gives this result:

SELECT fname, lname, order_date
FROM sales_order, customer
WHERE sales_order.cust_id =* customer.id
ORDER BY order_date

You can further restrict an outer join by comparing it to a constant. This means that you can zoom in on precisely the values you really want to see and use the outer join to list the rows that did not make the cut.

Top of page  Transact-SQL outer join restrictions

There are several restrictions for Transact-SQL outer joins:

Top of page  Views used with Transact-SQL outer joins

If you define a view with an outer join, and then query the view with a qualification on a column from the inner table of the outer join, the results may not be what you expect. The query returns all rows from the null-supplying table. Rows that do not meet the qualification show a NULL value in the appropriate columns of those rows.

The following rules determine what types of updates you can make to columns through join views:

Top of page  How NULL affects Transact-SQL joins

NULL values in tables or views being joined will never match each other. Since bit columns do not permit NULLs, a value of 0 appears in an outer join when there is no match for a bit column that is in the inner table.

The result of a join of NULL with any other value is NULL. Because null values represent unknown or inapplicable values, Transact-SQL has no reason to believe that one unknown value matches another.

You can detect the presence of null values in a column from one of the tables being joined only by using an outer join. If there are two tables, each of which has a NULL in the column that will participate in the join. A left outer join displays the NULL in the first table.

Top of page  

Collection Contents Index Joins involving derived tables CHAPTER 7.  Using Subqueries pdf/chap6.pdf