fhg
Volume 7, Number 37 -- October 24, 2007

Good Reasons to Use Unrequired Correlation Names

Published: October 24, 2007

by Ted Holt

Correlation names are alternate names given to tables in an SQL command. Sometimes correlation names are required. Other times they are optional, but helpful. I would go so far as to say that it is good practice to use correlation names as often as possible, even when they are not obligatory. Let me share two good reasons to do so.

But first, let's review the basics of correlation names. A correlation name follows a table name in the FROM clause. The table name and correlation name must be separated by white space. You may also include the filler word AS between the two. I always include AS because the query sounds better when I'm reading it.

This query has no correlation name.

SELECT classid, instructor, building, room
  FROM schedule

CLASSID

INSTRUCTOR

BUILDING

ROOM

101

F02

41

320

102

F03

41

218

103

F02

41

212

104

F05

42

302

105

F04

41

165

106

F06

41

212

107

F01

42

302

108

F07

41

212

109

F08

41

320


Here's one that assigns correlation name SK to the schedule table.

SELECT classid, instructor, building, room
  FROM schedule as sk

CLASSID

INSTRUCTOR

BUILDING

ROOM

101

F02

41

320

102

F03

41

218

103

F02

41

212

104

F05

42

302

105

F04

41

165

106

F06

41

212

107

F01

42

302

108

F07

41

212

109

F08

41

320


In this case, using the correlation name doesn't buy me anything, so let's look at a more complex query.

SELECT classid, classes.name,        
       instructor, faculty.name,     
       building, buildings.name, room
  FROM schedule
  LEFT JOIN classes                  
    ON classid = classes.id          
  LEFT JOIN buildings                
    ON building = buildings.id       
  LEFT JOIN faculty                  
    ON instructor = faculty.id       

CLASSID

NAME

INSTRUCTOR

NAME

BUILDING

NAME

ROOM

101

Antarctic Literature

F02

Nootix, Herman

41

Narrow Hall

320

102

Pig Latin IV

F03

Andree, Polly

41

Narrow Hall

218

103

Rodeo Appreciation

F02

Nootix, Herman

41

Narrow Hall

212

104

Antarctic History II

F05

O'Var, Sam

42

Vaux Hall

302

105

Cooking with Guppies

F04

LeNoll, Ty

41

Narrow Hall

165

106

Null

F06

Sillen, Penny

41

Narrow Hall

212

107

Null

F01

Monella, Sal

42

Vaux Hall

302

108

Null

F07

Doo, Billy

41

Narrow Hall

212

109

Null

F08

Fishul, Benny

41

Narrow Hall

320


There are four tables--count 'em! Three of them--Classes, Buildings, and Faculty--have common column (field) names of ID and Name. I had to qualify all instances of the ID and Name fields with the table names in order to eradicate ambiguity.

This is fine, but it can turn into an awful lot of typing. This leads us to one of the most common uses of correlation names. You can use correlation names as shorter names for tables (and views). Here's the same query using b, c, and f as correlation names for the Buildings, Classes, and Faculty tables, respectively.


SELECT classid, c.name,      
       instructor, f.name,   
       building, b.name, room
  FROM schedule              
  LEFT JOIN classes as c     
    ON classid = c.id        
  LEFT JOIN buildings as b   
    ON building = b.id       
  LEFT JOIN faculty as f     
    ON instructor = f.id     

CLASSID

NAME

INSTRUCTOR

NAME

BUILDING

NAME

ROOM

101

Antarctic Literature

F02

Nootix, Herman

41

Narrow Hall

320

102

Pig Latin IV

F03

Andree, Polly

41

Narrow Hall

218

103

Rodeo Appreciation

F02

Nootix, Herman

41

Narrow Hall

212

104

Antarctic History II

F05

O'Var, Sam

42

Vaux Hall

302

105

Cooking with Guppies

F04

LeNoll, Ty

41

Narrow Hall

165

106

Null

F06

Sillen, Penny

41

Narrow Hall

212

107

Null

F01

Monella, Sal

42

Vaux Hall

302

108

Null

F07

Doo, Billy

41

Narrow Hall

212

109

Null

F08

Fishul, Benny

41

Narrow Hall

320


The only time a correlation name is required is when one table is used more than once in a query. For instance, to find the names of professors who share classrooms, we might run this query:

SELECT s1.building, s1.room, f1.NAME, f2.NAME 
  FROM schedule AS s1                         
  JOIN schedule AS s2                         
    ON s1.building = s2.building              
   AND s1.room = s2.room                      
   AND s1.classid <> s2.classid               
  JOIN faculty AS f1                          
    ON s1.instructor = f1.id                  
  JOIN faculty AS f2                          
    ON s2.instructor = f2.id                  
ORDER BY 1, 2                                 

BUILDING

ROOM

NAME

NAME         

41

212

Nootix, Herman

Sillen, Penny

41

212

Nootix, Herman

Doo, Billy   

41

212

Sillen, Penny

Nootix, Herman

41

212

Sillen, Penny

Doo, Billy   

41

212

Doo, Billy

Nootix, Herman

41

212

Doo, Billy

Sillen, Penny

41

320

Nootix, Herman

Fishul, Benny

41

320

Fishul, Benny

Nootix, Herman

42

302

O'Var, Sam

Monella, Sal 

42

302

Monella, Sal

O'Var, Sam   


In this query, we need to access the schedule table twice and the faculty table twice. Without correlation names, there would be no way to determine which instance of each table was being referenced.

I hope that covers the basics. Now, as I was saying, there are good reasons to use correlation names when correlation names are not required. Here are two.

First, using a correlation name ensures that the SQL interpreter catches a missing-comma error. For instance, look at this query and tell me how many columns of data it produces.

SELECT CLASSID PERIOD, BUILDING, ROOM INSTRUCTOR 
FROM schedule

Yes! That's right! The query produces three columns of data!


PERIOD

BUILDING

INSTRUCTOR

101

41

320

102

41

218

103

41

212

104

42

302

105

41

165

106

41

212

107

42

302

108

41

212

109

41

320


The first column, ClassID, is renamed to Period. The Building column is second. The room number is listed in the third column, but it is renamed to instructor. Clearly, the author of this query intended to produce five columns of data, but omitted two commas.

Let's look at the same query, mistakes included, with correlation names.

SELECT sked.CLASSID sked.PERIOD, sked.BUILDING,
       sked.ROOM sked.INSTRUCTOR               
  FROM schedule as sked

SQL produces error SQL0104: Token . was not valid. Valid tokens: , FROM INTO. Adding the missing commas produces the correct output.

SELECT sked.CLASSID, sked.PERIOD, sked.BUILDING,
       sked.ROOM, sked.INSTRUCTOR               
  FROM schedule as sked

CLASSID

PERIOD

BUILDING

ROOM

INSTRUCTOR

101

A

41

320

F02

102

A

41

218

F03

103

B

41

212

F02

104

B

42

302

F05

105

C

41

165

F04

106

B

41

212

F06

107

B

42

302

F01

108

D

41

212

F07

109

D

41

320

F08


I consider this an improvement, as I prefer syntax errors to logic errors any day of the week.

A second good reason to use correlation names when they are not required is to enhance the readability of a query. Look at the following query and tell me in which table Room is stored.

SELECT classid, classes.NAME, 
       instructor, faculty.NAME AS facname, 
       building, buildings.NAME AS bname, room
  FROM schedule
  LEFT JOIN classes 
    ON classid = classes.id
  LEFT JOIN buildings
    ON building = buildings.id
  LEFT JOIN faculty
    ON instructor = faculty.id

Now look at this query, which is functionally equivalent to the preceding one, and answer the same question.

SELECT sk.classid, classes.NAME, 
       sk.instructor, faculty.NAME AS facname, 
       sk.building, buildings.NAME AS bname, sk.room
  FROM schedule AS sk
  LEFT JOIN classes 
    ON sk.classid = classes.id
  LEFT JOIN buildings
    ON sk.building = buildings.id
  LEFT JOIN faculty
    ON sk.instructor = faculty.id

Did you decide that the second query was easier to understand? The longer the query, the more I appreciate the inclusion of syntactically unnecessary correlation names.

P. S. I know that the word unrequired is not in the dictionary. I used it anyway in order to shorten the title.

P. P. S. For your reference, here is the data I used in developing these queries.

CREATE TABLE BUILDING
  ( ID    DEC (3,0),        
    NAME  CHAR(20));        

ID   NAME        
23   Decthee Hall
25   Offuv Center
41   Narrow Hall 
42   Vaux Hall   

CREATE TABLE CLASSES
  ( ID    DEC (3,0),       
    NAME  CHAR(20));       

ID   NAME                 
101   Antarctic Literature 
102   Pig Latin IV         
103   Rodeo Appreciation   
104   Antarctic History II 
105   Cooking with Guppies

CREATE TABLE FACULTY    
 (ID CHAR (3), NAME CHAR (20));

ID   NAME           
F01  Monella, Sal   
F02  Nootix, Herman 
F03  Andree, Polly  
F04  LeNoll, Ty     
F05  O'Var, Sam     
F06  Sillen, Penny  
F07  Doo, Billy     
F08  Fishul, Benny  

CREATE TABLE classes
   CLASSID DEC(3, 0),   
   PERIOD  CHAR (1),
   BUILDING DEC (3,0),   
   ROOM     DEC (4,0),   
  INSTRUCTOR CHAR (3))

CLASSID  PERIOD  BUILDING   ROOM   INSTRUCTOR
  101      A         41      320      F02    
  102      A         41      218      F03    
  103      B         41      212      F02    
  104      B         42      302      F05    
  105      C         41      165      F04    
  106      B         41      212      F06    
  107      B         42      302      F01    
  108      D         41      212      F07    
  109      D         41      320      F08    


                     Post this story to del.icio.us
               Post this story to Digg
    Post this story to Slashdot


Sponsored By
WORKSRIGHT SOFTWARE

Do you need area code information?
Do you need ZIP Code information?
Do you need ZIP+4 information?
Do you need city name information?
Do you need county information?
Do you need a nearest dealer locator system?

We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

Just call us and we'll arrange for 30 days FREE use of either
ZIP/CITY or PER/ZIP4.

WorksRight Software, Inc.
Phone: 601-856-8337
Fax: 601-856-9432
E-mail: software@worksright.com
Web site: www.worksright.com


Senior Technical Editor: Ted Holt
Technical Editors: Howard Arner, Joe Hertvik, Shannon O'Donnell, Kevin Vandever
Contributing Technical Editors: Joel Cochran, Wayne O. Evans, Raymond Everhart,
Bruce Guetzkow, Brian Kelly, Marc Logemann, David Morris
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

Sponsored Links

COMMON:  Join us at the annual 2008 conference, March 30 - April 3, in Nashville, Tennessee
BOSaNOVA:  Download our 'Best Practices for Securing your Backup' whitepaper
NowWhatJobs.net:  NowWhatJobs.net is the resource for job transitions after age 40


 

IT Jungle Store Top Book Picks

The System i RPG & RPG IV Tutorial and Lab Exercises: List Price, $59.95
The System i Pocket RPG & RPG IV Guide: List Price, $69.95
The iSeries Pocket Database Guide: List Price, $59.00
The iSeries Pocket Developers' Guide: List Price, $59.00
The iSeries Pocket SQL Guide: List Price, $59.00
The iSeries Pocket Query Guide: List Price, $49.00
The iSeries Pocket WebFacing Primer: List Price, $39.00
Migrating to WebSphere Express for iSeries: List Price, $49.00
iSeries Express Web Implementer's Guide: List Price, $59.00
Getting Started with WebSphere Development Studio for iSeries: List Price, $79.95
Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
Getting Started with WebSphere Express for iSeries: List Price, $49.00
WebFacing Application Design and Development Guide: List Price, $55.00
Can the AS/400 Survive IBM?: List Price, $49.00
The All-Everything Machine: List Price, $29.95
Chip Wars: List Price, $29.95


 
The Four Hundred
State of the System i: First-Hand Reports from Second-Hand Dealers

System i Sales Drop Again in Q3, IBM Says Little

IBM Hit by Financial Services Slowdown in Q3

Mad Dog 21/21: Symphony for the Devil

The Linux Beacon
Ubuntu Hits Launch Target for 7.10 Linux Release

Novell Delivers Workgroup Software Bundle for SMBs

Intel Is Back on Track in Q3, AMD Is Fighting to Get There

IBM Hit by Financial Services Slowdown in Q3

Four Hundred Stuff
Talend Adds i5/OS Support to Open Source ETL Tool

VAI to Deliver Flexible Computer-Telephone Integration, Thanks to iMS

LogLogic Delivers Fine-Grained User Activity Monitoring

NGS Launches Pre-Built Data Mart for Distributors

Big Iron
IBM Hit by Financial Services Slowdown in Q3

Top Mainframe Stories From Around the Web

Chats, Webinars, Seminars, Shows, and Other Happenings

System i PTF Guide
October 20, 2007: Volume 9, Number 42

October 13, 2007: Volume 9, Number 41

October 6, 2007: Volume 9, Number 40

September 29, 2007: Volume 9, Number 39

September 22, 2007: Volume 9, Number 38

September 15, 2007: Volume 9, Number 37

The Windows Observer
Office Communication Server 2007 Launched by Microsoft

Will OCS 2007 Live Up to the Hype?

Zend Puts Out New Release of Commercial-Grade PHP

Growing Businesses, Upgrades Drive IT Hiring in Q4

The Unix Guardian
Sun Elaborates on its xVM Virtualization Plans

Apple's Leopard Mac OS X Server Coming October 26

IBM Hit by Financial Services Slowdown in Q3

As I See It: Great Looking Genes

Four Hundred Monitor
Four Hundred Monitor's
Full iSeries Events Calendar

THIS ISSUE SPONSORED BY:

WorksRight Software
Help/Systems
Computer Measurement Group


Printer Friendly Version


TABLE OF CONTENTS
Good Reasons to Use Unrequired Correlation Names

Externally Described Database IO through Data Structures

Admin Alert: The System i High Availability Roadmap

Four Hundred Guru

BACK ISSUES

From the IT Jungle Forums
Finding *OUTFILE Template Files

i5/OS V5R4 Release Notes

MCH1202

Crashing processes!

SQL 'Hidden' Field