[thelist] basic SQL question...

Aredridel aredridel at nbtsc.org
Fri Aug 22 23:02:08 CDT 2003


On Fri, 2003-08-22 at 21:51, jsWalter wrote:
> Can you tell me why 'A' is better or worse than 'B' ?
> 
> Just trying to find out what is best to use and when.
> 
> 
> SQL sample 'A'
>    SELECT * 
>     FROM img_items, img_cat 
>     WHERE img_id = 20
>     AND img_items.cat_id = img_cat.cat_id;
> 
> SQL sample 'B'
>    SELECT * 
>     FROM img_items
>     NATURAL JOIN img_cat
>     WHERE img_items.img_id = 20;

They're the same thing, though not every SQL dialect supports NATURAL
JOIN.

A natural join is just a shorthand for the explicit join above -- and in
my opinion, prettier.  Some dialects also support an explicit JOIN
that's not in the where clause:

    SELECT *
      FROM img_items
      JOIN img_cat ON (img_cat.cat_id = img_items.cat_id)
      WHERE img_items.img_id = 20

I like that one best -- both explicit, and keeping the JOINs out of the
filter (WHERE) clause, which makes me happy.

Ari


More information about the thelist mailing list