Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using IN operator with a sub-query

Status
Not open for further replies.

gamingmouse

Programmer
May 26, 2006
7
US
I'm familiar with

Code:
SELECT *
FROM my_table
WHERE id IN (2,4,8)

But how can I do something like this:

Code:
SELECT *
FROM my_table
WHERE id IN (
  SELECT *
  FROM my_table
  WHERE x = 1
);

Please IGNORE that the subquery in the above example is totally unneeded. This is a purely snyntactical question.

Thanks,
gm
 
SELECT *
FROM my_table
WHERE id IN (
SELECT [!]id[/!]
FROM my_table
WHERE x = 1
);

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
What if the Select statement is a saved Query. That is, it does not work if I save

Code:
  SELECT id
  FROM my_table
  WHERE x = 1;

as MyQuery

and then try to do:
Code:
SELECT *
FROM my_table
WHERE id IN (MyQuery);
 
Seems you're playing with ms-access.
A saved query is like a VIEW in real database world, so use it as it were a TABLE.
Note: for JetSQL or Access query issues post in forum701

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top