SELECT ArticleID, ArticleTitle
FROM Articles a
JOIN dbo.PressKitMagazines pm
ON a.MagazineID = pm.MagazineID AND pm.PressKitID = @PressKitID
JOIN dbo.PressKitClass pc
ON a.ClassID = pc.ClassID AND pc.PressKitID = @PressKitID
You do not need to involve Magazines
, Class
or PressKit
because you do not need to return the Name
s associated with their ID
s. You didn't need them in your original query either:
SELECT ArticleID, ArticleTitle
FROM Articles
WHERE MagazineID IN (
SELECT MagazineID
FROM dbo.PressKitMagazines
WHERE PressKitID = @PressKitID )
AND ClassID IN (
SELECT classID
FROM dbo.PressKitClass
WHERE PressKitID = @PressKitID )
Suppose that table T
holds the rows that make some statement parameterized by its columns into a true statement. Eg Articles
holds the rows that satisfy:
"article ArticleID titled ArticleTitle was in magazine MagazineID & was of class ClassID"
or in shorthand:
Article(ArticleID, ArticleTitle, MagazineID, ClassID)
Inside an SQL SELECT statement, a table's column names are prefixed by an alias and a dot (which can be left out when there's no ambiguity):
a(a.ArticleID, a.ArticleTitle, a.MagazineID, a.ClassID)
So the rows in:
SELECT C FROM U WHERE C IN (SELECT C FROM T WHERE
condition
)
or with prefixes and dots:
SELECT U.C FROM U WHERE U.C IN (SELECT T.C FROM T WHERE
condition
)
are the rows where:
SELECT U.C FROM U JOIN T ON
condition
AND U.C = T.C
because they both return rows with just column C where:
U(...,U.C,...) AND T(...,T.C,...) AND condition AND U.C = T.C
For composing queries see this answer.