Sql-server – Select rows from two joined table that not in a third table

join;sql server

I have 3 table A,B,C,D with the same columns and I'm trying to retrieve the 
user_id that are in table a and b but not c.

For instance:
Table A                       
user_id     closed acct
10102345     Yes
12456786     Yes

Table B             
user_id  closed acct  
10102345     Yes
12456786     Yes  

Table C
user_id  closed acct
45345696     No
45698788     Yes
12456786     Yes

Here is what the code that I try but it taking so long to execute:
SELECT user_id FROM TableA WHERE user_id NOT IN (SELECT DISTINCT user_id
FROM TableB) OR user_id NOT IN (SELECT DISTINCT ID FROM Table C)

Best Answer

SELECT A.*, B.*
FROM A 
JOIN B ON A.user_id = B.user_id
WHERE NOT EXISTS ( SELECT NULL
                   FROM C
                   WHERE C.user_id = B.user_id )

or

SELECT A.*, B.*
FROM A 
JOIN B ON A.user_id = B.user_id
LEFT JOIN C ON C.user_id = B.user_id
WHERE C.user_id IS NULL