Join Using SELECT SQL Does Not Return Any Records

Last reviewed: June 27, 1995
Article ID: Q95670
The information in this article applies to:
  • Microsoft FoxPro for Windows, versions 2.5 and 2.5a
  • Microsoft FoxPro for MS-DOS, versions 2.0, 2.5, and 2.5a

SUMMARY

When the SELECT SQL statement is used to join two databases, the desired effect is to retrieve records that meet conditions involving the databases in the FROM clause. However, under some circumstances, the join might not return any records. The code and explanation below demonstrate this.

MORE INFORMATION

The query below demonstrates a join of the CUSTOMER.DBF and INVOICES.DBF databases in the TUTORIAL directory. This join should retrieve all the customers who are NOT in the INVOICES database:

   SELECT customer.company, customer.cno ;
     FROM customer, invoices ;
     WHERE customer.cno NOT IN ;
       (SELECT invoices.cno FROM invoices ;
          WHERE customer.cno = invoices.cno) ;
   INTO CURSOR QUERY

The query above retrieves all the customers who do not have any invoices. However, if the INVOICES database is empty, the above query doesn't return any records even though the expected result is for every record to be returned. This effect occurs because the INVOICES database appears in the first FROM clause of the first SELECT statement.

A database that appears in a FROM clause must contain at least one record in order for the query to work correctly. Therefore, if the INVOICES database reference in the first FROM clause of the first SELECT statement is removed, all the customers who are not in the INVOICES database will be returned. This result occurs even if the INVOICES database is empty. The query below demonstrates this behavior:

   SELECT customer.company, customer.cno ;
     FROM customer ;
     WHERE customer.cno NOT IN ;
        (SELECT invoices.cno FROM invoices ;
          WHERE customer.cno = invoices.cno) ;
   INTO CURSOR QUERY


Additional reference words: FoxDos FoxWin 2.00 2.50 2.50a
KBCategory: kbprg
KBSubcategory: FxprgSql


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 27, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.