Skip to content Skip to sidebar Skip to footer

Nested Inner Join Query

I have four tables called attr, data, extradata and syst. I have to do multiple/nested inner joins to get some attributes from all 4 tables, and running into issues because of that

Solution 1:

Assuming attr.ID maps to data.ID, you can simply join all of the tables together and all of the conditions go in your WHERE clause:

SELECT TOP(10) a.ID 
FROM attr AS a 
    INNER JOIN data AS X ON a.ID = x.ID
    INNER JOIN extradata XA ON X.dataID = XA.dataID
    INNER JOIN syst AS s ON a.sysID = s.sysID
WHERE X.data = 'condition1' andNOT XA.additionaldata = 'condition2'and s.desc = 'condition3'

Joining attr to data allows you to also join attr to extradata, because data becomes the link between all 3.

Post a Comment for "Nested Inner Join Query"