How to give the output of the first query(which has two values) as the input to the second?

abhijithln picture abhijithln · Jan 20, 2011 · Viewed 26.6k times · Source

i get 2 names as the output of the first query.... eg: paul,peter now this should be the input for the second query, which has to display paul's and peter's email ids....

Answer

Alexander Malakhov picture Alexander Malakhov · Jan 20, 2011

For nested queries I would strongly recommend WITH clause. It makes long complex queries order of magnitude easier to understand / construct / modify:

WITH 
   w_users AS( -- you can name it whatever you want
      SELECT id
        FROM users
       WHERE < long condition here >
   ),
   w_other_subquery AS(
      ...
   )
SELECT email_id
  FROM ...
 WHERE user_id IN (SELECT id FROM w_users)