Better "Join" vs "Where" clause?

E

EagleOne

2003, 2007


At the EOD, it appears that a WHERE clause accomplishes the same data set as an [INNER] Join.

What are the benefits of one over the other?


Also, it does not appear that the use of [ or ] preceding / following Table names and/or Field
names changes data set results.

Is the use of [ or ] a convention for reviewing code or are there substantive issues in some
end-use cases?

TIA EagleOne
 
S

Sylvain Lafontaine

Well, if you don't display any field from the second table when you are
joining two tables then yes, an INNER JOIN might give you the apparence that
it is doing the same job as a WHERE clause. Start displaying some fields
from the second table and you'll start to feel the difference between a JOIN
and a WHERE clauses.

For [], it's better not to use them because your code will be more readable;
however, if you have some special characters such as blank spaces, dot or $
in the name of your tables or fields then you must use them:

Select [My little table].[My first field] from [My little table] ...

Not sure but I think that you must also use them if the name of a table or
of a field is the same as of a reserved word; for example if you have a
field called Date instead of something less confusing like Date1, EndDate,
etc. When you'll have many tables and complexe relationships, using aliases
for your tables will also be a very good idea:

Select LT.[My first field] from [My little table] as LT ....

--
Sylvain Lafontaine, ing.
MVP - Windows Live Platform
Email: sylvain aei ca (fill the blanks, no spam please)
Independent consultant and remote programming for Access and SQL-Server
(French)
 
J

John W. Vinson

2003, 2007


At the EOD, it appears that a WHERE clause accomplishes the same data set as an [INNER] Join.

What are the benefits of one over the other?

A Query using a JOIN is much more likely to be updateable than one using a
WHERE, and I've heard that it can also be faster (since the query optimizer
makes better use of indexes).
Also, it does not appear that the use of [ or ] preceding / following Table names and/or Field
names changes data set results.

It certainly shouldn't!!!!
Is the use of [ or ] a convention for reviewing code or are there substantive issues in some
end-use cases?

The brackets are required if the fieldname contains blanks or other special
characters (which you should probably avoid doing in any case), or if you
(again, unwisely) use a reserved word such as Date or Group as a fieldname.
IME they are allowed anytime and never seem to do any harm; I'll generally
bracket the fieldnames just to make it obvious that they ARE fielnames,
especially if I'm building a SQL string in code.
 
E

EagleOne

Thanks John

John W. Vinson said:
2003, 2007


At the EOD, it appears that a WHERE clause accomplishes the same data set as an [INNER] Join.

What are the benefits of one over the other?

A Query using a JOIN is much more likely to be updateable than one using a
WHERE, and I've heard that it can also be faster (since the query optimizer
makes better use of indexes).
Also, it does not appear that the use of [ or ] preceding / following Table names and/or Field
names changes data set results.

It certainly shouldn't!!!!
Is the use of [ or ] a convention for reviewing code or are there substantive issues in some
end-use cases?

The brackets are required if the fieldname contains blanks or other special
characters (which you should probably avoid doing in any case), or if you
(again, unwisely) use a reserved word such as Date or Group as a fieldname.
IME they are allowed anytime and never seem to do any harm; I'll generally
bracket the fieldnames just to make it obvious that they ARE fielnames,
especially if I'm building a SQL string in code.
 
D

David W. Fenton

A Query using a JOIN is much more likely to be updateable than one
using a WHERE, and I've heard that it can also be faster (since
the query optimizer makes better use of indexes).

I don't know about updateability, but your statement about the query
optimizer is false. An explicit join and an implicit join (i.e.,
using the equivalent WHERE clause) are optimized by Jet EXACTLY THE
SAME, so there is going to be no difference in performance at all.
 
E

EagleOne

In my current situation, I do not need update-ability. But I have learned quite a bit from the
comments to my question. Thanks!
 
R

Rick Brandt

In my current situation, I do not need update-ability. But I have
learned quite a bit from the comments to my question. Thanks!

For many queries it comes down only to preference. I prefer join syntax
because that is more self-documenting about how the input sources
"relate" to each other leaving the WHERE clause to document selection
criteria.
 
B

Bob Barrows

2003, 2007


At the EOD, it appears that a WHERE clause accomplishes the same data
set as an [INNER] Join.

What are the benefits of one over the other?
In Access, except for readability, probably none, unless there ahve been
some changes in A2007 that I don't know about.

If you ever plan to upgrade to SQL Server, you had better get used to using
the JOIN syntax, especially for outer joins.
 
E

EagleOne

Documentation is important. I'll add that thought as I try to improve.

Thanks EagleOne
 
D

David W. Fenton

For many queries it comes down only to preference. I prefer join
syntax because that is more self-documenting about how the input
sources "relate" to each other leaving the WHERE clause to
document selection criteria.

I only use implicit joins where it's absolutely necessary. One such
situation is if you need to join on a GUID field.
 
D

David W. Fenton

If you ever plan to upgrade to SQL Server, you had better get used
to using the JOIN syntax, especially for outer joins.

Good advice in principle, but in fact, Jet SQL's join syntax is
different from SQL Server's (and just about any other database
engine), so it won't necessarily upsize better just because you've
used a join.

And so far as I know, SQL Server also optimizes implicit joins
identically to explicit ones. In fact, it seems to me that because
of Jet's oddball join syntax, it might be more portable to use
implicit joins, since there's a lot more in common in terms of WHERE
clauses than JOINs.
 
R

Rick Brandt

Good advice in principle, but in fact, Jet SQL's join syntax is
different from SQL Server's (and just about any other database engine),
so it won't necessarily upsize better just because you've used a join.

What is different about it other than a very liberal use of parenthesis?
 
B

Bob Barrows

David said:
Good advice in principle, but in fact, Jet SQL's join syntax is
different from SQL Server's (and just about any other database
engine), so it won't necessarily upsize better just because you've
used a join.

Sorry, but this just isn't true. SQL Server handles Jet's plethora of
parentheses quite handily, thank you. It's the other way around: joins that
work nicely in T-SQL fail miserably in Jet due to its insistence on using
parentheses to force the FROM clause to consist of a single <table
expression> to said:
And so far as I know, SQL Server also optimizes implicit joins
identically to explicit ones. In fact, it seems to me that because
of Jet's oddball join syntax, it might be more portable to use
implicit joins, since there's a lot more in common in terms of WHERE
clauses than JOINs.

You may have a point with inner joins, but with outer joins, using the JOIN
syntax vs the WHERE syntax can cause different results to be returned. In
fact, the *= syntax for performing outer joins implicitly in the WHERE
clause has been deprecated, mainly for this reason.
 
M

Michel Walsh

I prefer join over where for better updatability, and also because Jet does
not allow (the deprecated) outer join syntax in the where clause.

As mentioned, use [ ] around ill formed name, or when you build SQL string
on the fly, where the user supplies the name:

str = "... FROM [" & suppliedTableName & "] ... "


(Assuming such practice is acceptable in the first place).



Vanderghast, Access MVP
 
D

David W. Fenton

What is different about it other than a very liberal use of
parenthesis?

I have never bothered to figure it out, but the fact is that you
can't cut and paste every Jet join into SQL server and expect it to
work unchanged.
 
D

David W. Fenton

Sorry, but this just isn't true. SQL Server handles Jet's plethora
of parentheses quite handily, thank you.

Sorry, Bob, but it's simply not true that every valid Jet SQL join
will work unchanged in SQL Server (well, unless you're using ANSI 92
SQL mode in Access, which most people are not). I've encountered
plenty of Jet joins that didn't work in SQL Server.

Of course, maybe we're talking past each other -- I'm talking about
running the SQL directly, in the SQL Server tools, rather than
running it in Access via ODBC (which takes care of any
incompatibilities).
It's the other way around: joins that
work nicely in T-SQL fail miserably in Jet due to its insistence
on using parentheses to force the FROM clause to consist of a
single <table expression> to <table expression> join.

Sure, that's true, but not relevant to the question of explicit
joins being more compatible than implicit joins.

If you want your SQL to be upsizable to SQL Server with the least
trouble, the best thing to do is not to muck around with implicit
vs. explicit joins, but to switch Access to use ANSI 92 SQL.
You may have a point with inner joins, but with outer joins, using
the JOIN syntax vs the WHERE syntax can cause different results to
be returned.

Examples, please.
In
fact, the *= syntax for performing outer joins implicitly in the
WHERE clause has been deprecated, mainly for this reason.

Sorry, but I don't know what you're referring to with "*=". I don't
recognize that as valid Jet SQL.
 
B

Bob Barrows

David said:
I have never bothered to figure it out, but the fact is that you
can't cut and paste every Jet join into SQL server and expect it to
work unchanged.

That's interesting. I've never run into anything like that. I just tried a
couple different examples which worked fine in my SQL2000 server.
Do you have an example of one that would fail?
 
B

Bob Barrows

David said:
Of course, maybe we're talking past each other -- I'm talking about
running the SQL directly, in the SQL Server tools, rather than
running it in Access via ODBC (which takes care of any
incompatibilities).

No, I'm talking about importing the Access tables into SQL Server, and
pasting the SQL that Access generated into SQL Query Analyzer.
If you want your SQL to be upsizable to SQL Server with the least
trouble, the best thing to do is not to muck around with implicit
vs. explicit joins, but to switch Access to use ANSI 92 SQL.

I've never touched that option
Examples, please.

Sure, they're easy enough to find in BOL ... let's see ... ah, here we go -
it's long so I'm putting it at the bottom
Sorry, but I don't know what you're referring to with "*=". I don't
recognize that as valid Jet SQL.

It isn't - I should have said "... the *= syntax for performing outer joins
implicitly in the
WHERE clause in SQL Server ..."

AFAIK one cannot perform an outer join in Access without using the explicit
JOIN syntax.

****paste from SQL BOL***********************************************
The rows selected by a query are filtered first by the FROM clause join
conditions, then the WHERE clause search conditions, and then the HAVING
clause search conditions. Inner joins can be specified in either the FROM or
WHERE clause without affecting the final result.

Outer join conditions, however, may interact differently with the WHERE
clause search conditions, depending on whether the join conditions are in
the FROM or WHERE clause. Therefore, the ability to specify Transact-SQL
outer joins in the WHERE clause is not recommended, is no longer documented,
and will be dropped in a future release.

For example, these queries both specify a left outer join to SELECT 23 rows
that display the title identification number, title name, and the number of
books sold:

-- Join in WHERE clause.
USE pubs
SELECT t.title_id, t.title, s.qty
FROM titles AS t, sales AS s
WHERE t.title_id *= s.title_id

-- Join in FROM clause.
USE pubs
SELECT t.title_id, t.title, s.qty
FROM titles AS t LEFT OUTER JOIN sales AS s
ON t.title_id = s.title_id
In this query, a search condition is also specified in the WHERE clause:

-- Join and search condition in WHERE clause.
USE pubs
SELECT t.title_id, t.title, s.qty
FROM titles AS t, sales AS s
WHERE t.title_id *= s.title_id
AND s.stor_id = '7066'
The condition stor_id = '7066' is evaluated along with the join. The join
only selects the rows for stor_id 7066 from the sales table, but because it
is an outer join null values are supplied as the store information in all
the other rows. This query returns 18 rows.

The join condition can be moved to the FROM clause, and the stor_id
condition left in the WHERE clause:

USE pubs
SELECT t.title_id, t.title, s.qty
FROM titles AS t LEFT OUTER JOIN sales AS s
ON t.title_id = s.title_id
WHERE s.stor_id = '7066'
This query returns only two rows because the restriction of stor_id = '7066'
is applied after the left outer join has been performed. This eliminates all
the rows from the outer join that have NULL for their stor_id. To return the
same information with the join condition in the FROM clause, specify the
stor_id = '7066' condition as part of the ON join_criteria section in the
FROM clause and remove the WHERE clause:

USE pubs
SELECT t.title_id, t.title, s.qty
FROM titles AS t LEFT OUTER JOIN sales AS s
ON t.title_id = s.title_id
AND s.stor_id = '7066'
 
M

Michel Walsh

Outer join with a condition in the on clause implying only one table. The
result differs even among Jet versions (and patches):

SELECT authors.*
FROM authors LEFT JOIN books ON authors.authorID = books.authorID AND
authors.City = 'Iqaluit'



as example. With Jet 3.5, you got no record, but with MS SQL Server, you get
all records from authors.



There is also the case of update through joins, which is immediately
available with JET, but with proprietary syntax for MS SQL Server (or
explicitly over a view).


Not linked to join, but you cannot use ALIAS over alias in MS SQL Server,
while you can in Jet:

SELECT price * 1.06 AS TPS, (price + TPS) * 1.045 AS PST, price +TPS
+ PST AS totalPrice FROM somewhere


Other mundane details, too.


Vanderghast, Access MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top