Please let me know how to get this output, I have searched a lot but all of the queries shows two separate tables one for employees and another department. Here I do not have it so I tried this:
SELECT e.department_id, e.Salary
FROM Employees1 e INNER JOIN employees1 s
ON e.Department_Id = s.Department_Id
WHERE (SELECT COUNT(DISTINCT(Salary)) FROM Employees1
WHERE e.Department_Id = s.Department_Id AND s.Salary > E.Salary) < 3
ORDER by E.Department_Id, E.Salary ;
But the output is not right.
2
Answers
If you are using MySql 8.0 or newer, then we can use the window function rank() since we need to find the unique salary value.
Query
You gonna need window function for sure: