next up previous contents index
Next: Table Aliases and Self-joins Up: Joining Tables Previous: One-to-Many Joins

  
Unjoined Tables

When joining tables, it is necessary to join each table mentioned in the FROM clause by specifying joins in the WHERE clause. If you use a table name in the FROM clause but fail to join it in the WHERE  clause, the table is marked as unjoined. It is then paired with every row in the query result. Figure [*] illustrates this effect using the tables from Figure [*].  

        test=> SELECT *
        test-> FROM animal, vegetable;
         animal_id |      name       | animal_id |      name       
        -----------+-----------------+-----------+-----------------
               507 | rabbit          |       507 | lettuce        
               508 | cat             |       507 | lettuce        
               507 | rabbit          |       507 | carrot         
               508 | cat             |       507 | carrot         
               507 | rabbit          |       507 | nut            
               508 | cat             |       507 | nut            
        (6 rows)
 

The SELECT does not join any column from animal to any column in vegetable, causing every value in animal to be paired with every value in vegetable. This result, called a Cartesian product , is usually not intended. When a query returns many more rows than expected, look for an unjoined table in the query. 


Bruce Momjian
2001-05-09