Search code examples
sqlsql-server-2000

SQL query - filter primary keys


I am using microsoft sql server and my query is of the following format (table1 primary key is (A,B)):

Select table1.A, table1.B, table2.C, Table3.D
from table1
left outer join table2 on table1.A = table2.A
left outer join tabl3 on table1.B = table3.B

//Here comes the question
//Except (A,B) in ( select A,B from BadTable)

How could I do it nicely? I was thinking about finding the set of CORRECT keys using sth like (cannot use except - running sql server 2000)

Select A,B
from table1
not exists ( select A,B from bad table)

and then inner join that with the main query. What do you think?


Solution

  • Select table1.A, table1.B, table2.C, Table3.D
    from table1 T1
    left outer join table2 on table1.A = table2.A
    left outer join tabl3 on table1.B = table3.B
    where not exists (select 1 from badTable where A = T1.A and B = T1.B)
    

    would be a good way