MySQL语句练习题,练习题的知识辐射范围很广,也很有难度。非常适合新手练习。
–建表
–学生表
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);
笔试题
1、查询课程编号为“01”的课程比“02”的课程成绩高的所有学生的学号。
要点:联结
难度:*****
– 写法一:
select a.s_id
from Score a join Score b on a.s_id = b.s_id and a.s_score >b.s_score
where a.c_id = ‘01’ and b.c_id= ‘02’;
– 写法二:
select a.s_id
from (select * from Score where c_id = ‘01’) as a
join (select * from Score where c_id=‘02’) as b
on a.s_id = b.s_id
where a.s_score > b.s_score;
2、查询平均成绩大于60分的学生的学号和平均成绩
难度:***
select s_id, avg(s_score)
from Score
group by s_id
having avg(s_score)>60;
3、查询所有学生的学号、姓名、选课数、总成绩
要点:内联结
难度:****
select Score.s_id AS ‘学号’, s_name AS ‘姓名’,count(c_id)AS ‘选课数’,sum(s_score)AS ‘总成绩’
from Score join Student on Score.s_id = Student.s_id
group by Score.s_id,s_name;
4、查询姓“张”的老师的个数
难度:**
select count(t_id)
from Teacher
where t_name like ‘张%’;
5.查询没学过“张三”老师课的学生的学号、姓名(重点)
要点: 三表联结
难度:*****
select s_id, s_name
from Student
where s_id not in
(select s_id from Score join Course on Score.c_id = Course.c_id
join Teacher on Course.t_id = Teacher.t_id
where t_name = ‘张三’);
6、查询学过“张三”老师所教的所有课的同学的学号、姓名
要点: 同上第5题
难度:*****
写法一:
select s_id, s_name
from Student
where s_id in
(select s_id from Score join Course on Score.c_id = Course.c_id
join Teacher on Course.t_id = Teacher.t_id
where t_name = ‘张三’);
写法二:
select Student.s_id, s_name
from Student JOIN Score on Student.s_id = Score.s_id
JOIN Course on Score.c_id = Course.c_id
JOIN Teacher on Teacher.t_id = Course.t_id
where t_name=‘张三’;
7、查询学过编号为“01”的课程并且也学过编号为“02”的课程的学生的学号、姓名
难度:*****
–写法一:
select s_id, s_name
from Student
where s_id in
(select a.s_id from
(select s_id from Score where c_id = ‘01’) as a
join (select s_id from Score where c_id =‘02’) as b
on a.s_id= b.s_id);
–写法二:
select s_id,s_name
from Student
where s_id in
(select s_id from Score where c_id = ‘01’)
AND s_id in
(select s_id from Score where c_id = ‘02’)
–写法三:
select a.s_id,a.s_name
from Student a JOIN Score b ON a.s_id=b.s_id
JOIN Score c ON a.s_id=c.s_id
where b.c_id=‘01’ and c.c_id=‘02’
and b.s_id=c.s_id ;
8、查询课程编号为“02”的总成绩
要点:聚合
难度:**
select sum(s_score)
from Score
where c_id = ‘02’;
9、查询所有课程成绩小于60分的学生的学号、姓名
难度:*****
select s_id, s_name
from Student
where s_id NOT IN (SELECT s_id FROM Score where s_score >=60)
10、查询没有学全所有课的学生的学号、姓名
难度:****
写法一:
select Student.s_id, Student.s_name
from Student join Score on Score.s_id = Student.s_id
group by s_id, s_name
having count(Score.c_id) < (select count(c_id) from Course);
写法二:
select s_id, s_name
from Student
where s_id IN (SELECT s_id FROM Score group by s_id
having count(c_id)<(select count(c_id) from Course))
小于还可以是不等于
11、查询至少有一门课与学号为“01”的学生所学课程相同的学生的学号和姓名
要点:联结
难度:*****
select distinct a.s_id, a.s_name
from Student a join Score b on a.s_id= b.s_id
where c_id in
(select c_id from Score where s_id = ‘01’)
and a.s_id<>‘01’;
陷阱两处你知道吗
12、查询和“01”号同学所学课程完全相同的其他同学的学号
难度:*****
select s_id
from Score
where c_id in
(select c_id from Score where s_id=‘01’)
and s_id <> ‘01’
group by s_id
having count(c_id)=(select count(c_id) from Score where s_id=‘01’);
13、把“SCORE”表中“张三”老师教的课的成绩都更改为此课程的平均成绩
难度:*****
update Score as a join
(select avg(s_score) as t, Score.c_id from Score
join Course on Score.c_id= Course.c_id
join Teacher on Teacher.t_id= Course.t_id
where t_name =‘张三’ group by c_id) as b#张三老师教的课与平均分
on a.c_id= b.c_id
set a.s_score= b.t;
14、查询和“02”号的同学学习的课程完全相同的其他同学学号和姓名(同12题)
难度:*****
select a.s_id,a.s_name
from Student a join Score b on a.s_id=b.s_id
where c_id in (select c_id from Score where s_id=‘02’)
and a.s_id <> ‘02’
group by a.s_id
having count(c_id)=(select count(c_id) from Score where s_id=‘02’);
15、删除学习“张三”老师课的SC表记录
难度:****8
delete from Score
where c_id in
(select c_id from Course join Teacher on Course.t_id=Teacher.t_id
where t_name =‘张三’);
17、按平均成绩从高到低显示所有学生的“数据库”(c_id=‘04’)、“企业管理”(c_id=‘01’)、“英语”(c_id=‘06’)三门的课程成绩,按如下形式显示:学生ID,数据库,企业管理,英语,有效课程数,有效平均分
难度:*****
select s_id as ‘学生ID’,
(case when c_id=‘04’ then s_score else NULL end) as ‘数据库’,
(case when c_id=‘01’ then s_score else NULL end) as ‘企业管理’,
(case when c_id=‘06’ then s_score else NULL end) as ‘英语’,
count(c_id) as 有效课程数,
avg(s_score) as 有效平均分
from Score
group by s_id
order by avg(s_score) DESC;
18、查询各科成绩最高和最低的分: 以如下的形式显示:课程ID,最高分,最低分
难度:***
select c_id as 课程ID,
max(s_score) as 最高分,
min(s_score) as 最低分
from Score
group by c_id;
19、按各科平均成绩从低到高和及格率的百分数从高到低排列,以如下形式显示:课程号,课程名,平均成绩,及格百分数
要点:关联查询
难度:*****
select a.c_id as ‘课程号’,c_name as ‘课程名’,avg(s_score) as ‘平均成绩’,
concat((select count(b.s_score) from Score b where b.s_score>=60 and a.c_id=b.c_id)/
(select count(b.s_score) from Score b where a.c_id=b.c_id)*100,’%’) as ‘及格百分数’
from Score a join Course c
on a.c_id=c.c_id
group by a.c_id,c_name
order by 平均成绩, 及格百分数 DESC;
20、查询如下课程平均成绩和及格率的百分数(用1行显示),其中企业管理为001,马克思为002,UML为003,数据库为004
这道题有问题不用做
21、查询不同老师所教不同课程平均分从高到低显示
要点:联结
难度:****
select Teacher.t_id,Teacher.t_name,Course.c_name,avg(Score.s_score)
from Teacher join Course on Teacher.t_id=Course.t_id
join Score on Course.c_id=Score.c_id
group by t_id,t_name,c_name
order by avg(Score.s_score) DESC;
22、查询如下课程成绩第3名到第6名的学生成绩单,其中企业管理为001,马克思为002,UML为003,数据库为004,以如下形式显示:
学生ID学生姓名企业管理马克思UML数据库平均成绩
题目有问题,这道题不做
23、使用分段[100-85],[85-70],[70-60],[<60]来统计各科成绩,分别统计各分数段人数:课程ID和课程名称
要点:case
难度:*****
select a.c_id AS ‘课程ID’,c_name ‘课程名称’,
sum(case when s_score between 85 and 100 then 1 else 0 end) as ‘[100-85]’,
sum(case when s_score >=70 and s_score<85 then 1 else 0 end) as ‘[85-70]’,
sum(case when s_score>=60 and s_score<70 then 1 else 0 end) as ‘[70-60]’,
sum(case when s_score<60 then 1 else 0 end) as ‘[<60]’
from Score a join Course b on a.c_id=b.c_id
group by a.c_id,c_name;
24、查询学生平均成绩及其名次
要点:算出在所有同学中有几个同学的平均分高于某个ID,然后+1,就是名次
难度:*****
SELECT s_id as ‘学号’,平均成绩,
(SELECT COUNT(*) FROM(SELECT s_id,AVG(s_score)AS ‘平均成绩’
FROM Score GROUP BY s_id)AS b
WHERE b.平均成绩>a.平均成绩)+1 as RANK
FROM (select s_id,avg(S_score) as 平均成绩 from Score group by s_id)AS a
order by 平均成绩 desc;
25、查询各科成绩前三名的记录(不考虑成绩并列情况)
要点:超过当前ID的人最多2人
难度:*****
这里笔者大部分满足了题意,但是没有满足“不考虑成绩并列情况”,欢迎给出建议!
SELECT c_id,s_id,s_score FROM Score a
WHERE (SELECT COUNT(*) FROM Score b WHERE a.c_id=b.c_id AND a.s_score
26、查询每门课程被选修的学生数
难度:**
select c_id, count(s_id)
from Score
group by c_id;
27、查询出只选修了两门课程的全部学生的学号和姓名
难度:***
写法一:
select Student.s_id,s_name, count(c_id)
from Student join Score on Student.s_id= Score.s_id
group by s_id,s_name
having count(c_id)=2;
写法二:
select s_id,s_name
from Student
WHERE s_id IN (SELECT s_id FROM Score GROUP BY s_id having count(c_id)=2)
28、查询男生、女生人数
难度:***
select s_sex, count(*) from Student group by s_sex;
29、查询名字中含有“风”字的学生信息
难度:**
select *
from Student
where s_name like ‘%风%’;
30、查询同名同姓学生名单并统计同名人数
难度:***
写法一:
select a.s_id, a.s_name,a.s_birth,a.s_sex
from Student AS a JOIN Student AS b
ON a.s_name=b.s_name
WHERE a.s_id<>b.s_id
写法二:
select s_name,count()
from Student
group by s_name
having count()>1
31、1990年出生的学生名单(注:Student表中s_birth列的类型是datetime)
难度:***
– 方法一
select s_name
from Student
where s_birth like ‘1990%’;
– 方法二
select s_name
from Student
where year(s_birth)=1990;
32、查询平均成绩大于85的所有学生的学号、姓名和平均成绩
难度:***
select a.s_id as 学号, s_name as 姓名, avg(b.s_score) as 平均分
from Student a join Score b on a.s_id=b.s_id
group by a.s_id,s_name
having avg(b.s_score) >85;
33、查询每门课程的平均成绩,结果按平均成绩升序排序,平均成绩相同时,按课程号降序排列
难度:***
select c_id, avg(s_score)
from Score
group by c_id
order by avg(s_score),c_id DESC;
34、查询课程名称为“数学”且分数低于60的学生姓名和分数
难度:***
select s_name as 学生姓名, s_score as 分数
from Student a join Score b on a.s_id=b.s_id
join Course c on c.c_id=b.c_id
where c_name=‘数学’
and s_score <60;
35、查询所有学生的选课情况
难度:***
select a.s_id as 学号, s_name as 姓名 , c.c_id as 课程号,c_name as 课程名称
from Student a join Score b on a.s_id=b.s_id
join Course c on b.c_id=c.c_id
36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数
难度:***
select s_name as 姓名,c_name as 课程名称,s_score as 分数
from Student a join Score b on at.s_id=b.s_id
join Course c on b.c_id=c.c_id
where s_score >70
37、查询不及格的课程并按课程号从大到小排列
难度:***
select c_id as 课程号 ,s_score as 分数
from Score
where s_score<60
order by c_id;
38、查询课程编号为03且课程成绩在80分以上的学生的学号和姓名
难度:****
写法一:
select a.s_id as 学号 ,s_name as 姓名
from Student a join Score b on a.s_id=b.s_id
where c_id=‘03’
and s_score>80;
写法二:
select s_id as 学号 ,s_name as 姓名
from Student
where s_id IN(SELECT s_id FROM Score WHERE c_id=‘03’
and s_score>80);
39、查询选了课程的学生人数
难度:**
select count(DISTINCT s_id) as 学生人数 from Score
40、查询选修“张三”老师所授课程的学生中成绩最高的学生姓名及其成绩
难度:*****
select s_name as 学生姓名, s_score as 成绩
from Student a join Score b on a.s_id=b.s_id
join Course c on c.c_id=b.c_id
join Teacher d on d.t_id=c.t_id
where t_name=‘张三’
order by s_score DESC
limit 1;
41、查询各个课程及相应的选修人数
难度:**
select a.c_id as 课程号,c_name as 课程名称, count(s_id)as 选修人数
from Score a join Course b on a.c_id=b.c_id
group by a.c_id;
42、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
难度:*****
select distinct a.s_id as 学生编号 ,a.c_id as 课程编号,a.s_score as 学生成绩
from Score a join Score b
on a.s_id=b.s_id and a.c_id<> b.c_id
where a.s_score=b.s_score;
43、查询每门课程成绩最好的前两名
同25题
难度:*****
SELECT a.c_id as 课程号,c_name as 课程名称,s_name as 姓名,b.s_score as chengji FROM
Course a join Score b on a.c_id=b.c_id
join Student c on c.s_id=b.s_id
WHERE (SELECT COUNT(*) FROM Score d WHERE a.c_id=d.c_id AND b.s_score
44、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排序,若人数相同,按课程号升序排序
要点:分组,排序
难度:***
select c_id as ‘课程号’, count(s_id) as ‘选修人数’
from Score
group by c_id
having count(s_id)>5
order by count(s_id) DESC,c_id;
45、查询至少选修两门课程的学生学号
难度:**
select s_id
from Score
group by s_id
having count(c_id)>=2;
46、查询选修了全部课程的学生信息
难度:****
写法一:
select a.s_id, s_name,s_birth, s_sex
from Student a join Score b on a.s_id=b.s_id
group by a.s_id
having count(Score.c_id)=(select count(distinct c_id) from Score);
写法二:
select * from Student
where s_id in(select s_id from Score GROUP BY s_id
having count(c_id)=(select count(distinct c_id) from Score))
47、查询没学过“张三”老师讲授的任一门课程的学生姓名
要点:三表联结
难度:****
select s_name from Student
where s_id not in
(select s_id from Score join Course on Score.c_id=Course.c_id
join Teacher on Teacher.t_id=Course.t_id
where t_name = ‘张三’);
48、查询两门以上不及格课程的同学的学号及其平均成绩
要点:执行顺序
难度:****
select s_id as 学号, avg(s_score)as 平均成绩
from Score
where s_score <60
group by s_id
having count(c_id)>=2;
49、检索课程编号为“04”且分数小于60的学生学号,结果按按分数降序排列
难度:**
select s_id
from Score
where c_id=‘04’
and s_score <60
order by s_score DESC;
50、删除学生编号为“02”的课程编号为“01”的成绩
难度:**
delete from Score
where s_id=‘02’
and c_id=‘01’;