tags:

views:

41

answers:

1
+2  Q: 

sql join syntax

I'm kind of new to writing sql and I have a question about joins. Here's an example select:

select bb.name from big_box bb, middle_box mb, little_box lb
where lb.color = 'green' and lb.parent_box = mb and mb.parent_box = bb;

So let's say that I'm looking for the names of all the big boxes that have nested somewhere inside them a little box that's green. If I understand correctly, the above syntax is another way of getting the same results that we could get by using the 'join' keyword.

Questions: is the above select statement efficient for the task it's doing? If not, what is a better way to do it? Is the statement syntactic sugar for a join or is it actually doing something else?

If you have links to any good material on the subject I'd gladly read it, but since I don't know exactly what this technique is called I'm having trouble googling it.

+3  A: 

You are using implicit join syntax. This is equivalent to using the JOIN keyword but it is a good idea to avoid this syntax completely and instead use explicit joins:

SELECT bb.name
FROM big_box bb
JOIN middle_box mb ON mb.parent_box = bb.id
JOIN little_box lb ON lb.parent_box = mb.id
WHERE lb.color = 'green'

You were also missing the column name in the join condition. I have guessed that the column is called id.

This type of query should be efficient if the tables are indexed correctly. In particular there should be foreign key constraints on the join conditions and an index on little_box.color.

An issue with your query is that if there are multiple green boxes inside a single box you will get duplicate rows returned. These duplicates can be removed by addding DISTINCT after SELECT.

Mark Byers
The "implicit" syntax is ANSI-89; the "explicit" is ANSI-92
OMG Ponies
Thorough answer, thanks for the tips. That was sloppy to leave off the bb.id (or something). Ok follow-up question: can I specify conditions from any of the tables in the where clause? ie: WHERE lb.color = 'green' and mb.color = 'red'
Nathan Spears
Also, googling for the implicit join, there are some observations that implicit join is "old and outdated'. Is that a consensus opinion among the wise?
Nathan Spears
@Nathan Spears: Most people prefer the explicit syntax. See for example http://stackoverflow.com/questions/44917/explicit-vs-implicit-sql-joins and http://stackoverflow.com/questions/1018822/inner-join-versus-where-clause-any-difference
Mark Byers
Thx! Not to be a nag but you missed my first question.
Nathan Spears
@Nathan Spears: Is your first question *is the above select statement efficient for the task it's doing?*. My answer is *This type of query should be efficient if the tables are indexed correctly.* Depending on the distribution of your data there might be better ways to do it, but it's certainly a good start and should perform well in most situations.
Mark Byers
I meant my first question in the comments: can I specify conditions from any of the tables in the where clause (of the select you suggested)? ie: WHERE lb.color = 'green' and mb.color = 'red'
Nathan Spears
@Nathan Spears: Yes you can.
Mark Byers