Sybase Joins

In SQL, a join is how you connect two or more tables to create a single result. As an example, we could connect together salesmen and their customers something like this...

Select Salesman, CustomerName
  from Salesman s, customer c
 where c.SalesmanCode = s.SalesmanCode
 order by salesman

This will pull out all the customers for each salesman...

Salesman    CustomerName
----------- -------------
Al Smith    GeekCo
Al Smith    Big PLC
Fred Bloggs LittleShops



Outer Joins

But suppose a salesman has no customers? He'll never appear in the report because the join shown above (known as an equi-join) will only produce output where a column in the first table matches a column in the second table. The way to overcome this is shown here...

Select Salesman, CustomerName
  from Salesman s, customer c
 where c.SalesmanCode *= s.SalesmanCode
 order by salesman

Note the * character before the = sign. This turns the query into a 'left outer join' and might produce output like this...

Salesman    CustomerName
----------- -------------
Al Smith    GeekCo
Al Smith    Big PLC
Fred Bloggs LittleShops
Guy New     NULL

There's a new row in the report for a salesman with no customer. Basically, the query returns every row in the table on the left side of the join together with any matches on the right. If there's no match, the query returns one row with the left hand table entry and a NULL for the right hand side.

You can, of course, reverse this to get a 'right outer join'...

Select Salesman, CustomerName
  from Salesman s, customer c
 where c.SalesmanCode =* s.SalesmanCode
 order by salesman
 

which might return...

Salesman    CustomerName
----------- -------------
Al Smith    GeekCo
Al Smith    Big PLC
Fred Bloggs LittleShops
NULL        NewShop

Showing that 'NewShop' hasn't been assigned to a salesman yet.
 





Find out more by searching Google here...

Google