这是架构:
CREATE TABLE `employees` (
`employee_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`department_id` int(11) DEFAULT NULL,
`boss_id` int(11) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`salary` varchar(255) DEFAULT NULL,
PRIMARY KEY (`employee_id`)
);
CREATE TABLE `departments` (
`department_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`department_id`)
);
这是数据集:
INSERT INTO `employees` (`employee_id`, `department_id`, `boss_id`, `name`, `salary`)
VALUES
(1,1,0,'manager','80000'),
(2,1,1,'emp1','60000'),
(3,1,1,'emp2','50000'),
(4,1,1,'emp3','95000'),
(5,1,1,'emp4','75000');
INSERT INTO `departments` (`department_id`, `name`)
VALUES
(1,'IT'),
(2,'HR'),
(3,'Sales'),
(4,'Marketing');
练习题:列出所在部门薪水最高的员工。
这是我的查询:
select e.name as 'employee name',max(e.salary) as 'salary',d.name as 'dept name'
from employees e join departments d
on e.department_id=d.department_id
group by d.name
为什么我的查询只返回一行?它不应该返回 4(每个部门名称一个)吗?
提前致谢!