title: SQL, nested queries, subqueries, correlated queries
body: <pre><code>SELECT T.[NAME] FROM  ( 
       SELECT Unregistered.[NAME] 
       FROM Unregistered 
          LEFT JOIN [Event] ON Event.ID = Unregistered.EventID 
       WHERE EventID IN ( 
                         SELECT EventID 
                         FROM Registered WHERE [NAME] Like '%blabla%' )
       UNION ALL
       SELECT Registered.[NAME] 
       FROM Registered
          LEFT JOIN [Event] ON Event.ID = Registered.EventID
       WHERE EventID IN ( 
                         SELECT EventID 
                         FROM Registered WHERE [NAME] Like '%blabla%' )
  ) AS T
  </code></pre>
  
  <p>So, I have a table, lets call it Events that is linked to tables Unregistered and Registered. I want to select all Unregistered AND Registered rows in ANY EVENT that is found by the content in this Registered table.</p>
  
  <p>Both Unregistered and Registered -tables have several (or none) entries with the same Event ID. This is just demo content and names, yet the problem is real.</p>
  
  <p>You see the problem, here I need to query twice for the ID range, how can I make this in one query and use the result in both union statements? As in whats the best and most efficient way to perform a query for the output?</p>
  
  <p>It's MS SQL Server.</p>
  
  <p>Thanks!</p>