title: SELECT TOP ... FROM UNION
body: <p>What is the best way to SELECT TOP N records from UNION of 2 queries? </p>
  
  <p>I can't do</p>
  
  <pre><code>SELECT TOP N ... FROM
     (SELECT ... FROM Table1
      UNION
      SELECT ... FROM Table2)
  </code></pre>
  
  <p>because both queries return huge results I need every bit of optimization possible and would like to avoid returning everything. For the same reason I cannot insert results into #TEMP table first either.</p>
  
  <p>I can't use <code>SET ROWCOUNT N</code> either because I may need to group results and this command will limit number of grouped rows, and not underlying row selections.</p>
  
  <p>Any other ideas? Thanks!</p>