15000 字的 MySQL 速查手册
数据分析挖掘与算法
共 11758字,需浏览 24分钟
· 2021-10-26
select 查询结果,如: [学号,平均成绩:组函数avg(成绩)]
from 从哪张表中查找数据,如: [涉及到成绩:成绩表score]
where 查询条件,如: [b.课程号='0003' and b.成绩>80]
group by 分组,如: [每个学生的平均:按学号分组]
(oracle,SQL server中出现在select 子句后的非分组函数,必须出现在group by子句后出现),MySQL中可以不用having 对分组结果指定条件,如: [大于60分]
order by 对查询结果排序,如: [增序: 成绩 ASC / 降序: 成绩 DESC]
;limit 使用limt子句返回topN(对应这个问题返回的成绩前两名),如: [ limit 2 ==>从0索引开始读取2个]
limit==>从0索引开始[0,N-1]
select * from table limit 2,1;
-- 含义是跳过2条取出1条数据,limit后面是从第2条开始读,读取1条信息,即读取第3条数据
select * from table limit 2 offset 1;
-- 含义是从第1条(不包括)数据开始取出2条数据,limit后面跟的是2条数据,offset后面是从第1条开始读取,即读取第2,3条
多表连接: 内连接(省略默认inner) join ...on..左连接left join tableName as b on a.key ==b.key右连接right join 连接union(无重复(过滤去重))和union all(有重复[不过滤去重])
union 并集 union all(有重复) oracle(SQL server)数据库
intersect 交集 minus(except) 相减(差集)
![](https://filescdn.proginn.com/b3f6984718e7bb472364ac708670e9ba/210ecc94688d9abcd989ceea9b5e3a7a.webp)
oracle
一、数据库对象:表(table) 视图(view) 序列(sequence) 索引(index) 同义词(synonym)
1. 视图: 存储起来的 select 语句
create view emp_vw
as
select employee_id, last_name, salary
from employees
where department_id = 90;
select * from emp_vw;
update emp_vw
set last_name = 'HelloKitty'
where employee_id = 100;
select * from employees
where employee_id = 100;
create view emp_vw2
as
select department_id, avg(salary) avg_sal
from employees
group by department_id;
select * from emp_vw2;
update emp_vw2
set avg_sal = 10000
where department_id = 100;
2. 序列:用于生成一组有规律的数值。(通常用于为主键设置值)
create sequence emp_seq1
start with 1
increment by 1
maxvalue 10000
minvalue 1
cycle
nocache;
select emp_seq1.currval from dual;
select emp_seq1.nextval from dual;
当多个表共用同一个序列时。 rollback 发生异常
create table emp1(
id number(10),
name varchar2(30)
);
insert into emp1
values(emp_seq1.nextval, '张三');
select * from emp1;
3. 索引:提高查询效率
create table emp2(
id number(10) primary key,
name varchar2(30)
)
create index emp_idx
on emp2(name);
create index emp_idx2
on emp2(id, name);
4. 同义词
create synonym d1 for departments;
select * from d1;
5. 表:
create table .../ drop table ... / rename ... to..../ truncate table.../alter table ...
insert into ... values ...
update ... set ... where ...
delete from ... where ...
select ...
组函数(MIN()/MAX()/SUM()/AVG()/COUNT())from ...join ... on ...
左外连接:left join ... on ... 右外连接: right join ... on ...where ...
group by ...
(oracle,SQL server中出现在select 子句后的非分组函数,必须出现在 group by子句后)having ...
用于过滤 组函数order by ...
asc 升序, desc 降序limit (0,4)
限制N条数据 如: topN数据
union 并集 union all(有重复) intersect 交集 minus 相减
![](https://filescdn.proginn.com/b3f6984718e7bb472364ac708670e9ba/210ecc94688d9abcd989ceea9b5e3a7a.webp)
![](https://filescdn.proginn.com/debbc06a42b1c02f19c19b9080cb48bb/088f2f4a5158202f7b687ce9cf9633fb.webp)
![](https://filescdn.proginn.com/5e09c2aa707a55223bf9601585ddffc1/dc9d5d899783837d5bfb0705a0c6f719.webp)
![](https://filescdn.proginn.com/f1ee44ead2e278251d823c425f1563c5/b5cab573840572d9783345b700547879.webp)
![](https://filescdn.proginn.com/d3d787d651ad79216345a63aed93c2a3/b9919ed43237ef04a99080a4ba5b846f.webp)
![](https://filescdn.proginn.com/1ab8d33eb7161376a2bb9b7537ec7231/997e8626b66890561756d6e69fdd3ba0.webp)
![](https://filescdn.proginn.com/618e6c138e5d86bdb76cb2afcd04325d/2e30a2c2e2a8e04429d20474d357b1a1.webp)
![](https://filescdn.proginn.com/cfd4f20cc6a9fec469cb7ee1deccbddd/697bbab5bc8d63146d61298c7a77245e.webp)
select employee_id, last_name, salary, department_id
from employees
where department_id in (70, 80) --> 70:1 80:34
union 并集 union all(有重复部分) intersect 交集 minus 相减
select employee_id, last_name, salary, department_id
from employees
where department_id in (80, 90) --> 90:4 80:34
select *
from employees
where salary > (
select salary
from employees
where employee_id = 149
)
select employee_id, manager_id, department_id
from employees
where manager_id in (
select manager_id
from employees
where employee_id in(141, 174)
) and department_id in (
select department_id
from employees
where employee_id in(141, 174)
) and employee_id not in (141, 174);
select employee_id, manager_id, department_id
from employees
where (manager_id, department_id) in (
select manager_id, department_id
from employees
where employee_id in (141, 174)
) and employee_id not in(141, 174);
from 子句中使用子查询
select max(avg(salary))
from employees
group by department_id;
select max(avg_sal)
from (
select avg(salary) avg_sal
from employees
group by department_id
) e
问题:返回比本部门平均工资高的员工的last_name, department_id, salary及平均工资
select last_name, department_id, salary, (select avg(salary) from employees where department_id = e1.department_id)
from employees e1
where salary > (
select avg(salary)
from employees e2
where e1.department_id = e2.department_id
)
select last_name, e1.department_id, salary, avg_sal
from employees e1, (
select department_id, avg(salary) avg_sal
from employees
group by department_id
) e2
where e1.department_id = e2.department_id
and e1.salary > e2.avg_sal;
查询:若部门为10 查看工资的 1.1 倍,部门号为 20 工资的1.2倍,其余 1.3 倍
SELECT
employee_id,
last_name,
salary,
CASE
department_id
WHEN 10 THEN
salary * 1.1
WHEN 20 THEN
salary * 1.2 ELSE salary * 1.3
END "new_salary"
FROM
employees;
SELECT
employee_id,
last_name,
salary,
decode( department_id, 10, salary * 1.1, 20, salary * 1.2, salary * 1.3 ) "new_salary"
FROM
employees;
问题:显式员工的employee_id,last_name和location。其中,若员工department_id与location_id为1800的department_id相同,则location为’Canada’,其余则为’USA’。
select employee_id, last_name, case department_id when (
select department_id
from departments
where location_id = 1800
) then 'Canada' else 'USA' end "location"
from employees;
问题:查询员工的employee_id,last_name,要求按照员工的department_name排序
select employee_id, last_name
from employees e1
order by (
select department_name
from departments d1
where e1.department_id = d1.department_id
)
问题:查询公司管理者的employee_id,last_name,job_id,department_id信息
select employee_id, last_name, job_id, department_id
from employees
where employee_id in (
select manager_id
from employees
)
select employee_id, last_name, job_id, department_id
from employees e1
where exists (
select 'x'
from employees e2
where e1.employee_id = e2.manager_id
)
问题:查询departments表中,不存在于employees表中的部门的department_id和department_name
select department_id, department_name
from departments d1
where not exists (
select 'x'
from employees e1
where e1.department_id = d1.department_id
)
更改 108 员工的信息: 使其工资变为所在部门中的最高工资, job 变为公司中平均工资最低的 job
update employees e1
set salary = (
select max(salary)
from employees e2
where e1.department_id = e2.department_id
), job_id = (
select job_id
from employees
group by job_id
having avg(salary) = (
select min(avg(salary))
from employees
group by job_id
)
)
where employee_id = 108;
删除 108 号员工所在部门中工资最低的那个员工.
delete from employees e1
where salary = (
select min(salary)
from employees
where department_id = (
select department_id
from employees
where employee_id = 108
)
)
select * from employees where employee_id = 108;
select * from employees where department_id = 100
order by salary;
rollback;
常见的SQL面试题:经典50题
学生表:student(学号,学生姓名,出生年月,性别) 成绩表:score(学号,课程号,成绩) 课程表:course(课程号,课程名称,教师号) 教师表:teacher(教师号,教师姓名)
ps:这些题考察SQL的编写能力,对于这类型的题目,需要你先把4张表之间的关联关系搞清楚了,最好的办法是自己在草稿纸上画出关联图,然后再编写对应的SQL语句就比较容易了。下图是我画的这4张表的关系图,可以看出它们之间是通过哪些外键关联起来的:
![](https://filescdn.proginn.com/56d98441403fbebb3dcb024877391d50/0b148f73d9fe27e2b7414fab94f5c5ec.webp)
一、创建数据库和表
![](https://filescdn.proginn.com/e2bf26c33d01e4a4768ef20e0dc5f1ce/83743e6e7d047281bc3f7f64f9e8e409.webp)
1.创建表
1)创建学生表(student)
![](https://filescdn.proginn.com/531b7ec0cfef39d6807ea1baa44150d9/861dc2264ff66b9363a9c2159c77d922.webp)
![](https://filescdn.proginn.com/4570ffa14fb30701ffc1de120658690c/8d0624110f602d552fbc704b22fa1014.webp)
![](https://filescdn.proginn.com/33ef40e86eb588ed497e94a41576c40a/a33a4b627f441fba83c0c237009640e3.webp)
2)创建成绩表(score)
![](https://filescdn.proginn.com/b3197452275e939950b91433f04b287c/ecee43bb4c0c15489af05dae987e02e5.webp)
3)创建课程表(course)
![](https://filescdn.proginn.com/2e0da126291a0448c1edf9228e5dd7eb/f4c9da7828b359cf18e6f43751d5cc98.webp)
4)教师表(teacher)
![](https://filescdn.proginn.com/58a9f729af200354bd8b63ddf301ed45/72da2f944d8b8291f95fd50d4f203825.webp)
![](https://filescdn.proginn.com/ba8c40fc40b243588451b011a5875965/a784c8b6b847ebd728bd29e272a1174e.webp)
insert into student(学号,姓名,出生日期,性别)
values('0001' , '猴子' , '1989-01-01' , '男');
insert into student(学号,姓名,出生日期,性别)
values('0002' , '猴子' , '1990-12-21' , '女');
insert into student(学号,姓名,出生日期,性别)
values('0003' , '马云' , '1991-12-21' , '男');
insert into student(学号,姓名,出生日期,性别)
values('0004' , '王思聪' , '1990-05-20' , '男');
![](https://filescdn.proginn.com/f0a8be7a1923e6e3685f3f743920200e/9adee816e64026f3a468533ff2ec9382.webp)
![](https://filescdn.proginn.com/ce721f54bbb763dfb95150f12f7d88d6/20b355b0c24cc2ee916fe9d389908e4a.webp)
![](https://filescdn.proginn.com/c5afbb6ae03bb40485957bbea35c6f15/407a5769eb9948fe1cefb676c0d013d9.webp)
insert into score(学号,课程号,成绩)
values('0001' , '0001' , 80);
insert into score(学号,课程号,成绩)
values('0001' , '0002' , 90);
insert into score(学号,课程号,成绩)
values('0001' , '0003' , 99);
insert into score(学号,课程号,成绩)
values('0002' , '0002' , 60);
insert into score(学号,课程号,成绩)
values('0002' , '0003' , 80);
insert into score(学号,课程号,成绩)
values('0003' , '0001' , 80);
insert into score(学号,课程号,成绩)
values('0003' , '0002' , 80);
insert into score(学号,课程号,成绩)
values('0003' , '0003' , 80);
![](https://filescdn.proginn.com/2fc8b77a2dde19b1a7f34dd8f8d9168a/aaaa7705ceab7275c859fcbf225a5b5c.webp)
insert into course(课程号,课程名称,教师号)
values('0001' , '语文' , '0002');
insert into course(课程号,课程名称,教师号)
values('0002' , '数学' , '0001');
insert into course(课程号,课程名称,教师号)
values('0003' , '英语' , '0003');
![](https://filescdn.proginn.com/1e99f87b86733cc67b32a33fb0a27899/b8d146878570aea04ce495b7d55082bc.webp)
-- 教师表:添加数据
insert into teacher(教师号,教师姓名)
values('0001' , '孟扎扎');
insert into teacher(教师号,教师姓名)
values('0002' , '马化腾');
-- 这里的教师姓名是空值(null)
insert into teacher(教师号,教师姓名)
values('0003' , null);
-- 这里的教师姓名是空字符串('')
insert into teacher(教师号,教师姓名)
values('0004' , '');
![](https://filescdn.proginn.com/3efb63921093e9053a835f1539062a81/a864977bc64346b04e6a545959842b94.webp)
![](https://filescdn.proginn.com/c74336c8d989229b2b072eb0d0784d21/96113045639d63c128054d842de2c643.webp)
三、50道面试题
![](https://filescdn.proginn.com/81752a370f5695cdd312a1e5bf29eb79/d027b6c9da7c4e9500bd6c1b93817109.webp)
![](https://filescdn.proginn.com/7dfec7784f1954fd8d9b5f321d71d95d/dd87a4bc929daa9307489274bffc6c21.webp)
select count(教师号)
from teacher
where 教师姓名 like '孟%';
2.汇总统计分组分析
![](https://filescdn.proginn.com/c6e9b3bc1e8ba565c4e9e4c3aad153da/89bdc5376d3999f6074a891f1cb344d4.webp)
--分析思路
--select 查询结果 [总成绩:汇总函数sum]
--from 从哪张表中查找数据[成绩表score]
--where 查询条件 [课程号是0002]
select sum(成绩)
from score
where 课程号 = '0002';
--这个题目翻译成大白话就是:查询有多少人选了课程
--select 学号,成绩表里学号有重复值需要去掉
--from 从课程表查找score;
select count(distinct 学号) as 学生人数
from score;
![](https://filescdn.proginn.com/345adea23ab7aaa5eff456fdaae87c89/2b8a74e1b4aba9fd312483c635c0ceae.webp)
/*
分析思路
select 查询结果 [课程ID:是课程号的别名,最高分:max(成绩) ,最低分:min(成绩)]
from 从哪张表中查找数据 [成绩表score]
where 查询条件 [没有]
group by 分组 [各科成绩:也就是每门课程的成绩,需要按课程号分组];
*/
select 课程号,max(成绩) as 最高分,min(成绩) as 最低分
from score
group by 课程号;
/*
分析思路
select 查询结果 [课程号,选修该课程的学生数:汇总函数count]
from 从哪张表中查找数据 [成绩表score]
where 查询条件 [没有]
group by 分组 [每门课程:按课程号分组];
*/
select 课程号, count(学号)
from score
group by 课程号;
/*
分析思路
select 查询结果 [性别,对应性别的人数:汇总函数count]
from 从哪张表中查找数据 [性别在学生表中,所以查找的是学生表student]
where 查询条件 [没有]
group by 分组 [男生、女生人数:按性别分组]
having 对分组结果指定条件 [没有]
order by 对查询结果排序[没有];
*/
select 性别,count(*)
from student
group by 性别;
![](https://filescdn.proginn.com/62ca980c1c94b3b21b3f8f9383a1e931/8e0f7aa1f145b9da7d9a4622cab865c0.webp)
/*
题目翻译成大白话:
平均成绩:展开来说就是计算每个学生的平均成绩
这里涉及到“每个”就是要分组了
平均成绩大于60分,就是对分组结果指定条件
分析思路
select 查询结果 [学号,平均成绩:汇总函数avg(成绩)]
from 从哪张表中查找数据 [成绩在成绩表中,所以查找的是成绩表score]
where 查询条件 [没有]
group by 分组 [平均成绩:先按学号分组,再计算平均成绩]
having 对分组结果指定条件 [平均成绩大于60分]
*/
select 学号, avg(成绩)
from score
group by 学号
having avg(成绩)>60;
/*
翻译成大白话:
第1步,需要先计算出每个学生选修的课程数据,需要按学号分组
第2步,至少选修两门课程:也就是每个学生选修课程数目>=2,对分组结果指定条件
分析思路
select 查询结果 [学号,每个学生选修课程数目:汇总函数count]
from 从哪张表中查找数据 [课程的学生学号:课程表score]
where 查询条件 [至少选修两门课程:需要先计算出每个学生选修了多少门课,需要用分组,所以这里没有where子句]
group by 分组 [每个学生选修课程数目:按课程号分组,然后用汇总函数count计算出选修了多少门课]
having 对分组结果指定条件 [至少选修两门课程:每个学生选修课程数目>=2]
*/
select 学号, count(课程号) as 选修课程数目
from score
group by 学号
having count(课程号)>=2;
/*
翻译成大白话,问题解析:
1)查找出姓名相同的学生有谁,每个姓名相同学生的人数
查询结果:姓名,人数
条件:怎么算姓名相同?按姓名分组后人数大于等于2,因为同名的人数大于等于2
分析思路
select 查询结果 [姓名,人数:汇总函数count(*)]
from 从哪张表中查找数据 [学生表student]
where 查询条件 [没有]
group by 分组 [姓名相同:按姓名分组]
having 对分组结果指定条件 [姓名相同:count(*)>=2]
order by 对查询结果排序[没有];
*/
select 姓名,count(*) as 人数
from student
group by 姓名
having count(*)>=2;
/*
分析思路
select 查询结果 [课程号]
from 从哪张表中查找数据 [成绩表score]
where 查询条件 [不及格:成绩 <60]
group by 分组 [没有]
having 对分组结果指定条件 [没有]
order by 对查询结果排序[课程号从大到小排列:降序desc];
*/
select 课程号
from score
where 成绩<60
order by 课程号 desc;
/*
分析思路
select 查询结果 [课程号,平均成绩:汇总函数avg(成绩)]
from 从哪张表中查找数据 [成绩表score]
where 查询条件 [没有]
group by 分组 [每门课程:按课程号分组]
having 对分组结果指定条件 [没有]
order by 对查询结果排序[按平均成绩升序排序:asc,平均成绩相同时,按课程号降序排列:desc];
*/
select 课程号, avg(成绩) as 平均成绩
from score
group by 课程号
order by 平均成绩 asc,课程号 desc;
/*
分析思路
select 查询结果 []
from 从哪张表中查找数据 [成绩表score]
where 查询条件 [课程编号为“04”且分数小于60]
group by 分组 [没有]
having 对分组结果指定条件 []
order by 对查询结果排序[查询结果按按分数降序排列];
*/
select 学号
from score
where 课程号='04' and 成绩 <60
order by 成绩 desc;
/*
分析思路
select 查询结果 [要求输出课程号和选修人数]
from 从哪张表中查找数据 []
where 查询条件 []
group by 分组 [每门课程:按课程号分组]
having 对分组结果指定条件 [学生选修人数(超过2人的课程才统计):每门课程学生人数>2]
order by 对查询结果排序[查询结果按人数降序排序,若人数相同,按课程号升序排序];
*/
select 课程号, count(学号) as '选修人数'
from score
group by 课程号
having count(学号)>2
order by count(学号) desc,课程号 asc;
/*
分析思路
先分解题目:
1)[两门以上][不及格课程]限制条件
2)[同学的学号及其平均成绩],也就是每个学生的平均成绩,显示学号,平均成绩
分析过程:
第1步:得到每个学生的平均成绩,显示学号,平均成绩
第2步:再加上限制条件:
1)不及格课程
2)两门以上[不及格课程]:课程数目>2
/*
第1步:得到每个学生的平均成绩,显示学号,平均成绩
select 查询结果 [学号,平均成绩:汇总函数avg(成绩)]
from 从哪张表中查找数据 [涉及到成绩:成绩表score]
where 查询条件 [没有]
group by 分组 [每个学生的平均:按学号分组]
having 对分组结果指定条件 [没有]
order by 对查询结果排序[没有];
*/
select 学号, avg(成绩) as 平均成绩
from score
group by 学号;
/*
第2步:再加上限制条件:
1)不及格课程
2)两门以上[不及格课程]
select 查询结果 [学号,平均成绩:汇总函数avg(成绩)]
from 从哪张表中查找数据 [涉及到成绩:成绩表score]
where 查询条件 [限制条件:不及格课程,平均成绩<60]
group by 分组 [每个学生的平均:按学号分组]
having 对分组结果指定条件 [限制条件:课程数目>2,汇总函数count(课程号)>2]
order by 对查询结果排序[没有];
*/
select 学号, avg(成绩) as 平均成绩
from score
where 成绩 <60
group by 学号
having count(课程号)>=2;
3.复杂查询
1.翻译成大白话
select 查询结果[学号] from 从哪张表中查找数据[成绩表:score] where 查询条件[成绩 < 60] group by 分组[没有] having 对分组结果指定条件[没有] order by 对查询结果排序[没有] limit 从查询结果中取出指定行[没有];
select 学号
from score
where 成绩 < 60;
select 查询结果[学号,姓名] from 从哪张表中查找数据[学生表:student] where 查询条件[用到运算符in] group by 分组[没有] having 对分组结果指定条件[没有] order by 对查询结果排序[没有] limit 从查询结果中取出指定行[没有];
select 学号,姓名
from student
where 学号 in (
select 学号
from score
where 成绩 < 60
);
/*
查找出学号,条件:没有学全所有课,也就是该学生选修的课程数 < 总的课程数
【考察知识点】in,子查询
*/
select 学号,姓名
from student
where 学号 in(
select 学号
from score
group by 学号
having count(课程号) < (select count(课程号) from course)
);
select 学号,姓名
from student
where 学号 in(
select 学号
from score
group by 学号
having count(课程号)=2
);
![](https://filescdn.proginn.com/cfd4f20cc6a9fec469cb7ee1deccbddd/697bbab5bc8d63146d61298c7a77245e.webp)
/*
查找1990年出生的学生名单
学生表中出生日期列的类型是datetime
*/
select 学号,姓名
from student
where year(出生日期)=1990;
sql面试题:topN问题
如何找到每个类别下用户最喜欢的产品是哪个? 如果找到每个类别下用户点击最多的5个商品是什么?
![](https://filescdn.proginn.com/99dcfa26b4763531a9c24ccf857d7dc0/7b72ea150fa72616445e03a6abc7bd0d.webp)
分组取每组最大值
select 课程号,max(成绩) as 最大成绩
from score
group by 课程号;
![](https://filescdn.proginn.com/6fd658b07afbebaacffc4ad77c4963c5/96cefce40f537357b4a03bd7bab86a1e.webp)
select *
from score as a
where 成绩 = (
select max(成绩)
from score as b
where b.课程号 = a.课程号);
![](https://filescdn.proginn.com/aab9e401c4c58a5d9360516f09a8c1d3/352942718e1aeba49481467a772e22f4.webp)
分组取每组最小值
select *
from score as a
where 成绩 = (
select min(成绩)
from score as b
where b.课程号 = a.课程号);
![](https://filescdn.proginn.com/a971c3b3bfc3cd812ea0440f522f1490/5993ad3e9a540206e4e4704b0f6ec24c.webp)
每组最大的N条记录
select 课程号,max(成绩) as 最大成绩
from score
group by 课程号;
![](https://filescdn.proginn.com/6fd658b07afbebaacffc4ad77c4963c5/96cefce40f537357b4a03bd7bab86a1e.webp)
-- 课程号'0001' 这一组里成绩前2名
select *
from score
where 课程号 = '0001'
order by 成绩 desc
limit 2;
-- 左右滑动可以可拿到全部sql
(select * from score where 课程号 = '0001' order by 成绩 desc limit 2)
union all
(select * from score where 课程号 = '0002' order by 成绩 desc limit 2)
union all
(select * from score where 课程号 = '0003' order by 成绩 desc limit 2);
![](https://filescdn.proginn.com/f7f2fc8d13fac2a6eaf815cf75c5f736/ead0453896454608a2eff44e5688c1e7.webp)
4.多表查询
![](https://filescdn.proginn.com/015bcabf093f3d39882741b15e74b85d/0720afa768b723d70dd1eccebdd5e6b8.webp)
select a.学号,a.姓名,count(b.课程号) as 选课数,sum(b.成绩) as 总成绩
from student as a left join score as b
on a.学号 = b.学号
group by a.学号;
select a.学号,a.姓名, avg(b.成绩) as 平均成绩
from student as a left join score as b
on a.学号 = b.学号
group by a.学号
having avg(b.成绩)>85;
select a.学号, a.姓名, c.课程号,c.课程名称
from student a inner join score b on a.学号=b.学号
inner join course c on b.课程号=c.课程号;
-- 考察case表达式
select 课程号,
sum(case when 成绩>=60 then 1
else 0
end) as 及格人数,
sum(case when 成绩 < 60 then 1
else 0
end) as 不及格人数
from score
group by 课程号;
-- 考察case表达式
select a.课程号,b.课程名称,
sum(case when 成绩 between 85 and 100
then 1 else 0 end) as '[100-85]',
sum(case when 成绩 >=70 and 成绩<85
then 1 else 0 end) as '[85-70]',
sum(case when 成绩>=60 and 成绩<70
then 1 else 0 end) as '[70-60]',
sum(case when 成绩<60 then 1 else 0 end) as '[<60]'
from score as a right join course as b
on a.课程号=b.课程号
group by a.课程号,b.课程名称;
select a.学号,a.姓名
from student as a inner join score as b on a.学号=b.学号
where b.课程号='0003' and b.成绩>80;
![](https://filescdn.proginn.com/db7bef058ea3d9c150ce85f605b9632c/793841c2bd7afd60e275e35dbc37f283.webp)
![](https://filescdn.proginn.com/e1079a87e4af86372a2cda0321232eac/e79a172f5500b42a5bec3cc97a9b3ff5.webp)
【面试题类型总结】这类题目属于行列如何互换,解题思路如下:
![](https://filescdn.proginn.com/e9efb58e3b8c5167a6cb5dde2fb55e4d/3cbe8cbb6c211b60cd6592e925f0181e.webp)
![](https://filescdn.proginn.com/cfa809921662cde2947a108be1142e07/0acf464cd704f9f649e6ed5c5b1ef42d.webp)
select 学号,'课程号0001','课程号0002','课程号0003'
from score;
![](https://filescdn.proginn.com/911257f66ff449ea7ff060fd43e0d9f7/e0b7d806d305bd2b266881e93e21cdaf.webp)
select 学号,
(case 课程号 when '0001' then 成绩 else 0 end) as '课程号0001',
(case 课程号 when '0002' then 成绩 else 0 end) as '课程号0002',
(case 课程号 when '0003' then 成绩 else 0 end) as '课程号0003'
from score;
![](https://filescdn.proginn.com/f4fb6d4391c6241b1689b689db53f577/f5891fba442706409a9d35f7c33c1c6f.webp)
![](https://filescdn.proginn.com/b923debf50714ad20956a2751ac22e2f/8f60f06489691f305a6900978c15061d.webp)
select 学号,
max(case 课程号 when '0001' then 成绩 else 0 end) as '课程号0001',
max(case 课程号 when '0002' then 成绩 else 0 end) as '课程号0002',
max(case 课程号 when '0003' then 成绩 else 0 end) as '课程号0003'
from score
group by 学号;
![](https://filescdn.proginn.com/c902618753da075b241b5c7c73580530/cd0ad6b554093a484834b4fb4948d164.webp)
作者:sh_c_2450957609
blog.csdn.net/u010565545/article/details/100785261
评论