50道经典sql练习包括建表语句和数据插入及答案

一、建表

student(学生表)

Id学号 (数据如: 001)
stu_name学生姓名(数据如:张三)

course(课程表)

Id课程编号(数据如: 005)
course_name课程名称(数据如:数学)
t_id任课教师编号(数据如: 002)

score(成绩表)

student_id学号(数据如: 001)
course_id课程编号(数据如: 005)
score成绩(数据如: 90)

teacher(教师表)

id教师编号(数据如: 002)
t_name教师姓名(数据如: 李盈)
CREATE TABLE `student` (
	`id` INT(11) NOT NULL AUTO_INCREMENT,
	`stu_name` VARCHAR(16) NOT NULL DEFAULT '0' COMMENT '学生姓名',
	`stu_age` VARCHAR(50) NOT NULL DEFAULT '0' COMMENT '学生年龄',
	`stu_sex` VARCHAR(50) NULL DEFAULT NULL COMMENT '性别,1男,2女',
	PRIMARY KEY (`id`)
)
COMMENT='学生表';

INSERT INTO `student` (`id`, `stu_name`, `stu_age`, `stu_sex`) VALUES (1, '赵雷', '1990-01-01', '男');
INSERT INTO `student` (`id`, `stu_name`, `stu_age`, `stu_sex`) VALUES (2, '钱电', '1990-12-21', '男');
INSERT INTO `student` (`id`, `stu_name`, `stu_age`, `stu_sex`) VALUES (3, '孙风', '1990-05-20', '男');
INSERT INTO `student` (`id`, `stu_name`, `stu_age`, `stu_sex`) VALUES (4, '李云', '1990-08-06', '男');
INSERT INTO `student` (`id`, `stu_name`, `stu_age`, `stu_sex`) VALUES (5, '周梅', '1991-12-01', '女');
INSERT INTO `student` (`id`, `stu_name`, `stu_age`, `stu_sex`) VALUES (6, '吴兰', '1992-03-01', '女');
INSERT INTO `student` (`id`, `stu_name`, `stu_age`, `stu_sex`) VALUES (7, '郑竹', '1989-07-01', '女');
INSERT INTO `student` (`id`, `stu_name`, `stu_age`, `stu_sex`) VALUES (8, '王菊', '1990-01-20', '女');
INSERT INTO `student` (`id`, `stu_name`, `stu_age`, `stu_sex`) VALUES (9, '冯丽', '1990-01-26', '女');
CREATE TABLE `course` (
	`id` INT(11) NOT NULL AUTO_INCREMENT,
	`course_name` VARCHAR(50) NULL DEFAULT '0' COMMENT '课程名称',
	`t_id` INT(11) NULL DEFAULT '0' COMMENT '教师id',
	PRIMARY KEY (`id`)
)
COMMENT='课程表'
COLLATE='utf8_general_ci';

INSERT INTO `course` (`id`, `course_name`, `t_id`) VALUES (1, '语文', 2);
INSERT INTO `course` (`id`, `course_name`, `t_id`) VALUES (2, '数学', 1);
INSERT INTO `course` (`id`, `course_name`, `t_id`) VALUES (3, '英语', 3);
INSERT INTO `course` (`id`, `course_name`, `t_id`) VALUES (4, '物理', 4);
INSERT INTO `course` (`id`, `course_name`, `t_id`) VALUES (5, '生物', 4);

CREATE TABLE `score` (
	`student_id` INT(11) NULL DEFAULT NULL COMMENT '学生id',
	`course_id` INT(11) NULL DEFAULT NULL COMMENT '课程id',
	`score` INT(11) NULL DEFAULT NULL COMMENT '分数',
	UNIQUE INDEX `course_id_student_id` (`student_id`, `course_id`)
)
COMMENT='成绩表'
COLLATE='utf8_general_ci';

INSERT INTO `score` (`student_id`, `course_id`, `score`) VALUES (1, 2, 90);
INSERT INTO `score` (`student_id`, `course_id`, `score`) VALUES (8, 3, 89);
INSERT INTO `score` (`student_id`, `course_id`, `score`) VALUES (5, 2, 87);
INSERT INTO `score` (`student_id`, `course_id`, `score`) VALUES (3, 5, 86);
INSERT INTO `score` (`student_id`, `course_id`, `score`) VALUES (1, 3, 86);
INSERT INTO `score` (`student_id`, `course_id`, `score`) VALUES (3, 1, 81);
INSERT INTO `score` (`student_id`, `course_id`, `score`) VALUES (3, 2, 80);
INSERT INTO `score` (`student_id`, `course_id`, `score`) VALUES (3, 3, 80);
INSERT INTO `score` (`student_id`, `course_id`, `score`) VALUES (2, 3, 80);
INSERT INTO `score` (`student_id`, `course_id`, `score`) VALUES (5, 1, 76);
INSERT INTO `score` (`student_id`, `course_id`, `score`) VALUES (5, 3, 69);
INSERT INTO `score` (`student_id`, `course_id`, `score`) VALUES (2, 2, 60);
INSERT INTO `score` (`student_id`, `course_id`, `score`) VALUES (6, 3, 59);
INSERT INTO `score` (`student_id`, `course_id`, `score`) VALUES (6, 4, 59);
INSERT INTO `score` (`student_id`, `course_id`, `score`) VALUES (4, 3, 59);
INSERT INTO `score` (`student_id`, `course_id`, `score`) VALUES (4, 1, 50);
INSERT INTO `score` (`student_id`, `course_id`, `score`) VALUES (2, 4, 50);
INSERT INTO `score` (`student_id`, `course_id`, `score`) VALUES (6, 1, 31);
INSERT INTO `score` (`student_id`, `course_id`, `score`) VALUES (4, 2, 30);
INSERT INTO `score` (`student_id`, `course_id`, `score`) VALUES (9, 3, 30);
INSERT INTO `score` (`student_id`, `course_id`, `score`) VALUES (7, 3, 30);
INSERT INTO `score` (`student_id`, `course_id`, `score`) VALUES (7, 4, 25);

CREATE TABLE `teacher` (
	`id` INT(11) NOT NULL AUTO_INCREMENT,
	`t_name` VARCHAR(50) NOT NULL COMMENT '教师名称',
	PRIMARY KEY (`id`)
)
COMMENT='教师表'
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=5
;

INSERT INTO `teacher` (`id`, `t_name`) VALUES (1, '张三');
INSERT INTO `teacher` (`id`, `t_name`) VALUES (2, '李四');
INSERT INTO `teacher` (`id`, `t_name`) VALUES (3, '王五');
INSERT INTO `teacher` (`id`, `t_name`) VALUES (4, '叶平');

二、sql练习语句

1、查询“001”课程比“002”课程成绩高的所有学生的学号;

2、查询平均成绩大于60分的同学的学号和平均成绩;

3、查询所有同学的学号、姓名、选课数、总成绩;

4、查询姓“李”的老师的个数;

5、查询没学过“叶平”老师课的同学的学号、姓名;

6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;

7、查询学过“叶平”老师所教的所有课的同学的学号、姓名;

8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;

9、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩 – (包括有成绩的和无成绩的)

10、查询没有学全所有课的同学的学号、姓名;

11、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名;

12、查询和"01"号的同学学习的课程完全相同的其他同学的信息

13、把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;

14、查询和“2”号的同学学习的课程完全相同的其他同学学号和姓名;

15、删除学习“叶平”老师课的SC表记录;

16、向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号“003”课程的同学学号、2号课的平均成绩;

18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分

19、按各科平均成绩从低到高和及格率的百分数从高到低顺序

21、查询不同老师所教不同课程平均分从高到低显示

23、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]

24、查询学生平均成绩及其名次

25、查询各科成绩前三名的记录:(不考虑成绩并列情况)

26、查询每门课程被选修的学生数

27、查询出只选修了一门课程的全部学生的学号和姓名

28、查询男生、女生人数

29、查询姓“王”的学生名单

32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列

33、查询平均成绩大于85的所有学生的学号、姓名和平均成绩

34、查询课程名称为“语文”,且分数低于60的学生姓名和分数

35、查询所有学生的选课情况;

36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;

37、查询不及格(<60)的课程,并按课程号从大到小排列

38、查询课程编号为3且课程成绩在80分以上的学生的学号和姓名;

39、求选了课程的学生人数

40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名,课程及其成绩

41、查询各个课程及相应的选修人数

42、查询不同课程成绩相同的学生的学号、课程号、学生成绩

44、统计每门课程的学生选修人数(超过3人的课程才统计)。要求输出课程号和选修人数,
查询结果按人数降序排列,若人数相同,按课程号升序排列

45、检索至少选修两门课程的学生学号

46、查询全部学生都选修的课程的课程号和课程名

47、查询没学过“叶平”老师讲授的任一门课程的学生姓名

48、查询两门以上不及格课程的同学的学号及其平均成绩

49、检索“4”课程分数小于60,按分数降序排列的同学学号

50、删除“9”同学的“4”课程的成绩

三、练习答案

1、查询“001”课程比“002”课程成绩高的所有学生的学号;

select a.* from (select * from score where course_id=1) a join
 (select * from score where course_id=2) b on a.student_id=
b.student_id where a.score>b.score;

2、查询平均成绩大于60分的同学的学号和平均成绩;

select student_id , truncate(avg(score),2) a  from score group by student_id having a>60;

3、查询所有同学的学号、姓名、选课数、总成绩;

 select s.id,s.stu_name,count(1),sum(c.score) from student s join score c  
 on s.id=c.student_id group by c.student_id;

4、查询姓“李”的老师的个数;

 select count(1) from teacher where t_name like '李%';

5、查询没学过“叶平”老师课的同学的学号、姓名;

   select s.id,s.stu_name  from student s where s.id not in (select s.id from student s join score c on
s.id=c.student_id join course cou on  c.course_id= cou.id join teacher
tea on cou.t_id = tea.id where tea.t_name = '叶平' 
);

6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;

select s.id ,s.stu_name from student s join score a on s.id = a.student_id and a.course_id=1
join score b on s.id = b.student_id and a.student_id=b.student_id and b.course_id=2 ;

7、查询学过“叶平”老师所教的所有课的同学的学号、姓名;

select s.id,s.stu_name from student s join score c on
s.id=c.student_id join course cou on  c.course_id= cou.id join teacher
tea on cou.t_id = tea.id where tea.t_name = '叶平';

8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;

select s.id,s.stu_name,a.*,b.* from student s  join (select * from score where course_id=1)
a on s.id=a.student_id join (select * from score where course_id=2) b on s.id=b.student_id
where b.score<a.score;

9、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
– (包括有成绩的和无成绩的)

select s.id,s.stu_name,round(avg(sco.score),2) a from student s left join score sco on s.id=sco.student_id
group by s.id having a<60 or a is null;

select s.id,s.stu_name,round(avg(sco.score),2) a from student s  join score sco on s.id=sco.student_id
group by s.id having a<60
union
select s.id,s.stu_name,0 from student s where s.id not in(select distinct sco.student_id from score sco);

10、查询没有学全所有课的同学的学号、姓名;

select id,stu_name from student where id not in(
select s.id from student s join (select * from score where course_id=1) a on s.id = a.student_id
join (select * from score where course_id=2) b on s.id = b.student_id
join (select * from score where course_id=3) c on s.id = c.student_id
join (select * from score where course_id=4) d on s.id = d.student_id);

11、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名;

select s.id,s.stu_name from student s join score sco on s.id = sco.student_id where sco.course_id
in(select course_id from score where student_id=1) group by s.id ;

12、查询和"01"号的同学学习的课程完全相同的其他同学的信息

select s.* from student s join score sco on s.id = sco.student_id where sco.course_id
in(select course_id from score where student_id=1) group by
s.id having count(1)=(select count(1) from score where student_id=1);

13、把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;

update score s join (
select sc.course_id ,round(avg(sc.score),2) avg from score sc join course c on sc.course_id=c.id
join teacher t on c.t_id = t.id where t.t_name= '叶平'  group by sc.course_id
) res
on res.course_id = s.course_id set s.score = res.avg where res.course_id = s.course_id;

14、查询和“2”号的同学学习的课程完全相同的其他同学学号和姓名;

   select s.id,s.stu_name from student s join score sc on s.id = sc.student_id where sc.course_id
in (select sc.course_id from student s join score sc on s.id = sc.student_id where s.id=2)
group by s.id
having count(1) = (select count(1) from student s join score sc on s.id = sc.student_id where s.id=2)

15、删除学习“叶平”老师课的SC表记录;

delete sc from score sc where
sc.course_id in (
select cou.id from course cou join teacher t on cou.t_id = t.id where t.t_name ='叶平')

16、向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号“003”课程的同学学号、2号课的平均成绩;

Insert score select s.id,'6',(Select avg(score)
   from score where course_id='2') from student s where s.id
                  not in (Select student_id from score where course_id='3');

18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分

select sc.course_id,max(score) 最高分,min(score) 最低分 from score sc group by sc.course_id;

19、按各科平均成绩从低到高和及格率的百分数从高到低顺序

  方法一:
select q.course_id,q.avg,concat(w.z,'%') 百分比 from (select sc.course_id , round(avg(sc.score),2)
 avg from score sc group by sc.course_id) q join
(select round(a.num/b.num*100,2) z,a.course_id from (select count(1) num,course_id from score
where score>=60 group by course_id) a
join (select count(1) num,course_id from score  group by course_id) b on a.course_id = b.course_id) w
on q.course_id = w.course_id group by q.avg asc,w.z desc

方式二:
select sc.course_id,round(avg(sc.score),2) avg,
concat(round(sum(case when sc.score>=60 then 1 else 0 end)/count(1),2)*100 ,'%') 百分数
from score sc group by sc.course_id order by
 avg asc,round(sum(case when sc.score>=60 then 1 else 0 end)/count(1),2) desc;

21、查询不同老师所教不同课程平均分从高到低显示

select t.t_name,c.course_name,round(avg(sc.score),2) avg from teacher t 
join course c on t.id = c.t_id join score sc on c.id = sc.course_id
group by t.id,sc.course_id order by avg desc

23、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]

select c.course_name,c.id,round(avg(sc.score),2) avg,count(1) 人数,
sum(case when sc.score <=100 and sc.score>=85 then 1 else 0 end) '[100-85]',
sum(case when sc.score<85 and sc.score>=70 then 1 else 0 end) '[85-70]',
sum(case when sc.score<70 and sc.score>=60 then 1 else 0 end) '[70-60]',
sum(case when sc.score<60 then 1 else 0 end) '[<60]'
from course c join score sc on c.id = sc.course_id
group by c.course_name

24、查询学生平均成绩及其名次

解题思路:设置一个字段从0开始自增,注意把查询学生平均成绩并排序作为一个子查询,
否则出现排名和平均成绩不符的问题,select 执行顺序form ,select,order by
 方式一:
set @n :=0;
select (@n :=@n+1) 名次,a.stu_name,a.平均分 from (select s.stu_name, round(avg(sc.score),2) 平均分 
from student s left join score sc
on s.id=sc.student_id group by s.id ) a order by a.平均分 desc 

解题思路:利用两个表的平均成绩统计排名,如果没有比这个平均成绩大的,则次数就是0,
再加1 ,就是第一名,如果有一个比次平均成绩大的,则次数就是1,再加1,就是第二名,一次类推
方式二:
select 1+(
select count(1) from (select round(avg(sco.score),2) avg1 from score sco group by sco.student_id )
a where a.avg1>b.avg2
) 名次,b.stu_name,b.avg2
from
(
select s.stu_name,round(avg(sc.score),2) avg2 from student s join score sc on s.id = sc.student_id
group by s.id
)b order by b.avg2 desc;

25、查询各科成绩前三名的记录:(不考虑成绩并列情况)

解题思路:
使score通过课程id自关联,并且统计a表的成绩小于b表的成绩,通过学生id和课程id进行分组,
通过having 字段过滤a的成绩小于b的成绩的次数条数出现的次数少于3次,就是在头3名
注意:使用左外关联,在on后面使用and a.score<b.score
的语句进行筛选,不符合条件的也会显示,放到where后面后直接过滤到,导致统计的数据缺失(如最大的成绩放到where后面不显示)
方式一:
select a.student_id,a.course_id,a.score from score a
left join score b on a.course_id = b.course_id and a.score<b.score
group by a.student_id,a.course_id
HAVING COUNT(b.student_id)<3
            ORDER BY a.course_id,a.score DESC
            
方式二:
select a.student_id,a.course_id,a.score from score a
left join score b on a.course_id = b.course_id and a.score<b.score
group by a.student_id,a.course_id
HAVING COUNT(a.student_id)<3
            ORDER BY a.course_id,a.score DESC

26、查询每门课程被选修的学生数

select sc.course_id,count(1) from score sc group by sc.course_id

27、查询出只选修了一门课程的全部学生的学号和姓名

select s.id,s.stu_name from score sc join student s on sc.student_id = s.id
group by sc.student_id having count(1) = 1;

28、查询男生、女生人数

   select sum(case when s.stu_sex='男' then 1 else 0 end  ) 男生人数,
sum(case when s.stu_sex='女' then 1 else 0 end) 女生人数
from student s

29、查询姓“王”的学生名单

 select * from student s where s.stu_name like "王%"

32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列

select sc.course_id,round(avg(sc.score),2) avg from score sc group by sc.course_id
order by avg asc,sc.course_id desc

33、查询平均成绩大于85的所有学生的学号、姓名和平均成绩

select s.id,s.stu_name ,round(avg(sc.score),2) avg from score sc join student s on sc.student_id = s.id
group by sc.student_id having avg>85;

34、查询课程名称为“语文”,且分数低于60的学生姓名和分数

    select s.stu_name,sc.score,c.course_name from score sc join student s on sc.student_id=s.id join
course c on sc.course_id = c.id where c.course_name = '语文' and sc.score<60;

35、查询所有学生的选课情况;

    select s.stu_name,c.course_name from student s join score sc on s.id = sc.student_id join
course c on sc.course_id = c.id;

36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;

select s.stu_name,c.course_name,sc.score from score sc join student s on sc.student_id = s.id join
course c on sc.course_id= c.id where sc.score>70;

37、查询不及格(<60)的课程,并按课程号从大到小排列

select * from score sc join course c on sc.course_id = c.id
where sc.score<60 order by sc.course_id desc;

38、查询课程编号为3且课程成绩在80分以上的学生的学号和姓名;

select s.id,s.stu_name,sc.score,sc.course_id from score sc join student s on sc.student_id = s.id
where sc.course_id=3 and sc.score>80

39、求选了课程的学生人数

select count(a.score) from (
select sc.score from score sc  join student s on sc.student_id = s.id group by s.id
) a ;

40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名,课程及其成绩

select s.stu_name,a.course_name,max(b.score) from score b join
(select c.id,c.course_name from score sc join course c on sc.course_id = c.id  join teacher t on c.t_id = t.id
where t.t_name = '叶平' group by c.id)
a on b.course_id = a.id
join student s on b.student_id = s.id
group by a.id

41、查询各个课程及相应的选修人数

elect sc.course_id,count(sc.student_id) from score sc group by sc.course_id;

42、查询不同课程成绩相同的学生的学号、课程号、学生成绩

select s1.stu_name,a.course_id,a.score,s2.stu_name,b.course_id,b.score
from score a join  score b on a.score = b.score
join student s1 on a.student_id = s1.id join student s2 on b.student_id = s2.id
where a.course_id != b.course_id

44、统计每门课程的学生选修人数(超过3人的课程才统计)。要求输出课程号和选修人数,
查询结果按人数降序排列,若人数相同,按课程号升序排列

select sc.course_id,count(1) from score sc group by sc.course_id having count(1)>3
order by count(1) desc ,sc.course_id asc;

45、检索至少选修两门课程的学生学号

fselect sc.student_id,count(1) from score sc group by sc.course_id
having count(1)>1;

46、查询全部学生都选修的课程的课程号和课程名

select sc.course_id, count(1) from score sc group by sc.course_id
having count(1) = (
select count(1) from student
)

47、查询没学过“叶平”老师讲授的任一门课程的学生姓名

select s.stu_name,sc.course_id from student s join score sc on s.id = sc.student_id
where sc.course_id not in (
select  c.id from  course c join teacher t on c.t_id = t.id where t.t_name='叶平'
)
group by s.id

48、查询两门以上不及格课程的同学的学号及其平均成绩

select sc.student_id,count(1) from score sc where sc.score<60
group by sc.student_id having count(1) >= 2

49、检索“4”课程分数小于60,按分数降序排列的同学学号

select sc.student_id,sc.score from score sc where sc.score<60 and sc.course_id=4
order by sc.score desc;

50、删除“9”同学的“4”课程的成绩

delete sc from score sc where sc.student_id=9 and sc.course_id=4;

四、有更优解或错误欢迎大家及时指出,谢谢!!!

  • 4
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
资源包括:19SQL语句查询题目及答案、建表SQL语句、题目相关的表截图。答案中除了包含intersect、except关键词的答案,其余都在MySQL上跑过,确保运行无误(MySQL不支持intersect、except关键词),因为脑细胞死得有些多,资源分不少请大家见谅。 题目如下: Q:Find all customers who have both an account and a loan at the Perryridge branch.(ps:MySQL不支持intersect运算符) Q:Find the number of depositors for each branch. Q:Find the names of all branches where the average account balance is more than $1,200. Q:Find the names of all branches that have greater assets than all branches located in Brooklyn. Q:Find all accounts with the maximum balance. Q:Find all branches that have greater assets than some branch located in Brooklyn. Q:Find all customers who have both an account and a loan at the bank. Q:Find all customers who have accounts at all branches located in Brooklyn. Q:Find the average account balance at the Perryridge branch. Q:Find the number of tuples in the customer relation. Q:Find the number of depositors in the bank. Q:Find the number of depositors for each branch. Q:Find all customers who have a loan at the bank but do not have an account at the bank. Q:Find all branches where the total account deposit is greater than the average of the total account deposits at all branches. Q:Find all customers who have both an account and a loan at the bank. Q:Find all customers who have at most one account at the Perryridge branch. Q:Provide as a gift for all loan customers of the Perryridge branch, a $200 savings account. Let the loan number serve as the account number for the new savings account. Q:Increase all accounts with balances over $10,000 by 6%, all other accounts receiver 5%.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值