• *18、查找当前薪水排名第二多的员工编号emp_no、薪水salary、last_name以及first_name,不准使用order by


    1、题目描述:

    查找当前薪水(to_date='9999-01-01')排名第二多的员工编号emp_no、薪水salary、last_name以及first_name,不准使用order by
    CREATE TABLE `employees` (
    `emp_no` int(11) NOT NULL,
    `birth_date` date NOT NULL,
    `first_name` varchar(14) NOT NULL,
    `last_name` varchar(16) NOT NULL,
    `gender` char(1) NOT NULL,
    `hire_date` date NOT NULL,
    PRIMARY KEY (`emp_no`));
    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`));

    输入描述:

    输出描述:

    emp_nosalarylast_namefirst_name
    10009 94409 Peac Sumant

    2、代码:

    (1)解法1:除了最高工资就是它最高,先找到最高工资,然后找到第二高工资

    select e.emp_no,max(s.salary),e.last_name,e.first_name from employees e, salaries s
    where s.salary < (select max(salary) from salaries)
    and s.to_date="9999-01-01" and e.emp_no=s.emp_no;

    (2)解法2:表内连接求第二高工资

    select e.emp_no,s.salary,e.last_name,e.first_name
    from employees e join salaries s on e.emp_no=s.emp_no 
    where  s.to_date='9999-01-01' and s.salary = 
    (
         select s1.salary
         from salaries s1 join salaries s2 on s1.salary<=s2.salary 
         where s1.to_date='9999-01-01' and s2.to_date='9999-01-01'
         group by s1.salary having count(distinct s2.salary)=2
     );
  • 相关阅读:
    golang 多个worker正常关闭的示例
    golang调试工具Delve
    Golang & GitLab-CI 详细实例步骤
    [git] 能在关键时刻救命的git指令
    高效实时数据排行榜实现
    [Golang] 编译程序时打上git提交信息标记
    [Golang] 开源一个帧同步服务器
    Golang etcd服务注册与发现
    Golang pprof详解
    shell 递归枚举文件并操作
  • 原文地址:https://www.cnblogs.com/guoyu1/p/12242344.html
Copyright © 2020-2023  润新知