next up previous contents index
Next: Joined Tables Up: Joining Tables Previous: Joining Tables

  
Table and Column References

 Before dealing with joins, we must mention one important feature. Up to this point, all queries have involved a single table. When a query involves multiple tables, column names can become confusing. Unless you are familiar with each table, it is difficult to know which column names belong to which tables. Sometimes two tables may use the same column name. For these reasons, SQL allows you to fully qualify column names by preceding the column name with the table name. Figure [*] shows an example of table name prefixing.  

        test=> SELECT firstname FROM friend WHERE state = 'PA';
            firstname    
        -----------------
         Victor         
        (1 row) 
         
        test=> SELECT friend.firstname FROM friend WHERE friend.state = 'PA';
            firstname    
        -----------------
         Victor         
        (1 row) 
         
        test=> SELECT f.firstname FROM friend f WHERE f.state = 'PA';
            firstname    
        -----------------
         Victor         
        (1 row)
 

In the figure, the first query has unqualified column names. The second query is the same, but with fully qualified column names. A period separates the table name from the column name. 

 The final query in Figure [*] shows another feature. Instead of specifying the table name, you can create a table alias to take the place of the table name in the query. The alias name follows the table name in the FROM clause. In this example, f is used as an alias for the friend table. While these features are not important in single table queries, they are useful in multitable queries. 


next up previous contents index
Next: Joined Tables Up: Joining Tables Previous: Joining Tables
Bruce Momjian
2001-05-09