SQL-8 找出所有员工当前(to_date='9999-01-01')具体的薪水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`));
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 |
| 74057 |
| 72527 |
思路: 在查找中 使用不重复的应该用distinct (而不是unique 约束)
有两种方式 distinct 和group
SQL:
select distinct salary from salaries
where to_date='9999-01-01'
order by salary desc
select salary from salaries
where to_date='9999-01-01'
group by salary
order by salary desc
看别人的分析 两种性能比较分析:链接:https://www.jianshu.com/p/34800d06f63d
1、数据量较大时,重复数据占比比较高时 group by 效率略好于distinct
2、重复数据占比较少时,用distinct的性能会优于group by

浙公网安备 33010602011771号