How to understand this sql self-connection?

topic description

< table > < thead > < tr > < th > Employee < / th > < / tr > < / thead > < tbody > < tr > < td > Id < / td > < td > Name < / td > < td > Salary < / td > < td > DepartmentId < / td > < / tr > < tr > < td > 1 < / td > < td > Joe < / td > < td > 70000 < / td > < td > 1 < / td > < / tr > < tr > < td > 2 < / td > < td > Henry < / td > < td > 80000 < / td > < td > 2 < / td > < / tr > < tr > < td > 3 < / td > < td > Sam < / td > < td > 60000 < / td > < td > 2 < / td > < / tr > < tr > < td > 4 < / td > < td > Max < / td > < td > 90000 < / td > < td > 1 < / td > < / tr > < tr > < td > 5 < / td > < td > Janet < / td > < td > 69000 < / td > < td > 1 < / td > < / tr > < tr > < td > 6 < / td > < td > Randy < / td > < td > 85000 < / td > < td > 1 < / td > < / tr > < / tbody > < / table > The

Employee table contains all employee information, with each employee having its own Id, salary and department Id.

< table > < thead > < tr > < th > Department < / th > < / tr > < / thead > < tbody > < tr > < td > Id < / td > < td > Name < / td > < / tr > < tr > < td > 1 < / td > < td > IT < / td > < / tr > < tr > < td > 2 < / td > < td > Sales < / td > < / tr > < / tbody > < / table > The

Department table contains information for all departments of the company.

write a SQL query to find out the top three highest-paid employees in each department.

related codes

/ / Please paste the code text below (do not replace the code with pictures)

answer:

SELECT d.Name AS Department, e.Name AS Employee, e.Salary
FROM Department d, Employee e
WHERE b.DepartmentId = d.Id
    AND (
        SELECT COUNT(DISTINCT Salary)
        FROM Employee
        WHERE DepartmentId = d.Id
            AND Salary > e.Salary
    ) < 3
ORDER BY Department

what result do you expect? What is the error message actually seen?

how to understand the subqueries:
(SELECT COUNT (DISTINCT Salary)
FROM Employee
WHERE DepartmentId = d.Id
AND Salary > e.Salary) < 3

try it yourself

SELECT a.Salary
    FROM Employee a ,Employee b WHERE a.Salary > b.Salary

found that I still can"t understand the query logic.

Sql
May.28,2021

the purpose of this subquery is:




 ()

then where holds

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e432f3-436cf.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e432f3-436cf.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?