首页 > 试题广场 >

找出所有员工当前薪水salary情况

[编程题]找出所有员工当前薪水salary情况
  • 热度指数:382645 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
有一个薪水表,salaries简况如下:
emp_no 
salary
from_date 
to_date
10001
72527 2002-06-22
9999-01-01
10002
72527 2001-08-02
9999-01-01
10003
43311 2001-12-01 9999-01-01

请你找出所有员工具体的薪水salary情况,对于相同的薪水只显示一次,并按照逆序显示,以上例子输出如下:
salary
72527
43311

示例1

输入

drop table if exists  `salaries` ; 
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`));
INSERT INTO salaries VALUES(10001,72527,'2002-06-22','9999-01-01');
INSERT INTO salaries VALUES(10002,72527,'2001-08-02','9999-01-01');
INSERT INTO salaries VALUES(10003,43311,'2001-12-01','9999-01-01');

输出

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

发表于 2024-07-18 15:25:59 回复(0)
select salary
from salaries
group by salary
order by salary desc

发表于 2024-04-28 10:11:38 回复(0)
SELECT
    salary
FROM
    (
        SELECT
            salary,
            ROW_NUMBER() OVER (
                PARTITION BY
                    salary
                ORDER BY
                    salary
            ) AS rn
        FROM
            salaries
    ) AS subquery
WHERE
    rn = 1
ORDER BY
    salary DESC;


超大数据量时可以这么写,效率也非常高
发表于 2024-04-02 11:41:47 回复(0)
select distinct salary from salaries order by salary desc;

编辑于 2024-02-05 12:13:28 回复(0)
select distinct salary
from salaries
where to_date = '9999-01-01'
order by salary DESC

发表于 2024-01-04 22:44:45 回复(0)
select 
salary
from salaries
group by salary
order by 1 desc

发表于 2023-08-14 15:55:30 回复(0)
# select 
#     distinct salary
# from 
#     salaries
# order by
#     salary
# desc;
# 大表一般用distinct效率不高,大数据量的时候都禁止用distinct,建议用group by解决重复问题。
select salary
from salaries
group by salary
order by salary desc;

发表于 2023-06-14 13:09:55 回复(0)
select distinct salary
from salaries
order by salary desc

发表于 2023-05-29 21:14:41 回复(0)
select 
    distinct salary
from 
    salaries  
order by 
    salary desc 

发表于 2022-12-06 22:26:19 回复(0)
为什么我在idea里的mysql console里 写 group by salary desc 可以逆序查出来,在这个答题页面里会报错
发表于 2022-09-23 16:43:38 回复(0)
--薪资分组排序,取一条
select a.salary from
(select salary,row_number()over(partition by salary) rk
from salaries)a
where a.rk =1
order by a.salary desc;
--直接薪资去重
select distinct salary from salaries;
--直接薪资分组
select salary from salaries group by salary;
发表于 2022-09-19 15:13:22 回复(0)
select distinct  salary  from salaries order by salary desc;
发表于 2022-09-11 19:58:15 回复(0)
select distinct salary from salaries
order by salary desc
发表于 2022-09-01 00:26:20 回复(0)
select distinct s.salary from salaries s order by salary desc;
 distinct 去除重复 desc降序排列
发表于 2022-08-24 18:37:18 回复(0)
select distinct salary from salaries
order by salary desc;
发表于 2022-08-06 21:04:45 回复(0)
select
  distinct salary
from
  salaries
order by
  salary desc
发表于 2022-08-04 13:36:09 回复(0)
with lsb as
(select salary,sum(salary) from salaries
group by salary
order by salary desc)
select salary from lsb

group by 解决方法
发表于 2022-07-07 10:10:20 回复(0)
一、思路
找出员工具体薪水的情况,并逆序;
1、去重,因为薪水可能出现重复值
2、因为是当前工资,所以筛选日期
3、对薪水进行逆序

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

发表于 2022-07-07 09:40:22 回复(0)
大表建议用group by,distinct效率太低
发表于 2022-06-02 13:59:31 回复(0)