Sub query alternative

  • Can the query at the bottom of the post be written without using a sub-query?  Thanks.

    ---------------------------------------------------

    declare @Tab1 table (Tab1ID int identity(1,1),Tab1Text varchar(10))

    declare @Tab2 table (Tab2ID int identity(1,1),Tab1ID int)

    insert into @Tab1 select 'T1'

    insert into @Tab1 select 'T2'

    insert into @Tab1 select 'T3'

    insert into @Tab1 select 'T4'

    insert into @Tab1 select 'T5'

    insert into @Tab2 select 1

    insert into @Tab2 select 3

    --Can this query be re-written without using a sub query?

    select t1.*

    from @Tab1 t1

    where t1.Tab1ID not in (select t2.Tab1ID from @Tab2 t2)

  • select t1.Tab1ID

    from @Tab1 t1

    inner join @Tab2 t2 on t2.Tab1ID = t1.Tab1ID

  • What about:

    select t1.*

    from @Tab1 t1

    left join @Tab2 t2 on t1.Tab1ID = t2.Tab1ID

    where t2.Tab2ID is null

  • You may have misread my query.  My query will return records in Tab1 that are NOT in Tab2.  Your query will return the records that are in both.

  • That's it.  Thanks

  • Try Adrienne's.  That works.   

    I wasn't born stupid - I had to study.

Viewing 6 posts - 1 through 5 (of 5 total)

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