mysql练习题
习题是从网上找的练习题,sql语句不太熟练,可能并不是最优解,有不对的地方请大家指正
表明和字段
- 学生表 Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
- 课程表 Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
- 教师表 Teacher(t_id,t_name) –教师编号,教师姓名
- 成绩表 Score(s_id,c_id,s_score) –学生编号,课程编号,分数
测试数据
--建表
--学生表
CREATE TABLE `Student`(
`s_id` VARCHAR(20),
`s_name` VARCHAR(20) NOT NULL DEFAULT '',
`s_birth` VARCHAR(20) NOT NULL DEFAULT '',
`s_sex` VARCHAR(10) NOT NULL DEFAULT '',
PRIMARY KEY(`s_id`)
);
--课程表
CREATE TABLE `Course`(
`c_id` VARCHAR(20),
`c_name` VARCHAR(20) NOT NULL DEFAULT '',
`t_id` VARCHAR(20) NOT NULL,
PRIMARY KEY(`c_id`)
);
--教师表
CREATE TABLE `Teacher`(
`t_id` VARCHAR(20),
`t_name` VARCHAR(20) NOT NULL DEFAULT '',
PRIMARY KEY(`t_id`)
);
--成绩表
CREATE TABLE `Score`(
`s_id` VARCHAR(20),
`c_id` VARCHAR(20),
`s_score` INT(3),
PRIMARY KEY(`s_id`,`c_id`)
);
--插入学生表测试数据
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女');
--课程表测试数据
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
--教师表测试数据
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
--成绩表测试数据
insert into Score values('01' , '01' , 80);
insert into Score values('01' , '02' , 90);
insert into Score values('01' , '03' , 99);
insert into Score values('02' , '01' , 70);
insert into Score values('02' , '02' , 60);
insert into Score values('02' , '03' , 80);
insert into Score values('03' , '01' , 80);
insert into Score values('03' , '02' , 80);
insert into Score values('03' , '03' , 80);
insert into Score values('04' , '01' , 50);
insert into Score values('04' , '02' , 30);
insert into Score values('04' , '03' , 20);
insert into Score values('05' , '01' , 76);
insert into Score values('05' , '02' , 87);
insert into Score values('06' , '01' , 31);
insert into Score values('06' , '03' , 34);
insert into Score values('07' , '02' , 89);
insert into Score values('07' , '03' , 98);
查询”01”课程比”02”课程成绩高的学生的信息及课程分数
SELECT
a.*,
b.s_score 01 _score,
c.s_score 02 _score
FROM
student a
JOIN score b ON a.s_id = b.s_id
AND b.c_id = '01'
LEFT JOIN score c ON a.s_id = c.s_id
AND c.c_id = '02'
WHERE
b.s_score > c.s_score
查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
SELECT
a.s_id,
a.s_name,
round( AVG( b.s_score ), 1 ) AS avg
FROM
student a
JOIN score b ON a.s_id = b.s_id
GROUP BY
b.s_id
HAVING
avg >= '60'
查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩 – (包括有成绩的和无成绩的)
union 合并两个或多个 SELECT 语句的结果集(select语句必须有相同的列)
SELECT
a.s_id,
a.s_name,
ROUND( AVG( b.s_score ), 2 ) avg
FROM
student a
JOIN score b ON a.s_id = b.s_id
GROUP BY
b.s_id
HAVING
avg < 60 UNION
SELECT
a.s_id,
a.s_name,
0 AS avg
FROM
student a
WHERE
a.s_id NOT IN ( SELECT DISTINCT s_id FROM score )
------------------------------------------------------------------
SELECT
a.s_id,
a.s_name,
ROUND( AVG( b.s_score ), 2 ) avg
FROM
student a
LEFT JOIN score b ON a.s_id = b.s_id
GROUP BY
a.s_id
HAVING
avg < 60
OR avg IS NULL
查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
SELECT
a.s_id,
a.s_name,
count( b.c_id ),
sum( b.s_score )
FROM
student a
LEFT JOIN score b ON a.s_id = b.s_id
GROUP BY
a.s_id
查询学过”张三”老师授课的同学的信息
SELECT
a.*
FROM
student a
JOIN score b ON a.s_id = b.s_id
AND b.c_id IN
( SELECT c.c_id FROM course c JOIN teacher d ON c.t_id = d.t_id AND d.t_name = '张三' )
查询没学过”张三”老师授课的同学的信息
思路:先查询出学过张三授课的所有同学id,id不在此集合中的同学就是没有选张三老师的课
select * from student where s_id not in(
select a.s_id
from student a
join score b on a.s_id = b.s_id and b.c_id in
(select c.c_id from course c
join teacher d on c.t_id = d.t_id and d.t_name = '张三'))
查询学过编号为”01”并且也学过编号为”02”的课程的同学的信息
隐式内连接和显示内连接
显示
select a.*
from student a
join score b on a.s_id = b.s_id and b.c_id = '01'
join score c on a.s_id = c.s_id and c.c_id = '02'
----------------------------------
隐式
select a.* from
student a,score b,score c
where a.s_id = b.s_id and a.s_id = c.s_id and b.c_id='01' and c.c_id='02'
----------------------------------
SELECT
a.*
FROM
student a
WHERE
a.s_id IN ( SELECT s_id FROM score WHERE c_id = '01' )
AND a.s_id NOT IN ( SELECT s_id FROM score WHERE c_id = '02' )
查询学过编号为”01”但是没有学过编号为”02”的课程的同学的信息
SELECT
a.*
FROM
student a
WHERE
a.s_id IN ( SELECT s_id FROM score WHERE c_id = '01' )
AND a.s_id NOT IN ( SELECT s_id FROM score WHERE c_id = '02' )
查询没有学全所有课程的同学的信息
先查询出学全所有课程的同学,再not in
select a.*
from student a
where a.s_id not in (select s_id from score group by s_id having count(1) =
(select count(1) from course))
查询至少有一门课与学号为”01”的同学所学相同的同学的信息
先找出所有复合条件的s_id
select a.*
from student a
where a.s_id != '01' and a.s_id in
(select DISTINCT s_id from score where c_id in
(select c_id from score where s_id = '01'))
==查询和”01”号的同学学习的课程完全相同的其他同学的信息==
c_id 在01同学的课程id集合中,并且所有课程的个数与01同学相等
select a.*
from student a
where a.s_id in
(select s_id from score where s_id != '01' and c_id in
(select c_id from score where s_id = '01')
group by s_id
having count(1) = (select count(1) from score where s_id = '01'))
查询没学过”张三”老师讲授的任一门课程的学生姓名
select a.s_name
from student a
where a.s_id not in
(select DISTINCT s_id from score where c_id in
(select c_id from course b
join teacher c on b.t_id = c.t_id and c.t_name = '张三'))
查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
join过滤条件放在on和where后区别:
内连接时无区别,外连接时不同
join过程:on是对笛卡尔积进行过滤,where是对以上过滤结果再次进行过滤
select a.s_id,a.s_name,ROUND(AVG(b.s_score),2)
from student a,score b
where a.s_id =b.s_id and a.s_id in
(select s_id from score where s_score <= 60 group by s_id having count(1) >= 2 )
group by a.s_id
select a.s_id,a.s_name,ROUND(AVG(b.s_score),2)
from student a
join score b on a.s_id =b.s_id where a.s_id in
(select s_id from score where s_score <= 60 group by s_id having count(1) >= 2 )
group by a.s_id
检索”01”课程分数小于60,按分数降序排列的学生信息
SELECT
a.*,
b.s_score
FROM
student a
JOIN score b ON a.s_id = b.s_id
AND b.c_id = '01'
AND b.s_score < 60
ORDER BY
b.s_score DESC
==按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩==
select a.*,b.01_score,b.02_score,b.03_score,b.avg
from student a
left join
(select c.s_id,
(select s_score from score where c.s_id = s_id and c_id = '01')as 01_score,
(select s_score from score where c.s_id = s_id and c_id = '02')as 02_score,
(select s_score from score where c.s_id = s_id and c_id = '03')as 03_score,
AVG(s_score) as avg from score c group by c.s_id) as b
on a.s_id = b.s_id
order by b.avg desc
-------------------------------------------------
select a.*,b.avg from
(select a.*,b.s_score 01_score,c.s_score 02_score,d.s_score 03_score
from student a
left join score b on a.s_id = b.s_id and b.c_id = '01'
left join score c on a.s_id = c.s_id and c.c_id = '02'
left join score d on a.s_id = d.s_id and d.c_id = '03'
group by a.s_id)as a left join
(select s_id ,round(avg(s_score),2)as avg from score group by s_id)as b
on a.s_id = b.s_id
order by b.avg desc
查询每门课程被选修的学生数
select c_id,count(1)
from score
group by c_id
查询出只有两门课程的全部学生的学号和姓名
select a.*
from student a
where a.s_id in
(select s_id from score group by s_id having count(1) = 2)
查询男生、女生人数
select s_sex,count(1)
from student
where s_sex = '男'
union
select s_sex,count(1)
from student
where s_sex = '女'
----------------------------
select s_sex,count(1)
from student
group by s_sex
查询同名同性学生名单,并统计同名人数
select a.s_name,a.s_sex,count(1)
from student a
group by a.s_name,a.s_sex
having count(1) >1
--------------------
select a.s_name,a.s_sex,count(1)
from student a
join student b
on a.s_name = b.s_name and a.s_sex = b.s_sex and a.s_id != b.s_id(一定要注意加上1表和2表的id不等)
查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
select c_id,avg(s_score) as avg
from score
group by c_id
order by avg desc,c_id asc
查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
select a.s_id,a.s_name,b.avg
from student a
join (
select s_id,round(avg(s_score),2) avg from score
group by s_id having avg >= 85) as b
on a.s_id = b.s_id
查询课程名称为”数学”,且分数低于60的学生姓名和分数
select a.s_id,a.s_name,b.s_score
from student a
join score b on a.s_id = b.s_id
and b.c_id = (select c_id from course where c_name = '数学')
and b.s_score < 60
查询所有学生的课程及分数情况;
select a.*,b.sum from
(select a.s_id,a.s_name,
b.s_score '数学',c.s_score '语文',d.s_score '英语'
from student a
left join score b on a.s_id = b.s_id and b.c_id = '01'
left join score c on a.s_id = c.s_id and c.c_id = '02'
left join score d on a.s_id = d.s_id and d.c_id = '03') as a
left join (select s_id,sum(s_score) as sum from score group by s_id) as b
on a.s_id = b.s_id
----------------------------------------------------------------
select a.s_id,
(select s_score from score where a.s_id = s_id and c_id = '01' ) as '数学',
(select s_score from score where a.s_id = s_id and c_id = '02' ) as '语文',
(select s_score from score where a.s_id = s_id and c_id = '03' ) as '英语',
sum(s_score) as sum
from score a group by s_id
查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
select c.s_name,b.c_name,a.s_score from
(select s_id,c_id,s_score
from score a
where s_score > 70
group by s_id,c_id) as a
left join course b on a.c_id = b.c_id
left join student c on a.s_id = c.s_id
-------------------------------------------------------
select a.s_name,b.c_name,c.s_score from course b
left join score c on b.c_id = c.c_id
left join student a on a.s_id=c.s_id
where c.s_score>70
查询课程编号为01且课程成绩在80分以上的学生的学号和姓名;
select b.s_id,b.s_name,a.s_score
from score a
join student b on a.s_id = b.s_id
and a.c_id = '01'
and a.s_score >= 80
查询选修”张三”老师所授课程的学生中,成绩最高的学生信息及其成绩
select b.*,max(a.s_score) as max,c.c_name
from score a
join student b on a.s_id = b.s_id and a.c_id = (
select c_id from course where t_id = (select t_id from teacher where t_name = '张三'))
join course c on c.c_id = a.c_id
查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
select DISTINCT c.*,a.*
from score a
join score b on a.c_id != b.c_id and a.s_score = b.s_score
join student c on a.s_id = c.s_id
查询每门功成绩最好的前两名
select a.*,b.s_name
from score a
join student b on a.s_id = b.s_id
where
(select count(1) from score where s_score >= a.s_score and c_id = a.c_id) <=2
order by a.c_id
统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select c_id,count(1) as count
from score
group by c_id
having count(1) >=5
order by count desc,c_id asc
检索至少选修两门课程的学生学号
select s_id,count(1)
from score
group by s_id
having count(1) >= 2
查询选修了全部课程的学生信息
select a.*
from student a
where a.s_id in
(select s_id from score group by s_id having count(1) = (
select count(1) from course))
查询各学生的年龄
– 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
select s_id,s_birth,
(DATE_FORMAT(now(),'%Y')- DATE_FORMAT(s_birth,'%Y')-
(case when DATE_FORMAT(now(),'%m%d') > DATE_FORMAT(s_birth,'%m%d') then 0 else 1 end)) as age
from student
查询本周过生日的学生
select *
from student
where WEEK(s_birth) = WEEK(now())
查询下周过生日的学生
select *
from student
where WEEK(s_birth) = WEEK(now())+1
查询本月过生日的学生
select *
from student
where MONTH(s_birth) = MONTH(now())
查询下月过生日的学生
select *
from student
where MONTH(s_birth) = MONTH(now())+1
查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
–及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
select a.c_id,b.c_name,
max(s_score) max,
min(s_score) min,
round(avg(s_score),2) avg,
(select count(1) from score where c_id = a.c_id and s_score >= 60)/count(1) '及格率',
(select count(1) from score where c_id = a.c_id and s_score >= 70 and s_score <= 80)/count(1) '中等率',
(select count(1) from score where c_id = a.c_id and s_score >= 80 and s_score <= 90)/count(1) '优良率',
(select count(1) from score where c_id = a.c_id and s_score >= 90)/count(1) '优秀率'
from score a
join course b on a.c_id = b.c_id
group by a.c_id
-----------------------------------------------------------
select a.c_id,b.c_name,
max(a.s_score) max,
min(a.s_score) min,
round(avg(a.s_score),2) avg,
round(sum(case when a.s_score >= 60 then 1 else 0 end)/sum(case when a.s_score then 1 else 0 end),2) '及格率',
round(sum(case when a.s_score BETWEEN 70 and 80 then 1 else 0 end)/sum(case when a.s_score then 1 else 0 end),2) '中等率',
round(sum(case when a.s_score BETWEEN 80 and 90 then 1 else 0 end)/sum(case when a.s_score then 1 else 0 end),2) '优良率',
round(sum(case when a.s_score >= 90 then 1 else 0 end)/sum(case when a.s_score then 1 else 0 end),2) '优秀率'
from score a
join course b on a.c_id = b.c_id
group by a.c_id
查询排名
因为mysql没有sql server,mysql的rank()函数,需要自己实现排名
排名的话首先实现一个已经从高到低排好序的表格
查询学生的总成绩并进行排名
注意@k和@score
的顺序,@score是上一次比较的总分成绩,如果这一次的总分与上一次相等,那么@i依然+1,但是@k即rank的值不变
select a.s_id as id,a.sum,
@i:= @i+1 as i,
@k:= (case when a.sum = @score then @k else @i end) as rank,
@score:= a.sum as pre_score
from (select *,sum(s_score) as sum from score group by s_id order by sum desc) as a,
(select @i:=0,@k:=1,@score:=0)s
如果@score写在@k前面面,那么case条件中的a.sum = @score必然相等,会出现以下结果
查询不同老师所教不同课程平均分从高到低显示
select c.t_id,c.t_name,a.c_id,b.c_name,round(avg(a.s_score),2)as avg
from score a
join course b on a.c_id = b.c_id
join teacher c on b.t_id = c.t_id
group by a.c_id
order by avg desc
查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
有相同分数时名次会出现一点偏差
以下两种思路:
select c.*,a.* from
(select b.*,
@i:=@i+1 as 排名
from (select * from score where c_id = '01' order by s_score desc) b,
(select @i:=0)s) a join student c on a.s_id = c.s_id
where a.排名 BETWEEN 2 and 3
union
select c.*,a.* from
(select b.*,
@i:=@i+1 as 排名
from (select * from score where c_id = '02' order by s_score desc) b,
(select @i:=0)s) a join student c on a.s_id = c.s_id
where a.排名 BETWEEN 2 and 3
union
select c.*,a.* from
(select b.*,
@i:=@i+1 as 排名
from (select * from score where c_id = '03' order by s_score desc) b,
(select @i:=0)s) a join student c on a.s_id = c.s_id
where a.排名 BETWEEN 2 and 3
---------------------------------------------------
select b.*,a.c_id,a.s_score
from score a
join student b on a.s_id = b.s_id
where (select count(1) from score where a.s_score < s_score and a.c_id = c_id) BETWEEN 1 and 2
order by a.c_id asc,a.s_score desc
查询学生平均成绩及其名次
select a.s_id,b.s_name,
@i:= @i+1 as i,
@k:= (case when a.avg = @avg then @k else @i end) as rank,
@avg:= a.avg as avg
from
(select s_id,round(avg(s_score),2)avg from score group by s_id order by avg desc)a join student b on a.s_id = b.s_id,
(select @i:=0,@k:=0,@avg:=0)s
查询各科成绩前三名的记录
注意区别:
where (select count(1) from score where s_score > a.s_score and c_id = a.c_id ) <3
where (select count(1) from score where s_score >= a.s_score and c_id = a.c_id ) <=3
如果第三名和第四名并列,加上等于号的话,并列的情况无法查询出来
select a.*
from score a
where (select count(1) from score where s_score > a.s_score and c_id = a.c_id ) <3
order by a.c_id asc,a.s_score desc
---------------------------------------------------------------------------
select a.s_id,a.c_id,a.s_score from score a
left join score b on a.c_id = b.c_id and a.s_score<b.s_score
group by a.s_id,a.c_id,a.s_score HAVING COUNT(b.s_id)<3
ORDER BY a.c_id,a.s_score DESC
统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
select a.c_id,b.c_name,
(sum(case when s_score >85 and s_score <=100 then 1 else 0 end)) as '[85-100]',
(sum(case when s_score >85 and s_score <=100 then 1 else 0 end))/count(1) as '百分比1',
(sum(case when s_score >70 and s_score <=85 then 1 else 0 end)) as '[70-85]',
(sum(case when s_score >70 and s_score <=85 then 1 else 0 end))/count(1) as '百分比2',
(sum(case when s_score >60 and s_score <=70 then 1 else 0 end)) as '[60-70]',
(sum(case when s_score >60 and s_score <=70 then 1 else 0 end))/count(1) as '百分比3',
(sum(case when s_score >0 and s_score <=60 then 1 else 0 end)) as '[0-60]',
(sum(case when s_score >0 and s_score <=60 then 1 else 0 end))/count(1) as '百分比4'
from score a
join course b on a.c_id = b.c_id
group by a.c_id
按各科成绩进行排序,并显示排名