0

Not sure how to ask this question, so I ask by giving scenario.

Select ID, CompanyID, Company_name from memberdetails where status in ('A','S');

the query returns say 1000 rows. Now I want to search the table industry_catagory with the result under column CompanyID from above. How can I create this is one single sql statement.

3
  • Do you need the results from the first query as well? Or do you just need the CompanyID so you can use it for the second query? Commented Nov 16, 2016 at 19:43
  • 1
    Join to the two tables together on CompanyId. Commented Nov 16, 2016 at 19:44
  • You could join or use a subquery. Use the query provided as a subquery WHERE companyID IN ( Select ID, CompanyID, Company_name from memberdetails where status in ('A','S')) Commented Nov 16, 2016 at 19:45

2 Answers 2

2

This should be using and more up to date Join syntax:

SELECT CompanyID 
FROM industry_catagory a 
INNER JOIN memberdetails b ON a.CompanyID = b.CompanyID
WHERE b.status in ('A','S')
Sign up to request clarification or add additional context in comments.

Comments

1

Use one of the next approaches:

Sub-Query:-

Select CompanyID from industry_catagory 
where CompanyID in (Select CompanyID from memberdetails where status in ('A','S'))

Join:-

Select CompanyID 
from industry_catagory a , memberdetails b
where a.CompanyID = b.CompanyID
and b.status in ('A','S')

1 Comment

Promote the use of explict JOIN sintaxis, Aaron Bertrand wrote a nice article Bad habits to kick : using old-style JOINs about it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.