select2

总结

课堂代码

drop database if exists hrs;
create database hrs default charset utf8mb4;

use hrs;

create table tb_dept
(
dno int not null comment '编号',
dname varchar(10) not null comment '名称',
dloc varchar(20) not null comment '所在地',
primary key (dno)
);

insert into tb_dept values 
    (10, '会计部', '北京'),
    (20, '研发部', '成都'),
    (30, '销售部', '重庆'),
    (40, '运维部', '深圳');

create table tb_emp
(
eno int not null comment '员工编号',
ename varchar(20) not null comment '员工姓名',
job varchar(20) not null comment '员工职位',
mgr int comment '主管编号',
sal int not null comment '员工月薪',
comm int comment '每月补贴',
dno int comment '所在部门编号',
primary key (eno),
foreign key (dno) references tb_dept (dno)
);

-- alter table tb_emp add constraint pk_emp_eno primary key (eno);
-- alter table tb_emp add constraint uk_emp_ename unique (ename);
-- alter table tb_emp add constraint fk_emp_mgr foreign key (mgr) references tb_emp (eno);
-- alter table tb_emp add constraint fk_emp_dno foreign key (dno) references tb_dept (dno);

insert into tb_emp values 
    (7800, '张三丰', '总裁', null, 9000, 1200, 20),
    (2056, '乔峰', '分析师', 7800, 5000, 1500, 20),
    (3088, '李莫愁', '设计师', 2056, 3500, 800, 20),
    (3211, '张无忌', '程序员', 2056, 3200, null, 20),
    (3233, '丘处机', '程序员', 2056, 3400, null, 20),
    (3251, '张翠山', '程序员', 2056, 4000, null, 20),
    (5566, '宋远桥', '会计师', 7800, 4000, 1000, 10),
    (5234, '郭靖', '出纳', 5566, 2000, null, 10),
    (3344, '黄蓉', '销售主管', 7800, 3000, 800, 30),
    (1359, '胡一刀', '销售员', 3344, 1800, 200, 30),
    (4466, '苗人凤', '销售员', 3344, 2500, null, 30),
    (3244, '欧阳锋', '程序员', 3088, 3200, null, 20),
    (3577, '杨过', '会计', 5566, 2200, null, 10),
    (3588, '朱九真', '会计', 5566, 2500, null, 10);


-- 查询月薪最高的员工姓名和月薪
select ename, sal from tb_emp where sal=
(select max(sal) from tb_emp);

select ename, sal from tb_emp where sal>=all
(select sal from tb_emp);

-- 查询员工的姓名和年薪((月薪+补贴)*13)
select ename as 姓名, (sal+ifnull(comm,0))*13 as 年薪 from tb_emp; 


-- 查询有员工的部门的编号和人数
select dno, count(*) from tb_emp
group by dno;

-- 查询所有部门的名称和人数
select dname, count(b.dno) from tb_dept a
left join tb_emp b on a.dno=b.dno
group by b.dno;

select dname, ifnull(total, 0) as total from tb_dept a
left join (select dno, count(*) as total from tb_emp group by dno) b
on a.dno=b.dno;

-- 查询月薪最高的员工(Boss除外)的姓名和月薪
select ename, sal from tb_emp order by sal desc limit 1, 1;

-- 查询月薪排第2名的员工的姓名和月薪
select ename, sal from tb_emp where sal=(
    select distinct sal from tb_emp order by sal desc limit 1,1
    );

select ename, sal from tb_emp where sal=(
select max(sal) as max_sal from tb_emp
where sal < (select max(sal) from tb_emp)
);

-- 查询月薪排第N名的员工的姓名和月薪
select ename, sal from(
select rowno, ename, sal 
from (
select (@rowno:=@rowno+1) as rowno, ename, sal 
from tb_emp, 
(select(@rowno:=0)) a
order by sal desc, comm desc
) t2 ) t1 where rowno=2;

-- 查询月薪超过平均月薪的员工的姓名和月薪
select ename sal from tb_emp where sal > (
    select avg(sal) from tb_emp
);

-- 查询月薪超过其所在部门平均月薪的员工的姓名、部门编号和月薪
select ename, a.dno, sal from tb_emp a
join(
    select dno, avg(sal) as avg_sal from tb_emp group by dno
) b on a.dno=b.dno 
where sal > avg_sal;

-- 查询部门中月薪最高的人姓名、月薪和所在部门名称
select ename, sal, dname from tb_emp a 
join(select dno, max(sal) as max_sal from tb_emp group by dno) b
on a.dno=b.dno
join tb_dept c on a.dno=c.dno
where sal=max_sal;

-- 查询主管的姓名和职位
-- 尽量少用in/not in 运算, 尽量少用distinct操作
-- 可以使用存在性判断(exists/not exists), 替代集合运算和去重操作
select ename, job from tb_emp,
(select distinct mgr from tb_emp where mgr is not null) b 
where eno=b.mgr;

select ename, job from tb_emp t1 where exists (
    select 'x' from tb_emp t2 where t1.eno=t2.mgr
) ;

-- 窗口函数: rownum() / rank() / dense_rank()
-- 窗口函数不适合业务数据库, 只适合做离线数据
-- 查询月薪排名4~6名的员工排名、姓名和月薪
select rowno, ename, sal 
from (select (@a:=@a+1) as rowno, ename, sal 
from tb_emp, 
(select @a:=0) t1
order by sal desc, comm desc
) t2
where rowno between 4 and 6;

select ename, sal, 
row_number() over (order by sal desc) as row_num, 
rank() over (order by sal desc) as ranking, 
dense_rank() over (order by sal desc) as dr
from tb_emp;

select ename, sal, ranking from (
select ename, sal, rank() over(order by sal desc) as ranking 
from tb_emp
) t1 where ranking between 4 and 6;
-- 窗口函数主要用于TopN查询问题
-- 查询每个部门月薪前2名的员工姓名和月薪
select ename, sal, dno from (
select ename, sal, dno, rank() over (partition by dno order by sal desc) as ranking
from tb_emp) t1 where ranking <=2;

select ename, sal, dno from tb_emp t1
where (select count(*) from tb_emp t2 where t1.dno=t2.dno and 
t2.sal > t1.sal) <2
order by dno asc, sal desc;

作业

1. 写一个函数,实现对列表进行二分查找

"""
Time:2021/6/9 17:39
Author:Second
"""


def bin_search(list1, value: int):
    min_index = 0
    max_index = len(list1)-1
    while min_index <= max_index:
        mid_index = (min_index + max_index) // 2
        if list1[mid_index] == value:
            return mid_index
        elif list1[mid_index] > value:
            max_index = mid_index - 1
        else:
            min_index = mid_index + 1
    return

result = bin_search(range(10), 3)
print(result)

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容