SQL sorting does not follow group by statement, always uses primary key

前端 未结 4 1050
清酒与你
清酒与你 2021-01-15 06:40

I have a SQL database with a table called staff, having following columns:

workerID (Prim.key), name, department, salary

I am

4条回答
  •  情话喂你
    2021-01-15 06:56

    Try this:

    SELECT s.workerID, s.name, s.department, s.salary
    FROM staff s 
    INNER JOIN (SELECT s.department, MAX(s.salary) AS biggest 
                FROM staff s GROUP BY s.department
              ) AS B ON s.department = B.department AND s.salary = B.biggest;
    

    OR

    SELECT s.workerID, s.name, s.department, s.salary
    FROM (SELECT s.workerID, s.name, s.department, s.salary 
          FROM staff s 
          ORDER BY s.department, s.salary DESC
        ) AS s 
    GROUP BY s.department;
    

提交回复
热议问题