first_page the funky knowledge base
personal notes from way, _way_ back and maybe today

T-SQL: Flippant Remarks about Table Aliases and the Self Join

Up until shortly before the writing if this article, I considered the use of table aliases just a matter of style. Since I enjoy writing (for humans) I preferred using table names instead of something like:

USE pubs
SELECT
    p.pub_id
,   p.pub_name
FROM
    publishers AS p

I have no trouble writing out "publishers." However, I find myself forced to use table aliases when I need to project two or more sets of data in the same table. This is related to the so-called "self-join" which can be used to, for example, find out the authors in Oakland, California who live in the same ZIP Code:

USE pubs SELECT DISTINCT au1.au_fname , au1.au_lname FROM authors au1 , authors au2 WHERE au1.zip = au2.zip AND au1.city = 'Oakland' AND au1.au_id <> au2.au_id

Note the AS clause is omitted from the FROM clause (just a matter of style).

mod date: 2002-05-31T17:54:39.000Z