SQL查询:对于每个部门,查找该部门中教师的最高薪金。
讲师模式:讲师(ID,姓名,部门名称,薪金)标识主键,部门名称外键引用部门。
部门(部门名称,建筑物,预算)
讲师表值:
ID Iname dept_name salary
10101 Srinivasan Comp. Sci. 65000
12121 Wu Finance 90000
15151 Mozart Music 40000
22222 Einstein Physics 95000
32343 El Said History 60000
33456 Gold Physics 87000
45565 Katz Comp. Sci. 75000
58583 Califieri History 62000
76543 Singh Finance 80000
76766 Crick Biology 72000
83821 Brandt Comp. Sci. 92000
98345 Kim Elec. Eng. 80000
字符串
如果我们尝试手动查找最高薪金,
Brandt Comp. Sci
Wu Finance
Mozart Music
Einstein Physics
Califieri History
Crick Biology
Kim Elec. Eng.
型
现在我运行这个查询,
select distinct
T.Iname,
T.dept_name
from instructor as T,
instructor as S
where T.salary > S.salary
group by T.dept_name;
型
我得到
Kim Elec. Eng.
Brandt Comp. Sci.
Crick Biology
Singh Finance
Gold Physics
Califieri History
型
我得到了不正确的结果,金融和物理和音乐甚至不包括在内。
我找不到我的错误。我想知道我需要在哪里修改?
谢谢
5条答案
按热度按时间inb24sb21#
我想下面的SQL会起作用。
字符串
或
型
ltqd579y2#
在MySQL 8+上,这个问题很容易使用
RANK
处理:字符串
请注意,如果出现平局,上述报告将报告给定部门内共享最高工资的两个或更多讲师。
e0bqpujr3#
使用EXISTS
字符串
iibxawm44#
另一种方法是在子查询td_1中查找部门的最大工资,然后使用salary和department将其与数据表连接
字符串
pu82cl6c5#
试试这个:
字符串