PDA

View Full Version : SQL formatting opinion


satis
8-19-09, 04:54 PM
ok, so just curious what people think.... which way is better to write a multi-table join?

1:
SELECT a.rowid, a.id
FROM queueUsers a
JOIN queueTypes b
on a.type = b.userType
JOIN queue c
on b.rowid = c.queuetype
and c.rowid = 3
JOIN queueUserAccess d
on a.access = d.accessid
and d.description = 'user'

2:
SELECT a.rowid, a.id
FROM queueUsers a
JOIN queueTypes b
on a.type = b.userType
JOIN queue c
on b.rowid = c.queuetype
JOIN queueUserAccess d
on a.access = d.accessid
WHERE c.rowid = 3
AND d.description = 'user'


They're both valid and they both work. One just puts all the conditional items into the where clause and the other in the join statements. The only thing that would change programmatically is c.rowid... I guess I could see the argument for putting that into a trailing where clause and the d.description into the JOIN. Anyway, just curious if anyone has an opinion.

YvetteKuhns
8-19-09, 05:11 PM
I think it is easier to read the second method but some people suggest that using ON instead of WHERE is better. It depends on the situation, depending upon where your data is stored and when each condition is executed. If you compared first names, then last names, you might have more results (unless you are talking about a family database) than if you compared last names, then first names in a name search.

Croc Hunter
8-22-09, 10:07 AM
For overhead 2. For user expandability 1.