How to use result set in "IN" Function

  • I would like to use thee result set in in function ex :

    Select * from table1 where column1 in (select column2 from table2)

    is it the right way of using it ?

  • Uhm, why don't you try it out?

    --------------------------------------------------------------------------
    A little knowledge is a dangerous thing (Alexander Pope)
    In order for us to help you as efficiently as possible, please read this before posting (courtesy of Jeff Moden)[/url]

  • Well i tried but its showing up the data for only one value instead of showing data for all the values(from second query )?

  • What you wrote should work.

    Note that you can re-write this query as a JOIN:

    SELECT t1.*

    FROM Table1 t1

    JOIN Table2 t2

    ON t1.Column1 = t2.Column2

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • Are you saying you want to see all columns from Table2 as well? Then Wayne's example only needs a tiny change:

    SELECT t1.*, t2.*

    FROM Table1 t1

    JOIN Table2 t2

    ON t1.Column1 = t2.Column2

    --------------------------------------------------------------------------
    A little knowledge is a dangerous thing (Alexander Pope)
    In order for us to help you as efficiently as possible, please read this before posting (courtesy of Jeff Moden)[/url]

  • Thanks Guys

  • WayneS (6/4/2010)


    What you wrote should work.

    Note that you can re-write this query as a JOIN:

    SELECT t1.*

    FROM Table1 t1

    JOIN Table2 t2

    ON t1.Column1 = t2.Column2

    This is not the same as the IN query. It will potentially procedure a different result if a value appears in multipl rows in table2.

  • True Nils, but since the OP's question wasn't exactly clear (and he didn't get any more specific later), both Wayne and I just took a stab at it.

    --------------------------------------------------------------------------
    A little knowledge is a dangerous thing (Alexander Pope)
    In order for us to help you as efficiently as possible, please read this before posting (courtesy of Jeff Moden)[/url]

  • I agree, the OP was unclear. I was not criticizing your answer, just pointing out that the two queries would return different results 🙂

  • Jan Van der Eecken (6/4/2010)


    Are you saying you want to see all columns from Table2 as well? Then Wayne's example only needs a tiny change:

    SELECT t1.*, t2.*

    FROM Table1 t1

    JOIN Table2 t2

    ON t1.Column1 = t2.Column2

    Good call Jan!

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply