From my database I want to get the lastname of employee, the number of employees who earn higher than him, and then rank the employee's salary in descending order; with the highest earning employee as zero. Here is how I have done it:
SELECT 0+COUNT(b.salary) rank
, a.lname
, a.salary
, COUNT(*) AS employee_count
FROM employee a LEFT OUTER JOIN employee b
ON a.salary<b.salary
GROUP BY a.salary, a.lname
ORDER BY salary DESC
And the result is shown like this:
So far so good. Except, Borg shouldn't be even there. Because no one is earning more than him. Now this happened because of using LEFT OUTER JOIN. I tried using INNER JOIN but it gives syntax error. So the question is how can I make INNER JOIN work with this?
If you just want to eliminate the top value:
SELECT *
FROM (
SELECT 0+COUNT(b.salary) rank
, a.lname
, a.salary
, COUNT(*) AS employee_count
FROM employee a
LEFT OUTER JOIN employee b
ON a.salary < b.salary
GROUP BY a.salary, a.lname
ORDER BY salary DESC
) T1
WHERE T1.rank > 0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.