title: Repeat query for every value in a column and union all results
body: <p>I wrote a query which gives me top 3 salaries for the <strong>specific department id</strong>. For example for <code>DepartmentId=100</code>:</p>
  
  <pre><code>select distinct top 3 e.Id, e.Salary, dep.Id
  from Employee e
  inner join Department dep on e.DepartmentId = dep.Id and dep.Id = 100
  </code></pre>
  
  <p>and it's working fine. </p>
  
  <p>Now, I want to run previous query for <strong>every department id</strong> and union all results. Something like this (written in a pseudocode):</p>
  
  <pre><code>Result &lt;- empty
  foreach depId in [Department].id
      Result = Result UNION run previous query with depId (insead of 100)
  </code></pre>
  
  <p>How can I achive this with SQL?</p>