There are no 2 primary keys in the tables. Some have composite primary keys, and this seems to be the case here. If the Cust table has (siteid, custid) as a PRIMARY KEY :
CREATE TABLE Cust ( siteid nvarchar(2), custid int, fname varchar(30), lname varchar(30), PRIMARY KEY (siteid, custid)
Then your foreign key definitions are wrong. You must have one (composite) foreign key referencing the primary key (composite):
CREATE TABLE Children ( childid1 nvarchar(2), childid2 int, siteid nvarchar(2), custid int, fname varchar(30), lname varchar(30), PRIMARY KEY (childid1, childid2), FOREIGN KEY (siteid, custid)
Then one way to write your query would be (Bugfix: this is ANSI SQL and works in another DBMS, but not in SQL-Server):
SELECT * FROM Cust WHERE (siteid, custid) NOT IN ( SELECT siteid, custid FROM Children ) ;
Or better with NOT EXISTS because it avoids the possible NULL errors that will make the NOT IN version display the expected results (and secondly, since NOT IN does not work in SQL-Server :)
SELECT * FROM Cust AS c WHERE NOT EXISTS ( SELECT * FROM Children AS ch WHERE ch.siteid = c.siteid AND ch.custid = c.custid ) ;
Tested in SQL-Fiddle
source share