Buddy Holly Buddy Holly - 4 months ago 8
SQL Question

SQL Query, Multiple Selections

Assume the data is of the format

ID Category
14424342 241
14424342 17
14234154 243
. .
. .


I want to create a table that has only the Ids and categories where Cat = 241

for the query I have

SELECT ID & Category FROM Table1 WHERE Category = 241


That returns

ID Category
14424342 241


But I also want the query to show every category corresponding to that ID.

Something that would return

ID Category
14424342 241
14424342 17
14424342 41
15238991 241
15238991 91
. .


Almost as if it were an if statement, if Cat = 241 return ID and Show each category associated with the ID

Answer

You could use a subquery of some sorts.

select ID, Category
from Table1
where ID in (select ID from Table1 where Category = 241)
Comments