Ian McCullough
Ian McCullough

Reputation: 1443

SQL subqueries or inner join?

I am building a new web application and have had a basic understanding of sql queries but I am stuck in this particular query.

These are the two tables I will be using this query. The "followingID" in the first table points to the "id" of the second table.

dbo.T_FOLLOWING_GROUP
-id
-groupLookupID
-followingID
-order

dbo.FOLLOWING
-id
-userID
-followingID

I need to get all the rows in dbo.FOLLOWING based on known values that I have of "userID" and "groupLookupID".

Is this enough information to go off of? I know there are ways of doing

`SELECT * FROM dbo.FOLLOWING 
 WHERE (insert select subquery here)`

Or is there some sort of inner join I need to use?

Upvotes: 1

Views: 107

Answers (2)

M.Ali
M.Ali

Reputation: 69494

INNER JOIN

SELECT AnyTableName.AnyColumnName1, AnyTableName.AnyColumnName2, ..... --- out these two tables
    FROM dbo.T_FOLLOWING_GROUP INNER JOIN dbo.FOLLOWING
    ON dbo.T_FOLLOWING_GROUP.followingID = dbo.FOLLOWING.id

And then you can add WHERE Clause in the end to futher filter the result set

IN Query

SELECT Column1, Column2, Column3, ...........
FROM dbo.T_FOLLOWING_GROUP
WHERE followingID IN (
                        SELECT DISTINCT id FROM dbo.FOLLOWING
                      )

Upvotes: 1

promanski
promanski

Reputation: 567

To have all in one query (using joins) try this:

SELECT f.* 
FROM dbo.FOLLOWING f 
LEFT JOIN dbo.T_FOLLOWING_GROUP fg on (f.id=fg.followingID)
WHERE f.userID=xxx AND fg.groupLookupID=yyy

(use your known values in place of xxx and yyy).

Upvotes: 0

Related Questions