Jump to content

table join best practice


jcombs_31

Recommended Posts

I'm no DB admin, but I've always written my queries like this to join say 2 tables:

 

SELECT *
FROM tbl1, tbl2
WHERE tbl1.key = tbl2.key

 

What is the difference in using something like INNER JOIN

 

SELECT *
FROM tbl1 INNER JOIN tbl2
ON tbl1.key = tbl2.key

 

There are a few different options with joins but by habit I've always used the first method. What are the benefits of using joins and when is it best to use one over another?

Link to comment
Share on other sites

Don't use the first method (also called theta style). -- edit: typo

 

First, it's very hard to mix comma with other JOINs.

Second, commas have different predence than JOINs, which is a nightmare.

Third, you end up munging the where clause with join conditions, which is hard to read.

Fourth, you can't leave off a join condition easily... but leaving off part of a where clause it very easy to do by accident.

Fifth, it's much easier to add extra tables to the second style.

 

Basically, it causes problem and is of no benefit whatsoever.

Link to comment
Share on other sites

@fenway

 

First, it's very hard to mix comma with other JOINs.

 

I don't get what you are trying to say. Of course, with JOINs, you will not use comma for specifying the joining of tables because that's the way it is. And its not even hard at all.

 

Second, commas have different predence than JOINs, which is a nightmare.

 

you mean precedence? You do not need to use comma separation in specifying tables when you do JOIN.

 

Third, you end up munging the where clause with join conditions, which is hard to read.

 

I never have a problem of having hard to read at all, actually its even more readable (for my case) than without JOIN. Why? Cause you need to specify everything in WHERE clause to see the connection/association where with JOIN you can see right away after you specify the table to connect to.

 

Fourth, you can't leave off a join condition easily... but leaving off part of a where clause it very easy to do by accident.

 

I don't get your point? If you are to specify the connection/association of the tables, why put it on WHERE? you specify it after JOIN with ON. And that's the trouble you get with not using JOIN since you will accidentally get the wrong set of information.

 

Fifth, it's much easier to add extra tables to the second style.

 

Yes, it maybe easier to write since you only need to add a comma and the table name (and alias) but I never had trouble doing some (I don't think it's really that hard).

 

 

Also, you are doing with INNER JOIN for that certain example. The trouble is that when you try switching to another type of JOIN, say LEFT JOIN. doing that with the previous one is more tedious compared with theta style (I didn't know it was called this way, another input, thanks), you only need to replace INNER with LEFT or with anything else (so long as JOIN).

 

Am doing no offense here, am only giving my opinion. :)

Link to comment
Share on other sites

Well, I appreciate the feedback.  I never paid much attention to the difference between all the joins, but I'll read up on it and see if it makes more sense.  I usually work in relatively small databases so it has never become an issue, but I see a lot of people with all these different join statements so it sparked some curiosity. 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.