title: Alternative to using subqueries in SQL statements?
body: <p>I have two tables:</p>
  
  <pre><code>TableA: (a temporary table)
  ItemId (int)
  
  TableB:
  ItemId (int), ParentID (int)
  </code></pre>
  
  <p>I want to retrieve all items in Table A where the ParentID of any of the items in Table A doesn't exist as an ItemID. (i.e. I want to get the root of the items in TableA) </p>
  
  <p>This query does what I want:</p>
  
  <pre><code>SELECT a.ItemID
  FROM TableA a
  INNER JOIN TableB b ON a.ItemId = b.ItemID
  WHERE b.ParentID NOT IN ( SELECT * from TableA ) 
  </code></pre>
  
  <p>as does this one:</p>
  
  <pre><code>SELECT b.ItemID 
  FROM TableB b
  WHERE b.ItemID IN ( SELECT * FROM TableA)
  AND b.ParentID NOT IN ( SELECT * FROM TableA )
  </code></pre>
  
  <p>I am not satisfied with either of the queries, particularly because of the use of NOT IN/IN. Is there a way to do this without them? Perhaps a cleaner way that doesn't require subqueries?</p>
  
  <p>Sample Data:</p>
  
  <pre><code>Table A
  -------
  2
  3
  5
  6
  
  Table B
  --------
  1 | NULL
  2 | 1
  3 | 1
  4 | 3
  5 | 3
  6 | 3
  </code></pre>
  
  <p>Desired Result:</p>
  
  <pre><code>2
  3
  </code></pre>
  
  <p>Thanks</p>