Home | Site map   
Home Products Downloads Support Contacts
   
  Interactive SQL Tutorial
 

13. UNION and Outer Joins.
There are occasions where you might want to see the results of multiple queries together, combining their output; use UNION. To merge the output of the following two queries, displaying the ID's of all Buyers, plus all those who have an Order placed:

SELECT BUYERIDbr> FROM ANTIQUES
UNION
SELECT OWNERID
FROM ORDERS

Notice that SQL requires that the Select list (of columns) must match, column-by-column, in data type. In this case BuyerID and OwnerID are of the same data type (integer). Also notice that SQL does automatic duplicate elimination when using UNION (as if they were two "sets"); in single queries, you have to use DISTINCT.

The outer join is used when a join query is "united" with the rows not included in the join, and are especially useful if constant text "flags" are included. First, look at the query:

SELECT OWNERID, 'is in both Orders & Antiques'

FROM ORDERS, ANTIQUES
WHERE OWNERID = BUYERID
UNION
SELECT BUYERID, 'is in Antiques only'
FROM ANTIQUES
WHERE BUYERID NOT IN
(SELECT OWNERID
FROM ORDERS)

The first query does a join to list any owners who are in both tables, and putting a tag line after the ID repeating the quote. The UNION merges this list with the next list. The second list is generated by first listing those ID's not in the Orders table, thus generating a list of ID's excluded from the join query. Then, each row in the Antiques table is scanned, and if the BuyerID is not in this exclusion list, it is listed with its quoted tag. There might be an easier way to make this list, but it's difficult to generate the informational quoted strings of text.

This concept is useful in situations where a primary key is related to a foreign key, but the foreign key value for some primary keys is NULL. For example, in one table, the primary key is a salesperson, and in another table is customers, with their salesperson listed in the same row. However, if a salesperson has no customers, that person's name won't appear in the customer table. The outer join is used if the listing of all salespersons is to be printed, listed with their customers, whether the salesperson has a customer or not--that is, no customer is printed (a logical NULL value) if the salesperson has no customers, but is in the salespersons table. Otherwise, the salesperson will be listed with each customer.

Index   << Prev 1 2 3 4 5 6 7 8 9 10 11 12 13


If you like to see how it works enter SQL statement and press Execute

 
 

Home | Products | Downloads | Support | Contacts

Customer quotes

Once again I want to tell you how impressive your software is. I am a computer applications teacher in the school so I don't know how much praise from me means to you. All I can tell you is that it is wonderful to find software that is easy to use and works so well.

Ian Roller
Ireland

What I needed to do was build a members log in area for a web site that the user could update as new people joined. He wanted this done in two days. With not much time I downloaded this software and had the lot done in under 3 min. Normal time for this would be about 5 days and even then you would still be debuging your scripts.



Richard Murray
Web developer

w

Contact us:

Xlinesoft.com
Herndon, VA 20171, USA

  1-888-290-6617
  support@xlinesoft.com

  © 1999 - 2005 XLineSoft. All rights reserved. All comments send to webmaster@xlinesoft.com