How to select the top 3 salaries of the department?
<p>I'm trying to solve a SQL problem online that has 2 tables as following, </p>

<p><strong>Employee</strong> table</p>

<p>The Employee table holds all employees. </p>

<pre><code>+----+-------+--------+--------------+
| Id | Name  | Sa1ary | DepartmentId |
+----+-------+--------+--------------+
|  1 | Joe   |  70000 |            1 |
|  2 | Henry |  80000 |            2 |
|  3 | Sam   |  60000 |            2 |
|  4 | Max   |  90000 |            1 |
|  5 | Janet |  69000 |            1 |
|  6 | Randy |  85000 |            1 |
+----+-------+--------+--------------+
</code></pre>

<p><strong>Department</strong> table</p>

<p><a href="https://i.stack.imgur.com/xeQhS.png" rel="noreferrer"><img src="https://i.stack.imgur.com/xeQhS.png" alt="enter image description here"></a></p>

<p>The SQL should return the following data</p>

<p><a href="https://i.stack.imgur.com/JvkTx.png" rel="noreferrer"><img src="https://i.stack.imgur.com/JvkTx.png" alt="enter image description here"></a></p>

<p>I have the SQL queries as following, </p>

<pre><code>    SELECT D.Name AS Department, E.Name AS Employee, E.Salary AS Salary 
FROM Employee E INNER JOIN Department D ON E.DepartmentId = D.Id 
WHERE (SELECT COUNT(DISTINCT(Salary)) FROM Employee 
       WHERE DepartmentId = E.DepartmentId AND Salary &gt; E.Salary) &lt; 3
ORDER by E.DepartmentId, E.Salary DESC;
</code></pre>

<p>The SQL is good, but, I'm little confused about the <code>&lt; 3</code> part. Shouldn't it be = 3 as in the question it was asked about top 3 salaries ? I appreciate an explanation of the SQL and will be helpful. </p>