<p>SQL Server 2005.</p>

<p>I'm adding Foreign Key constraints to the database of an application that allegedly didn't need them.  Naturally, the data has become unreliable and there are orphaned entries in the foreign key field.</p>

<p>Setup:<br/>
Two tables, TableUser and TableOrder.
TableUser has Primary Key 'UserID', and TableOrder has Foreign Key 'UserID'.</p>

<p>How do I find the rows where TableOrder.UserID has no matching entry in TableUser.UserID?</p>

<p>For example, TableOrder.UserID has a value of 250, but there is no matching TableUser.UserID key for 250.</p>

Here's one way:
```
```
select * from TableOrder where UserID not in (select UserID from TableUser);
```
```
There are many different ways to write this sort of query.