Visual Database Tools

Using a Table Twice in One Query

You can use the same table two (or more) times within a single query.  There are several situations in which you do this.

By joining the table to itself using this reflexive relationship, you can establish a result set in which each row contains a boss's name and the name of one of that boss's employees.  The resulting SQL might look like this:

SELECT 
    boss.lname, 
    boss.fname, 
    employee.lname, 
    employee.fname
FROM 
    employee
        INNER JOIN 
        employee boss 
        ON employee.manager_emp_id 
        =  boss.emp_id

For more information about creating joins using reflexive relationships, see Creating Self-Joins Automatically.

Note   To distinguish between the multiple uses of any one table, the preceding query uses the following aliases:  favorite_title, favorite_titleauthor, other_titleauthor, and other_title.  For more information about aliases, see Creating Table Aliases.

See Also

Structure of Retrieval Queries | Drawing a Reflexive Relationship