/    Sign up×
Community /Pin to ProfileBookmark

Benefits of using the keyword JOIN in SQL query explicitly?

Hi SQL experts,
I just figured out this query:

[code]SELECT fullname from an, Profiles
WHERE an.lid=Profiles.lid AND an.type=Profiles.type
AND Profiles.lid_P=99 AND Profiles.type_P=’Ulli’
[/code]

It works fine without using the keyword JOIN.
However when reading joined queries of others they are always using LEFT JOIN, INNER JOIN etc.
My question is: Is my version just a simplified one or are there benefits when using the JOIN instruction explicitly, e. g. better performance or hints to the RDBMS for optimising?

to post a comment

5 Comments(s)

Copy linkTweet thisAlerts:
@sibertDec 15.2020 — > @Sempervivum#1625929 SELECT fullname from an, Profiles

> WHERE an.lid=Profiles.lid AND an.type=Profiles.type

> AND Profiles.lid_P=99 AND Profiles.type_P='Ulli'


How long takes a query with JOIN compared to WHERE? It is all about speed.

``<i>
</i>SELECT fullname from an
LEFT JOIN Profiles ON an.lid=Profiles.lid
WHERE an.type=Profiles.type AND Profiles.lid_P=99 AND Profiles.type_P='Ulli'<i>
</i>
``
Copy linkTweet thisAlerts:
@SempervivumauthorDec 15.2020 — @sibert#1625930 The information in your original posting I read in my email notification is striking. Thanks for this info.
Copy linkTweet thisAlerts:
@NogDogDec 15.2020 — SELECT fullname from an, Profiles will always do an inner join on an and Profiles. If you ever want a different type of join, then you need to use the explicit JOIN syntax. I prefer to always use the explicit syntax so that it's always obvious to whoever might be reading the code (including me 12 months later) what was intended.

Whether there are any performance differences, I'd have to test it before I'd say one way or another. (DBMS's are pretty smart these days at optimizing queries.) Also, if you use naming conventions such that the columns to be joined on across tables are the same name, I like using the USING() syntax:
<i>
</i>SELECT a.col1, b.col2
FROM table_a a
INNER JOIN table_b b USING(table_a_id)
Copy linkTweet thisAlerts:
@SempervivumauthorDec 15.2020 — Hi nogdog and many thanks for this detailed answer, it's very informative.

In the meantime I tried to reproduce sibert's results and check the speed when using a subquery additionally. I used test tables of about 10,000 records each. As the global result phpmyadmin displayed always said "query took 0.0000 secs" I used the profiler. The result is that specifying LEFT JOIN improved the speed significantly, more than a factor of 10. And the speed when using a subquery is approximately the same as LEFT JOIN.

Unfortunately I was not able to find or switch on a sum-up of the detailed times so that it was a bit difficult to compare the overall times of the different queries.

I didn't dive into the types of JOIN yet but seems to be worthwile to do so.
Copy linkTweet thisAlerts:
@NogDogDec 15.2020 — > @Sempervivum#1625940 I didn't dive into the types of JOIN yet but seems to be worthwile to do so.

Yeah, the LEFT JOIN being faster is of no use if you want the functionality of an INNER JOIN. That being said, query performance/optimization is a somewhat arcane art. Next thing you know, you'll be diving into the mysterious world of EXPLAIN. :)

Most important starting rule of thumb: any columns being used in JOIN and/or WHERE clauses probably need to be indexed.
×

Success!

Help @Sempervivum spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...