I have the following SQL query.
SELECT c.course_id, c.cat_id, cc.cat_name, c.created_date, c.title, c.description, c.notify, c.copyright, c.icon, c.release_date, c.primary_language,
c.end_date, c.banner FROM %scourses c, %scourse_cats cc, %scourse_enrollment ce WHERE c.cat_id = cc.cat_id AND ce.course_id = c.course_id;
How do I re-factor the same using JOIN?
r937
2
SELECT c.course_id
, c.cat_id
, cc.cat_name
, c.created_date
, c.title
, c.description
, c.notify
, c.copyright
, c.icon
, c.release_date
, c.primary_language
, c.end_date
, c.banner
FROM %scourses c
INNER
JOIN %scourse_cats cc
ON cc.cat_id = c.cat_id
INNER
JOIN %scourse_enrollment ce
ON ce.course_id = c.course_id
Hi Rudy,
Could you tell me which query is more efficient?
Also, what is the difference between inner join, right and left join (in this context)?
r937
4
both inner joins (comma list style or JOIN syntax style) are equally efficient
as for the difference between inner, left, and right, please use google – those joins have fundamentally different semantics
So, the query that I provided and the one that you gave are essentially the same, just with different syntax?
r937
6
yes, except the JOIN syntax is ~way~ better
Thanks. I see the bigger picture now.
P.S. I just got your book. Seems I gotta learn some stuff
But isn’t ON table1.pk = table2.fk more efficient than the WHERE table1.pk = table2.fk because of the order it’s evaluated?
I might be wrong, I’m just wondering for my own knowledge. I thought I remembered reading this somewhere once.
r937
9
no, i don’t think so
use EXPLAIN on your real world examples (using both styles of join) to confirm