Avatar of sbornstein2
sbornstein2
 asked on

Basic SQL Query Select Question FK tables

Hello all,

Okay this is a basic question but want the best practice.   If I have a FK ID field in my main query FROM table that joins to that PK table.    I know it does not matter but best practice the ID field should that reference the main FROM table or the joined PF table.   For example,

SELECT
IV.InvoiceGroupID,   --should this use the main table InvoiceGroupID or the IG table best practice
IG.InvoiceGroupName
FROM Invoice IV
JOIN InvoiceGroup IG ON IV. InvoiceGroupID = IG.InvoiceGroupID

Also one other thing is the JOIN which table should always be the first in the ON statement?   Sometimes I find myself mixing them.

Again all best practice I am looking for.

Thanks all
Microsoft SQL Server

Avatar of undefined
Last Comment
sbornstein2

8/22/2022 - Mon
Jim Horn

Since the JOIN..ON query forces these two be the same value, and both columns are available when the SELECT clause processes, I don't believe it matters.

>Also one other thing is the JOIN which table should always be the first in the ON statement?  
I haven't heard of an instance where this matters, but I almost always see JOIN ... ON left side object.column = right side object.column.
David Todd

Hi

Here's a suggestion for performance on Foreign key joins: Consider adding a non-clustered index on the foreign key column. The other side of the join is usually a primary key, which is likely to be the clustered index of that table. Primary key constraints are implemented by an index. Foreign key constraints are not automatically indexed.

HTH
  David
ASKER CERTIFIED SOLUTION
Scott Pletcher

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Vitor Montalvão

sbornstein2, do you still need help with this question?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
sbornstein2

ASKER
Thanks