牛客网数据库SQL实战08—— 找出所有员工当前薪水salary情况

牛客网数据库SQL实战08—— 找出所有员工当前薪水salary情况

题目描述

找出所有员工当前(to_date=‘9999-01-01’)具体的薪水salary情况,对于相同的薪水只显示一次,并按照逆序显示

CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));

输入描述:

输出描述:

salary
94692
94409
88958
88070
省略
25828

我的解答

select distinct salary
from salaries
where to_date='9999-01-01'
order by salary desc

此题应注意以下两点:

  1. 相同薪水显示一次,则使用SELECT DISTINCT可去除重复值
  2. 要求逆序排列,则在最后应使用ORDER BY salary DESC

我觉得最好的答案

但是大表一般用distinct效率不高,大数据量的时候都禁止用distinct,建议用group by解决重复问题。

select salary from salaries 
where to_date='9999-01-01' 
group by salary order by salary desc 

对于distinct与group by的使用:

  1. 当对系统的性能高并数据量大时使用group by
  2. 当对系统的性能不高时使用数据量少时两者皆可
  3. 尽量使用group by
发布了175 篇原创文章 · 获赞 58 · 访问量 37万+

猜你喜欢

转载自blog.csdn.net/sunbocong/article/details/105482892