当前位置:   article > 正文

50道经典sql练习包括建表语句和数据插入及答案_查询全部学生选修的课程和课程号和课程名 表结构: course表: sc表: student表: 表

查询全部学生选修的课程和课程号和课程名 表结构: course表: sc表: student表: 表

一、建表

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', '女');
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
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);

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
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);

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
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, '叶平');

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

二、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;
  • 1
  • 2
  • 3

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

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

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;
  • 1
  • 2

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

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

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 = '叶平' 
);
  • 1
  • 2
  • 3
  • 4

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 ;
  • 1
  • 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 = '叶平';
  • 1
  • 2
  • 3

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;
  • 1
  • 2
  • 3

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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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);
  • 1
  • 2
  • 3
  • 4
  • 5

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 ;
  • 1
  • 2

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);
  • 1
  • 2
  • 3

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;
  • 1
  • 2
  • 3
  • 4
  • 5

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)
  • 1
  • 2
  • 3
  • 4

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 ='叶平')
  • 1
  • 2
  • 3

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');
  • 1
  • 2
  • 3

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

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

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;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

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
  • 1
  • 2
  • 3

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

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

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

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;
  • 1
  • 2

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
  • 1
  • 2
  • 3

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

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

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
  • 1
  • 2

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;
  • 1
  • 2

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;
  • 1
  • 2

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;
  • 1
  • 2

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;
  • 1
  • 2

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;
  • 1
  • 2

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
  • 1
  • 2

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 ;
  • 1
  • 2
  • 3

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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

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

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
  • 1
  • 2
  • 3
  • 4

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;
  • 1
  • 2

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

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

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

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

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
  • 1
  • 2
  • 3
  • 4
  • 5

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

select sc.student_id,count(1) from score sc where sc.score<60
group by sc.student_id having count(1) >= 2
  • 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;
  • 1
  • 2

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

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/955529
推荐阅读
相关标签
  

闽ICP备14008679号