数据表介绍
--1.学生表
Student(SId,Sname,Sage,Ssex)
--SId 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
--2.课程表
Course(CId,Cname,TId)
--CId 课程编号,Cname 课程名称,TId 教师编号
--3.教师表
Teacher(TId,Tname)
--TId 教师编号,Tname 教师姓名
--4.成绩表
SC(SId,CId,score)
--SId 学生编号,CId 课程编号,score 分数
创表
--学生表 Student
create table Student(SId varchar(10),Sname varchar(10),Sage datetime,Ssex varchar(10));
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-12-20' , '男');
insert into Student values('04' , '李云' , '1990-12-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-01-01' , '女');
insert into Student values('07' , '郑竹' , '1989-01-01' , '女');
insert into Student values('09' , '张三' , '2017-12-20' , '女');
insert into Student values('10' , '李四' , '2017-12-25' , '女');
insert into Student values('11' , '李四' , '2012-06-06' , '女');
insert into Student values('12' , '赵六' , '2013-06-13' , '女');
insert into Student values('13' , '孙七' , '2014-06-01' , '女');
-- 科目表 Course
create table Course(CId varchar(10),Cname nvarchar(10),TId varchar(10));
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
-- 教师表 Teacher
create table Teacher(TId varchar(10),Tname varchar(10));
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
-- 成绩表 SC
create table SC(SId varchar(10),CId varchar(10),score decimal(18,1));
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98);
1.查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数(****)
select sid,cid,score from sc where cid=1;
+------+------+-------+
| sid | cid | score |
+------+------+-------+
| 01 | 01 | 80.0 |
| 02 | 01 | 70.0 |
| 03 | 01 | 80.0 |
| 04 | 01 | 50.0 |
| 05 | 01 | 76.0 |
| 06 | 01 | 31.0 |
+------+------+-------+
select sid,cid,score from sc where cid=2;
+------+------+-------+
| sid | cid | score |
+------+------+-------+
| 01 | 02 | 90.0 |
| 02 | 02 | 60.0 |
| 03 | 02 | 80.0 |
| 04 | 02 | 30.0 |
| 05 | 02 | 87.0 |
| 07 | 02 | 89.0 |
+------+------+-------+
select s1.sid,s2.cid,s2.score
from (select sid,cid,score from sc where cid=1) as s1,
(select sid,cid,score from sc where cid=2) as s2
where s1.sid=s2.sid and s1.score>s2.score;
+------+------+-------+
| sid | cid | score |
+------+------+-------+
| 02 | 02 | 60.0 |
| 04 | 02 | 30.0 |
+------+------+-------+
select stu.sid,stu.sname,stu.sage,stu.ssex,ss.cid,ss.score from student as stu
right join (select s1.sid,s2.cid,s2.score
from (select sid,cid,score from sc where cid=1) as s1,
(select sid,cid,score from sc where cid=2) as s2
where s1.sid=s2.sid and s1.score>s2.score
) as ss
on stu.sid=ss.sid;
+------+--------+---------------------+------+------+-------+
| sid | sname | sage | ssex | cid | score |
+------+--------+---------------------+------+------+-------+
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 | 02 | 60.0 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 | 02 | 30.0 |
+------+--------+---------------------+------+------+-------+
select * from sc as s1
join sc as s2 on s1.sid=s2.sid
where s1.cid=1 and s2.cid=2 and s1.score>s2.score; ## 自联结获取满足条件的sid
2.查询同时存在" 01 "课程和" 02 "课程的情况(*)
select * from sc where cid=1;
select * from sc where cid=2;
select * from (select * from sc where cid=1) as s1 join (select * from sc where cid=2)as s2
on s1.sid=s2.sid;
+------+------+-------+------+------+-------+
| SId | CId | score | SId | CId | score |
+------+------+-------+------+------+-------+
| 01 | 01 | 80.0 | 01 | 02 | 90.0 |
| 02 | 01 | 70.0 | 02 | 02 | 60.0 |
| 03 | 01 | 80.0 | 03 | 02 | 80.0 |
| 04 | 01 | 50.0 | 04 | 02 | 30.0 |
| 05 | 01 | 76.0 | 05 | 02 | 87.0 |
+------+------+-------+------+------+-------+
select s1.* from sc as s1 ## 自联结方法
join sc as s2 on s1.sid=s2.sid
where s1.cid=1 and s2.cid=2;
3.查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )(*)
select * from sc where cid=1;
select * from sc where cid=2;
select s1.sid,s2.cid,s2.score from (select * from sc where cid=1)as s1
left join (select * from sc where cid=2) as s2
on s1.sid=s2.sid;
+------+------+-------+
| SId | CId | score |
+------+------+-------+
| 01 | 02 | 90.0 |
| 02 | 02 | 60.0 |
| 03 | 02 | 80.0 |
| 04 | 02 | 30.0 |
| 05 | 02 | 87.0 |
| 06 | NULL | NULL |
+------+------+-------+
4.查询不存在" 01 "课程但存在" 02 "课程的情况(**)
select sid from sc where cid=1; # 此处不能select *,因为后面not in判断的时候数据较多会报错
-- select * from sc where cid=2;
-- 错误
-- select s2.sid,s1.cid,s1.score
-- from (select * from sc where cid=1)as s1
-- right join (select * from sc where cid=2)as s2
-- on s1.sid=s2.sid;
select * from sc
where sid not in (select sid from sc where cid=1) and cid=2;
+------+------+-------+
| SId | CId | score |
+------+------+-------+
| 07 | 02 | 89.0 |
+------+------+-------+
5.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩(*)
select sid,avg(score) as av from sc group by sid having av>60;
select stu.sid,stu.sname,s1.av from student as stu # round(s1.av,2)可以取小数点后两位
join (select sid,avg(score) as av from sc group by sid having av>60)as s1
on stu.sid=s1.sid ;
+------+--------+----------+
| sid | sname | av |
+------+--------+----------+
| 01 | 赵雷 | 89.66667 |
| 02 | 钱电 | 70.00000 |
| 03 | 孙风 | 80.00000 |
| 05 | 周梅 | 81.50000 |
| 07 | 郑竹 | 93.50000 |
+------+--------+----------+
6.查询在 SC 表存在成绩的学生信息(*)
select student.* from student join (select distinct sid from sc)as s1
on student.sid=s1.sid;
+------+--------+---------------------+------+
| SId | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 03 | 孙风 | 1990-12-20 00:00:00 | 男 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 |
| 05 | 周梅 | 1991-12-01 00:00:00 | 女 |
| 06 | 吴兰 | 1992-01-01 00:00:00 | 女 |
| 07 | 郑竹 | 1989-01-01 00:00:00 | 女 |
+------+--------+---------------------+------+
7.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )(**)
select sid,count(*)as co,sum(score)as su from sc group by sid;
+------+----------+------------+
| sid | co | su |
+------+----------+------------+
| 01 | 3 | 269.0 |
| 02 | 3 | 210.0 |
| 03 | 3 | 240.0 |
| 04 | 3 | 100.0 |
| 05 | 2 | 163.0 |
| 06 | 2 | 65.0 |
| 07 | 2 | 187.0 |
+------+----------+------------+
select stu.sid,stu.sname,s2.co,s2.su from student as stu
left join (select sid,count(*)as co,sum(score)as su from sc group by sid)as s2
on stu.sid=s2.sid;
+------+--------+------+-------+
| sid | sname | co | su | #这里join的是一个表,但是没有count(sid)的数据,所以这里co显示的null
+------+--------+------+-------+
| 01 | 赵雷 | 3 | 269.0 | #将聚集函数拿出来,先join两个表再求count 和 sum 就不会这样了
| 02 | 钱电 | 3 | 210.0 |
| 03 | 孙风 | 3 | 240.0 |
| 04 | 李云 | 3 | 100.0 |
| 05 | 周梅 | 2 | 163.0 |
| 06 | 吴兰 | 2 | 65.0 |
| 07 | 郑竹 | 2 | 187.0 |
| 09 | 张三 | NULL | NULL |
| 10 | 李四 | NULL | NULL |
| 11 | 李四 | NULL | NULL |
| 12 | 赵六 | NULL | NULL |
| 13 | 孙七 | NULL | NULL |
+------+--------+------+-------+
select stu.sid,stu.sname,count(sc.sid),sum(sc.score) from student as stu left join sc
on stu.sid=sc.sid
group by stu.sid,stu.sname; # 因为select里聚合函数前有多个字段,所以这里必须与前面统一不然报错
+------+--------+---------------+---------------+
| sid | sname | count(sc.sid) | sum(sc.score) |
+------+--------+---------------+---------------+
| 01 | 赵雷 | 3 | 269.0 |
| 02 | 钱电 | 3 | 210.0 |
| 03 | 孙风 | 3 | 240.0 |
| 04 | 李云 | 3 | 100.0 |
| 05 | 周梅 | 2 | 163.0 |
| 06 | 吴兰 | 2 | 65.0 |
| 07 | 郑竹 | 2 | 187.0 |
| 09 | 张三 | 0 | NULL |
| 10 | 李四 | 0 | NULL |
| 11 | 李四 | 0 | NULL |
| 12 | 赵六 | 0 | NULL |
| 13 | 孙七 | 0 | NULL |
+------+--------+---------------+---------------+
8.查询「李」姓老师的数量
select count(*) from teacher where tname like '李%';
9.查询学过「张三」老师授课的同学的信息(*)
select tid from teacher where tname='张三';
select cid from course where tid=1;
select distinct sid from sc where cid=2 ;
select course.cid from course
where tid in (select tid from teacher where tname='张三');
select distinct sid from sc where cid in(select course.cid from course
where tid in (select tid from teacher where tname='张三'));
select stu.sid,stu.sname,stu.sage,stu.ssex from student as stu
join (select distinct sid from sc where cid in(select course.cid from course
where tid in (select tid from teacher where tname='张三')))as s1
on stu.sid=s1.sid;
+------+--------+---------------------+------+
| sid | sname | sage | ssex |
+------+--------+---------------------+------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 03 | 孙风 | 1990-12-20 00:00:00 | 男 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 |
| 05 | 周梅 | 1991-12-01 00:00:00 | 女 |
| 07 | 郑竹 | 1989-01-01 00:00:00 | 女 |
+------+--------+---------------------+------+
# 参考答案 更简洁
select stu.*
from Student as stu
join SC on stu.SId = SC.SId
join course as C on SC.CId = C.CId
join Teacher as T on C.TId = T.TId
where T.Tname = '张三';
10.查询没有学全所有课程的同学的信息(****)
select count(cid) from course;
select sid,count(cid) from sc group by sid having count(cid)<3;
select stu.* from student as stu
right join (select sid,count(cid) from sc group by sid having count(cid)<3)as s1
on stu.sid=s1.sid
union
select stu.* from student as stu
where stu.sid not in (select distinct sid from sc);
+------+--------+---------------------+------+
| SId | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 05 | 周梅 | 1991-12-01 00:00:00 | 女 |
| 06 | 吴兰 | 1992-01-01 00:00:00 | 女 |
| 07 | 郑竹 | 1989-01-01 00:00:00 | 女 |
| 09 | 张三 | 2017-12-20 00:00:00 | 女 |
| 10 | 李四 | 2017-12-25 00:00:00 | 女 |
| 11 | 李四 | 2012-06-06 00:00:00 | 女 |
| 12 | 赵六 | 2013-06-13 00:00:00 | 女 |
| 13 | 孙七 | 2014-06-01 00:00:00 | 女 |
+------+--------+---------------------+------+
# 答案使用排除法,select出不在 count(cid)=3 的信息便可
select * from Student where SId not in(
select SId from SC group by SId having count(CId) = (select count(*) from course)
);
11.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息(**)
select cid from sc where sid=1;
select distinct sid from sc
where cid in (select cid from sc where sid=1);
select * from student
where sid in (select sid from sc
where cid in (select cid from sc where sid=1));
+------+--------+---------------------+------+
| SId | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 03 | 孙风 | 1990-12-20 00:00:00 | 男 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 |
| 05 | 周梅 | 1991-12-01 00:00:00 | 女 |
| 06 | 吴兰 | 1992-01-01 00:00:00 | 女 |
| 07 | 郑竹 | 1989-01-01 00:00:00 | 女 |
+------+--------+---------------------+------+
12.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息(******)
-- select sid,count(cid) from sc
-- where cid in (select cid from sc where sid=5)
-- group by sid
-- having count(*)=(select count(*) from sc where sid=5 group by sid);
-- select stu.* from student as stu
-- join (select sid,count(*) from sc
-- where cid in (select cid from sc where sid=1)
-- group by sid
-- having count(*)=(select count(*) from sc where sid=1 group by sid)) as ss
-- on stu.sid=ss.sid;
-- select s2.sid
-- from sc as s1 join sc as s2
-- on s1.cid=s2.cid and s1.sid=04 and s2.sid!=04
-- group by s2.sid
-- having count(s2.cid)=(select count(cid) from sc where sid=4);
# 列出两个sc表附上课程数,然后从两个表中根据sid不等,cid相等,count(cid)相等
'''
此处要更改 group by 后面内容必须与select后内容一致的情况:
查看当前数据库中的 sql_mode
select @@sql_mode;
-- 1.修改全局中的 sql_mode
set @@global.sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
-- 2.修改mysql的配置文件 my.ini [mysqld]
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
更改过后 select *,count(cid) as num from sc group by sid 这一句就不会报错了
'''
select s2.sid,student.sname from
(select *,count(cid) as num from sc group by sid ) as s1
join
(select *,count(cid) as num from sc group by sid) as s2
on s1.cid=s2.cid
and s1.sid=1
and s2.sid!=1
and s1.num=s2.num
join student
on student.sid = s2.sid;
+------+--------+---------------------+------+
| SId | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 03 | 孙风 | 1990-12-20 00:00:00 | 男 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 |
+------+--------+---------------------+------+
13.查询没学过"张三"老师讲授的任一门课程的学生姓名(***)
select tid from teacher where tname='张三';
select cid from course
where tid in (select tid from teacher where tname='张三');
select sid from sc
where cid in(select cid from course
where tid in (select tid from teacher where tname='张三'));
select sid from student
where sid not in (select sid from sc
where cid in(select cid from course
where tid in (select tid from teacher where tname='张三')));
+------+
| sid |
+------+
| 06 |
| 09 |
| 10 |
| 11 |
| 12 |
| 13 |
+------+
14.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select sid,avg(score)as av,count(cid)as co from sc where score<60 group by sid having co>=2;
select stu.sid,stu.sname,sa.av from student as stu
right join (select sid,avg(score)as av,count(cid)as co
from sc where score<60 group by sid having co>=2) as sa
on stu.sid=sa.sid;
+------+--------+----------+
| sid | sname | av |
+------+--------+----------+
| 04 | 李云 | 33.33333 |
| 06 | 吴兰 | 32.50000 |
+------+--------+----------+
15.检索" 01 "课程分数小于 60,按分数降序排列的学生信息(*)
select * from sc where cid=1 and score<60 order by score desc;
+------+------+-------+
| SId | CId | score |
+------+------+-------+
| 04 | 01 | 50.0 |
| 06 | 01 | 31.0 |
+------+------+-------+
16.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩(***)
select sid,avg(score) as av from sc group by sid order by av desc;
select * from sc left join (select sid,avg(score) as av from sc group by sid)as s1
on sc.sid=s1.sid
order by av desc;
+------+------+-------+------+----------+
| SId | CId | score | sid | av |
+------+------+-------+------+----------+
| 07 | 02 | 89.0 | 07 | 93.50000 |
| 07 | 03 | 98.0 | 07 | 93.50000 |
| 01 | 03 | 99.0 | 01 | 89.66667 |
| 01 | 01 | 80.0 | 01 | 89.66667 |
| 01 | 02 | 90.0 | 01 | 89.66667 |
| 05 | 01 | 76.0 | 05 | 81.50000 |
| 05 | 02 | 87.0 | 05 | 81.50000 |
| 03 | 03 | 80.0 | 03 | 80.00000 |
| 03 | 01 | 80.0 | 03 | 80.00000 |
| 03 | 02 | 80.0 | 03 | 80.00000 |
| 02 | 02 | 60.0 | 02 | 70.00000 |
| 02 | 03 | 80.0 | 02 | 70.00000 |
| 02 | 01 | 70.0 | 02 | 70.00000 |
| 04 | 02 | 30.0 | 04 | 33.33333 |
| 04 | 03 | 20.0 | 04 | 33.33333 |
| 04 | 01 | 50.0 | 04 | 33.33333 |
| 06 | 01 | 31.0 | 06 | 32.50000 |
| 06 | 03 | 34.0 | 06 | 32.50000 |
+------+------+-------+------+----------+
#最优解(摘抄)
select a.sname,b.score 语文,c.score 数学,d.score 英语,avg(e.score)
from student a
left join sc b on a.sid=b.sid and b.cid='01'
left join sc c on a.sid=c.sid and c.cid='02'
left join sc d on a.sid=d.sid and d.cid='03'
left join sc e on a.sid=e.sid
group by a.sname,语文,数学,英语
order by avg(e.score) desc;
17.查询各科成绩最高分、最低分和平均分:(*******)
以如下形式显示:
课程 ID,课程 name,最高分,最低分,平均分,及格率(>=60),中等率(70-80),优良率(80-90),优秀率(>=90)
要求输出 课程号 和 选修人数 ,查询结果按 人数 降序排列,若人数相同,按 课程号 升序排列
#选修人数 最高分 最低分 平均分
select cid,count(sid),max(score),min(score),avg(score) from sc group by cid;
#及格率 中等率 优良率 优秀率
select cid,SUM(CASE WHEN score >= 60 THEN 1 ELSE 0 END) / count(CId)as 及格率,
SUM(CASE WHEN score>=70 and score<80 THEN 1 ELSE 0 END) / count(CId)as 中等率,
SUM(CASE WHEN score>=80 and score<90 THEN 1 ELSE 0 END) / count(CId)as 优良率,
SUM(CASE WHEN score>=90 THEN 1 ELSE 0 END) / count(CId)as 优秀率
from sc
group by cid;
select sc.cid,c.cname,
count(sc.cid)as 人数,
max(sc.score)as 最大值,
min(sc.score)as 最小值,
avg(sc.score)as 平均值,
SUM(IF(sc.score >= 60,1,0) ) / count(sc.CId)as 及格率,
SUM(CASE WHEN sc.score>=70 and sc.score<80 THEN 1 ELSE 0 END) / count(sc.CId)as 中等率,
SUM(CASE WHEN sc.score>=80 and sc.score<90 THEN 1 ELSE 0 END) / count(sc.CId)as 优良率,
SUM(CASE WHEN sc.score>=90 THEN 1 ELSE 0 END) / count(sc.CId)as 优秀率
from sc,course as c
where sc.cid=c.cid
group by sc.cid,c.cname
order by 人数 desc,sc.cid;
+------+--------+--------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+
| cid | cname | 人数 | 最大值 | 最小值 | 平均值 | 及格率 | 中等率 | 优良率 | 优秀率 |
+------+--------+--------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+
| 01 | 语文 | 6 | 80.0 | 31.0 | 64.50000 | 0.6667 | 0.3333 | 0.3333 | 0.0000 |
| 02 | 数学 | 6 | 90.0 | 30.0 | 72.66667 | 0.8333 | 0.0000 | 0.5000 | 0.1667 |
| 03 | 英语 | 6 | 99.0 | 20.0 | 68.50000 | 0.6667 | 0.0000 | 0.3333 | 0.3333 |
+------+--------+--------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+
18.按各科平均成绩进行排序,并显示排名, Score 重复时保留名次空缺(****)
select cid,avg(score)as 平均分 from sc group by cid order by 平均分 desc;
+------+-----------+
| cid | 平均分 |
+------+-----------+
| 02 | 72.66667 |
| 03 | 68.50000 |
| 01 | 64.50000 |
+------+-----------+
select b.cid,b.avg_sc,count(distinct a.avg_sc)as rank
from (select avg(score) as avg_sc from sc group by cid) as a
join (select cid,avg(score) as avg_sc from sc group by cid) as b
on a.avg_sc>=b.avg_sc
group by b.cid
order by rank;
+------+----------+------+
| cid | avg_sc | rank |
+------+----------+------+
| 02 | 72.66667 | 1 |
| 03 | 68.50000 | 2 |
| 01 | 64.50000 | 3 |
+------+----------+------+
19.按各科平均成绩进行排序,并显示排名, Score 重复时不保留名次空缺(****)
同上
select b.cid,b.avg_sc,@i := @i+1 as rank
from (select @i:=0)a,
(select cid,avg(score)as avg_sc from sc group by cid order by avg_sc desc)b;
+------+----------+------+
| cid | avg_sc | rank |
+------+----------+------+
| 02 | 72.66667 | 1 |
| 03 | 68.50000 | 2 |
| 01 | 64.50000 | 3 |
+------+----------+------+
20.查询学生的总成绩,并进行排名,总分重复时保留名次空缺(****)
select sid,sum(score) from sc group by sid;
select stu.*,s1.总分 from student as stu
left join (select sid,sum(score)as 总分 from sc group by sid)as s1
on stu.sid=s1.sid
order by 总分 desc;
select b.sid,b.总分,count(distinct a.总分) as rank
from (select sum(score)as 总分 from sc group by sid)a
join (select sid,sum(score) as 总分 from sc group by sid) b
on a.总分>=b.总分
group by b.sid
order by rank;
+------+--------+------+
| sid | 总分 | rank |
+------+--------+------+
| 01 | 269.0 | 1 |
| 03 | 240.0 | 2 |
| 02 | 210.0 | 3 |
| 07 | 187.0 | 4 |
| 05 | 163.0 | 5 |
| 04 | 100.0 | 6 |
| 06 | 65.0 | 7 |
+------+--------+------+
21.查询学生的总成绩,并进行排名,总分重复时不保留名次空缺
select sid,总分,@i := @i+1 as rank
from (select @i :=0)a,
(select sid,sum(score) as 总分 from sc group by sid order by 总分 desc) b;
+------+--------+------+
| sid | 总分 | rank |
+------+--------+------+
| 01 | 269.0 | 1 |
| 03 | 240.0 | 2 |
| 02 | 210.0 | 3 |
| 07 | 187.0 | 4 |
| 05 | 163.0 | 5 |
| 04 | 100.0 | 6 |
| 06 | 65.0 | 7 |
+------+--------+------+
22.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0]及所占百分比(***)
select co.cid,co.cname,
sum(case when sc.score>=85 and sc.score<=100 then 1 else 0 end)/count(sc.sid) '[100-85]',# 这里as的要显示的字符串必须加引号
sum(case when sc.score>=70 and sc.score<85 then 1 else 0 end)/count(sc.sid) '[85-70]',
sum(case when sc.score>=60 and sc.score<70 then 1 else 0 end)/count(sc.sid) '[70-60]',
sum(case when sc.score<60 and sc.score>=0 then 1 else 0 end)/count(sc.sid) '[60-0]'
from sc,course as co
where sc.cid=co.cid
group by co.cid,co.cname;
+------+--------+----------+---------+---------+--------+
| cid | cname | [100-85] | [85-70] | [70-60] | [60-0] |
+------+--------+----------+---------+---------+--------+
| 01 | 语文 | 0.0000 | 0.6667 | 0.0000 | 0.3333 |
| 02 | 数学 | 0.5000 | 0.1667 | 0.1667 | 0.1667 |
| 03 | 英语 | 0.3333 | 0.3333 | 0.0000 | 0.3333 |
+------+--------+----------+---------+---------+--------+
select co.cid,co.cname, ## 切记这里的%,需要显示的字符串皆需要引号
concat(sum(case when sc.score>=85 and sc.score<=100 then 1 else 0 end)/count(sc.sid)*100,'%') '[100-85]',
concat(sum(case when sc.score>=70 and sc.score<85 then 1 else 0 end)/count(sc.sid)*100,'%') '[85-70]',
concat(sum(case when sc.score>=60 and sc.score<70 then 1 else 0 end)/count(sc.sid)*100,'%') '[70-60]',
concat(sum(case when sc.score<60 and sc.score>=0 then 1 else 0 end)/count(sc.sid)*100,'%') '[60-0]'
from sc,course as co
where sc.cid=co.cid
group by co.cid,co.cname;
+------+--------+----------+----------+----------+----------+
| cid | cname | [100-85] | [85-70] | [70-60] | [60-0] |
+------+--------+----------+----------+----------+----------+
| 01 | 语文 | 0.0000% | 66.6667% | 0.0000% | 33.3333% |
| 02 | 数学 | 50.0000% | 16.6667% | 16.6667% | 16.6667% |
| 03 | 英语 | 33.3333% | 33.3333% | 0.0000% | 33.3333% |
+------+--------+----------+----------+----------+----------+
23.查询各科成绩前三名的记录(*)
(select cid,score from sc where cid=1 order by score desc limit 3)
union all ### 注意这里 多个 union 每一段都需要用括号括起来,前面一个union则不需要
(select cid,score from sc where cid=2 order by score desc limit 3)
union all
(select cid,score from sc where cid=3 order by score desc limit 3);
+------+-------+
| cid | score |
+------+-------+
| 01 | 80.0 |
| 01 | 80.0 |
| 01 | 76.0 |
| 02 | 90.0 |
| 02 | 89.0 |
| 02 | 87.0 |
| 03 | 99.0 |
| 03 | 98.0 |
| 03 | 80.0 |
+------+-------+
24.查询每门课程被选修的学生数(*)
select cid,count(sid) from sc group by cid;
+------+------------+
| cid | count(sid) |
+------+------------+
| 01 | 6 |
| 02 | 6 |
| 03 | 6 |
+------+------------+
25.查询出只选修两门课程的学生学号和姓名(*)
select stu.sid,stu.sname
from student as stu
right join (select sid,count(cid) from sc group by sid having count(cid)=2) as a
on stu.sid=a.sid;
+------+--------+
| sid | sname |
+------+--------+
| 05 | 周梅 |
| 06 | 吴兰 |
| 07 | 郑竹 |
+------+--------+
26.查询男生、女生人数(*)
select ssex,count(*) from student group by ssex;
+------+----------+
| ssex | count(*) |
+------+----------+
| 女 | 8 |
| 男 | 4 |
+------+----------+
27.查询名字中含有「风」字的学生信息(*)
select * from student where sname like '%风%';
+------+--------+---------------------+------+
| SId | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 03 | 孙风 | 1990-12-20 00:00:00 | 男 |
+------+--------+---------------------+------+
28.查询同名同性学生名单,并统计同名人数(*)
select stu1.sname,stu1.ssex,count(stu1.sname) from student as stu1
join student as stu2
on stu1.sid != stu2.sid
where stu1.sname=stu2.sname and stu1.ssex=stu2.ssex
group by stu1.sname,stu1.ssex;
+--------+------+-------------------+
| sname | ssex | count(stu1.sname) |
+--------+------+-------------------+
| 李四 | 女 | 2 |
+--------+------+-------------------+
29.查询 1990 年出生的学生名单(*)
select * from student where sage like '1990%';
+------+--------+---------------------+------+
| SId | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 03 | 孙风 | 1990-12-20 00:00:00 | 男 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 |
+------+--------+---------------------+------+
select * from Student where year(Sage) = 1990;
30.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列(*)
select cid,avg(score) from sc group by cid order by avg(score) desc,cid;
+------+------------+
| cid | avg(score) |
+------+------------+
| 02 | 72.66667 |
| 03 | 68.50000 |
| 01 | 64.50000 |
+------+------------+
31.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩(*)
select sc.sid,stu.sname,avg(sc.score) as avg_sc
from student as stu
inner join sc
on stu.sid=sc.sid
group by sc.sid,stu.sname
having avg_sc>=85;
+------+--------+----------+
| sid | sname | avg_sc |
+------+--------+----------+
| 01 | 赵雷 | 89.66667 |
| 07 | 郑竹 | 93.50000 |
+------+--------+----------+
32.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数(*)
select course.cname,stu.sname,sc.score from course
join sc on sc.cid=course.cid
join student as stu on sc.sid=stu.sid
where course.cname='数学'
and sc.score<60;
+--------+--------+-------+
| cname | sname | score |
+--------+--------+-------+
| 数学 | 李云 | 30.0 |
+--------+--------+-------+
33.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)(*)
select student.sname,course.cname,sc.score from student
left join sc on student.sid=sc.sid
left join course on sc.cid=course.cid;
+--------+--------+-------+
| sname | cname | score |
+--------+--------+-------+
| 赵雷 | 语文 | 80.0 |
| 钱电 | 语文 | 70.0 |
| 孙风 | 语文 | 80.0 |
| 李云 | 语文 | 50.0 |
| 周梅 | 语文 | 76.0 |
| 吴兰 | 语文 | 31.0 |
| 赵雷 | 数学 | 90.0 |
| 钱电 | 数学 | 60.0 |
| 孙风 | 数学 | 80.0 |
| 李云 | 数学 | 30.0 |
| 周梅 | 数学 | 87.0 |
| 郑竹 | 数学 | 89.0 |
| 赵雷 | 英语 | 99.0 |
| 钱电 | 英语 | 80.0 |
| 孙风 | 英语 | 80.0 |
| 李云 | 英语 | 20.0 |
| 吴兰 | 英语 | 34.0 |
| 郑竹 | 英语 | 98.0 |
| 张三 | NULL | NULL |
| 李四 | NULL | NULL |
| 李四 | NULL | NULL |
| 赵六 | NULL | NULL |
| 孙七 | NULL | NULL |
+--------+--------+-------+
34.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数(*)
select student.sname,course.cname,sc.score from student
join sc on student.sid=sc.sid
join course on sc.cid=course.cid
where sc.score>70;
+--------+--------+-------+
| sname | cname | score |
+--------+--------+-------+
| 赵雷 | 语文 | 80.0 |
| 赵雷 | 数学 | 90.0 |
| 赵雷 | 英语 | 99.0 |
| 钱电 | 英语 | 80.0 |
| 孙风 | 语文 | 80.0 |
| 孙风 | 数学 | 80.0 |
| 孙风 | 英语 | 80.0 |
| 周梅 | 语文 | 76.0 |
| 周梅 | 数学 | 87.0 |
| 郑竹 | 数学 | 89.0 |
| 郑竹 | 英语 | 98.0 |
+--------+--------+-------+
35.查询不及格的课程(*)
select student.sname,course.cname,sc.score from student
join sc on student.sid=sc.sid
join course on sc.cid=course.cid
where sc.score<60;
+--------+--------+-------+
| sname | cname | score |
+--------+--------+-------+
| 李云 | 语文 | 50.0 |
| 李云 | 数学 | 30.0 |
| 李云 | 英语 | 20.0 |
| 吴兰 | 语文 | 31.0 |
| 吴兰 | 英语 | 34.0 |
+--------+--------+-------+
36.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名(**)
### 因为1课程大于80分的没有,所以这里用大于号没有返回结果就会报错,所以必须当表里有满足条件的才行
select sc.sid,student.sname,sc.score
from student
join sc
on student.sid=sc.sid
where sc.cid=1 and sc.score>=80 ;
+------+--------+-------+
| sid | sname | score |
+------+--------+-------+
| 01 | 赵雷 | 80.0 |
| 03 | 孙风 | 80.0 |
+------+--------+-------+
37.求每门课程的学生人数(*)
select sc.cid,course.cname,count(sc.cid) from course
join sc on course.cid=sc.cid
group by sc.cid,course.cname;
+------+--------+---------------+
| cid | cname | count(sc.cid) |
+------+--------+---------------+
| 01 | 语文 | 6 |
| 02 | 数学 | 6 |
| 03 | 英语 | 6 |
+------+--------+---------------+
38.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩(**)
select course.cname,sc.sid,student.sname,student.ssex,sc.score from student
join sc on student.sid=sc.sid
join course on sc.cid=course.cid
join teacher on course.tid=teacher.tid
where teacher.tname='张三'
order by sc.score desc
limit 1;
+--------+------+--------+------+-------+
| cname | sid | sname | ssex | score |
+--------+------+--------+------+-------+
| 数学 | 01 | 赵雷 | 男 | 90.0 |
+--------+------+--------+------+-------+
39.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩(***)
select max(sc.score)
from sc join course on sc.cid=course.cid
join teacher on course.tid=teacher.tid
where teacher.tname='张三';
+---------------+
| max(sc.score) |
+---------------+
| 90.0 |
+---------------+
select course.cname,sc.sid,student.sname,student.ssex,sc.score from student
join sc on student.sid=sc.sid
join course on sc.cid=course.cid
join teacher on course.tid=teacher.tid
where teacher.tname='张三'
and sc.score=(select max(sc.score)
from sc join course on sc.cid=course.cid
join teacher on course.tid=teacher.tid
where teacher.tname='张三');
+--------+------+--------+------+-------+
| cname | sid | sname | ssex | score | #这是一种思路判断每个人得分数是否等于最大分
+--------+------+--------+------+-------+ #还有一种方法,求出最大分,然后cid与分数均相同的拉出来
| 数学 | 01 | 赵雷 | 男 | 90.0 |
+--------+------+--------+------+-------+
select stu.sname,sc.score
from student stu join sc on stu.sid=sc.sid
and sc.cid in (select cid from course where tid in (select tid from teacher where tname='张三'))
join (select cid,max(score) m from sc group by cid) c on sc.cid=c.cid and sc.score=c.m;
40.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩(*)
select distinct s1.* from sc as s1
join sc as s2 on s1.cid!=s2.cid #自联结的时候可将用于判断的直接作为on的条件
where s1.score=s2.score
order by s1.sid;
+------+------+-------+
| SId | CId | score |
+------+------+-------+
| 01 | 01 | 80.0 |
| 02 | 03 | 80.0 |
| 03 | 03 | 80.0 |
| 03 | 01 | 80.0 |
| 03 | 02 | 80.0 |
+------+------+-------+
41.查询每门课程成绩最好的前两名(*)
(select * from sc where cid=1 order by score desc limit 2)
union all
(select * from sc where cid=2 order by score desc limit 2)
union all
(select * from sc where cid=3 order by score desc limit 2);
+------+------+-------+
| SId | CId | score |
+------+------+-------+
| 01 | 01 | 80.0 |
| 03 | 01 | 80.0 |
| 01 | 02 | 90.0 |
| 07 | 02 | 89.0 |
| 01 | 03 | 99.0 |
| 07 | 03 | 98.0 |
+------+------+-------+
42.统计每门课程的学生选修人数(超过 5 人的课程才统计)(*)
select cid,count(sid) from sc group by cid having count(sid)>5;
+------+------------+
| cid | count(sid) |
+------+------------+
| 01 | 6 |
| 02 | 6 |
| 03 | 6 |
+------+------------+
43.检索至少选修两门课程的学生学号(*)
select sid from sc group by sid having count(cid)>=2;
+------+
| sid |
+------+
| 01 |
| 02 |
| 03 |
| 04 |
| 05 |
| 06 |
| 07 |
+------+
44.查询选修了全部课程的学生信息(**)
select sid from sc group by sid having count(cid)=(select count(cid) from course);
select stu.* from student as stu
right join (select sid from sc group by sid having count(cid)=(select count(cid) from course)) as s1
on stu.sid=s1.sid;
+------+--------+---------------------+------+
| SId | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 03 | 孙风 | 1990-12-20 00:00:00 | 男 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 |
+------+--------+---------------------+------+
45.查询各学生的年龄,只按年份来算(*)
select sname,year(now())-year(sage) as age from student ;
+--------+------+
| sname | age |
+--------+------+
| 赵雷 | 29 |
| 钱电 | 29 |
| 孙风 | 29 |
| 李云 | 29 |
| 周梅 | 28 |
| 吴兰 | 27 |
| 郑竹 | 30 |
| 张三 | 2 |
| 李四 | 2 |
| 李四 | 7 |
| 赵六 | 6 |
| 孙七 | 5 |
+--------+------+
46.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一(**)#timestampdiff() 从日期时间表达式中减去间隔
-- mysql> SELECT TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01');
-- -> 3
-- mysql> SELECT TIMESTAMPDIFF(YEAR,'2002-05-01','2001-01-01');
-- -> -1
-- mysql> SELECT TIMESTAMPDIFF(MINUTE,'2003-02-01','2003-05-01 12:05:55');
-- -> 128885
select student.SId,student.Sname,
TIMESTAMPDIFF(YEAR,student.Sage,CURDATE())as age from student;
+------+--------+------+
| SId | Sname | age |
+------+--------+------+
| 01 | 赵雷 | 29 |
| 02 | 钱电 | 28 |
| 03 | 孙风 | 28 |
| 04 | 李云 | 28 |
| 05 | 周梅 | 27 |
| 06 | 吴兰 | 27 |
| 07 | 郑竹 | 30 |
| 09 | 张三 | 1 |
| 10 | 李四 | 1 |
| 11 | 李四 | 7 |
| 12 | 赵六 | 6 |
| 13 | 孙七 | 5 |
+------+--------+------+
47.查询本周过生日的学生(**)#因为有闰年所以这题操作麻烦,先根据今天周几,然后将前后几天的范围与sage比较才对,以下做法错误
select * from student
where WEEKOFYEAR(student.Sage)=WEEKOFYEAR(CURDATE());
48.查询下周过生日的学生(**)
select * from student where weekofyear(sage)-1=weekofyear(curdate());
49.查询本月过生日的学生(**)
select * from student where month(sage)=month(curdate());
50.查询下月过生日的学生(**)
select * from student where month(sage)-1=month(curdate());
附1.SQL语句循环:
\d //
create procedure p1()
begin
set @i=1;
while @i<10000 do
insert into users values(null,concat('user:',@i),20,'100',@i);
set @i=@i+1;
end while;
end;
//
call p1()
附2.更改 group by 必须与select一致的情况:
查看当前数据库中的 sql_mode
select @@sql_mode;
-- 修改全局中的 sql_mode
set @@global.sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
-- 修改mysql的配置文件 my.ini [mysqld]
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION