我正在学习SQL,并在MySQL中创建了以下数据集:
Create table Departments (DepartmentID int primary key, Name text);
insert into Departments values
(1001,'SRO'),
(2001,'Drs'),
(3001,'Accounting');
Create table Employees
(
EmployeeID int not null,
DepartmentID int not null,
ManagerID int,
Name varchar(50) not null,
Salary int not null,
primary key(EmployeeID),
foreign key (DepartmentID)
references Departments(DepartmentID)
);
insert into Employees values
(68319,1001,NULL,'Kayling','6000.00'),
(66928,3001,68319,'Blaze','2750.00'),
(67832,1001,68319,'Clare','2550.00'),
(65646,2001,68319,'Jonas','2957.00'),
(67858,2001,65646,'Scarlet','3100.00'),
(69062,2001,65646,'Frank','3100.00'),
(63679,2001,69062,'Sandrine','900.00'),
(64989,3001,66928,'Adelyn','1700.00'),
(65271,3001,66928,'Wade','1350.00');
我想找出每个部门的最高工资,研究了我看到的许多文章,建议以这种形式运行MAX
:
SELECT max(Salary),DepartmentID
FROM Employees
GROUP BY DepartmentID;
然而,根据答案here中描述的操作顺序,
FROM, including JOINs
WHERE
SELECT the row obtained by from and where in a temporary area for others
operation (and build the column alias)
DISTINCT
GROUP BY
HAVING
ORDER BY
LIMIT and OFFSET
return the final result
在GROUP BY
之前调用MAX
函数,这意味着在执行GROUP BY
之前,MAX
将在整个表中运行,根据我的理解,这应该导致出现单个最大值across the table,该值应该是6000.然而,情况并非如此,我确实看到的结果令人困惑,我想要的 struct 是正确的:
max(Salary) DepartmentID
6000 1001
3100 2001
2750 3001
不知何故,MAX
函数在GROUP BY
之后工作,但也在由于GROUP BY
删除多个组行而丢失所有行之前工作.
如果我继续这样理解,就会给我带来更多的问题.
如果对SELECT
执行GROUP BY
操作before,则GROUP BY
将删除每个DepartmentID
的所有行,只保留一行randomly chosen,这意味着当SELECT
中的MAX
函数有机会运行时,它将只看到一个工资值,该值可以是任何值,而不是每个部门的最大值.
我错过了什么让我的理解变得复杂的地方?