50道数据库SQL练习题(深入理解各关键字的作用)

article/2025/10/9 3:17:00

目录

  • 表结构
  • 创建表
  • 练习题
    • 1、查询“001”课程比“002”课程成绩高的所有学生的学号
    • 2、查询所有同学的学号、姓名、选课数、总成绩
    • 3、查询平均成绩大于60分的同学的 学号和平均成绩
    • 4、查询姓“葛”的老师的个数
    • 5、查询没学过“五木”老师课的同学的学号、姓名
    • 6、查询学过“101”并且也学过编号“102”课程的同学的学号、姓名
    • 7、查询学过“五木”老师所教的所有课的同学的学号、姓名
    • 8、查询课程编号“102”的成绩比课程编号“101”课程低的所有同学的学号、姓名
    • 9、查询所有课程成绩小于60分的同学的学号、姓名
    • 10、查询没有学全所有课的同学的学号、姓名
    • 11、查询至少有一门课与学号为“1”的同学所学相同的同学的学号和姓名
    • 12、查询至少学过学号为“001”同学所有课的其他同学学号和姓名
    • 13、把“成绩”表中“五木”老师教的课的成绩都更改为此课程的平均成绩
    • 14、查询和“1002”号的同学学习的课程完全相同的其他同学学号和姓名
    • 15、删除学习“五木”老师课的SC表记录
    • 16、向成绩表中插入一些记录,这些记录要求符合以下条件:没有上过编号“103”课程的同学学号、2号课的平均成绩
    • 17、按平均成绩从高到低显示所有学生的“数据库”、“Python”、两门的课程成绩,(数据库,Linux)按如下形式显示: 学生ID,数据库,Python,有效课程数,有效平均分
    • 18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
    • 19、按各科平均成绩从低到高和及格率的百分数从高到低顺序
    • 20、查询如下课程平均成绩和及格率的百分数(用"1行"显示):(001),(002),(003),(004)
    • 21、查询不同老师所教不同课程平均分从高到低显示
    • 22.查询如下课程成绩第 3 名到第 6 名的学生成绩单:1号课程,2号课程,3号课程,4号课程 [学生ID],[学生姓名],1号课程,2号课程,3号课程,4号课程,平均成绩
    • 23、统计各分数段人数显示结果如下:课程ID,课程名,<60人数,60<=x<80,>=80
    • 24、查询学生平均成绩及其名次
    • 25、查询各科成绩前三名的记录:(不考虑成绩并列情况)
    • 26、查询每门课程被选修的学生数
    • 27、查询出只选修了一门课程的全部学生的学号和姓名
    • 28、查询男生、女生人数
    • 29、查询姓“张”的学生名单
    • 30、查询同名同姓学生名单,并统计同名人数
    • 30、1995年出生的学生名单(注:Student表中Sage列的类型是datetime)
    • 32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
    • 33、查询平均成绩大于85的所有学生的学号、姓名和平均成绩
    • 34、查询课程名称为“数据库”,且分数低于60的学生姓名和分数
    • 35、查询所有学生的选课情况(学生姓名、课程名称)
    • 36、查询任何一门课程成绩全部都在70分以上的姓名、课程名称和分数
    • 37、查询平均成绩不及格的课程,并按课程号从大到小排列
    • 38、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名
    • 39、求选了课程的学生人数
    • 40、查询选修“五木”老师所授课程中,每门成绩最高的学生姓名及其成绩。按如下个格式输出 课程名称 学生姓名 成绩(假设每门课最高分只有一个人)
    • 41、查询各个课程及相应的选修人数
    • 42、查询不同课程成绩相同的学生的学号、课程号、学生成绩
    • 43、查询每门功成绩最好的前两名
    • 44、统计每门课程的学生选修人数(超过10人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,查询结果按人数降序排列,若人数相同,按课程号升序排列
    • 45、检索至少选修两门课程的学生学号
    • 46、查询全部学生都选修的课程的课程号和课程名
    • 47、查询没学过“五木”老师讲授的任一门课程的学生姓名
    • 48、查询两门以上不及格课程的同学的学号及其平均成绩 (小于70)
    • 49、检索“004”课程分数小于60,按分数降序排列的同学学号
    • 50、删除“004”同学的“001”课程的成绩

表结构

设有学生表(T_Student)、教师表(T_teacher)、课程表(T_Course)和成绩表(T_score)。
表结构及其关系如下UML图所示。
在这里插入图片描述

创建表

下面列出了Oracle数据库表的创建代码。我并没有设主键。

create table T_Student
(Student_ID int, Student_name varchar2(32),Student_age int,Student_sex char(1)
);
//学生表由于涉及到个人信息,这里不做展示。请自行补全。
create table T_teacher
(t_id int,t_name varchar2(20)
);
insert into T_Teacher values(101,'葛老师');
insert into T_Teacher values(102,'五木老师');
insert into T_Teacher values(103,'华仔老师');
insert into T_Teacher values(104,'罗老师');
insert into T_Teacher values(105,'黄老师');
create table T_Course
(Course_ID int,Course_name varchar2(30), T_ID int 
);
//某一个老师教的课程可以是多种的。
insert into T_Course values(101,'数据库',101);
insert into T_Course values(102,'QT',102);
insert into T_Course values(103,'C++',103);
insert into T_Course values(104,'C语言',104);
insert into T_Course values(105,'linux',105);create table T_score
(Student_ID int,  Course_ID int,score number(5,2)
);
insert into T_score values(1,101,80);insert into T_score values(9,101,76);
insert into T_score values(9,102,44);
insert into T_score values(9,104,83);
insert into T_score values(9,105,70);insert into T_score values(11,101,83);
insert into T_score values(11,102,75);
insert into T_score values(11,103,98);
insert into T_score values(11,104,66);
insert into T_score values(11,105,83);insert into T_score values(13,104,83);
insert into T_score values(13,105,91);insert into T_score values(14,101,79);
insert into T_score values(14,102,100);
insert into T_score values(14,103,81);
insert into T_score values(14,104,77);insert into T_score values(15,101,84);
insert into T_score values(15,103,88);
insert into T_score values(15,104,60);
insert into T_score values(15,105,70);
//只列出部分成绩表的数据 可自行补齐。

练习题

这里列出了大约50个SQL练习题,解法未考虑效率只是为了尽可能多的练习语法。下面解法为Oracle数据库语法格式。

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

--解法一
create or replace view v11 
as
select *
from T_score
where course_id = 101;create or replace view v12 
as
select *
from T_score
where course_id = 102;select a.student_id 学号
from v11 a, v12 b
where a.student_id = b.student_id and a.score > b.score; 
--解法二  
create or replace view v13
as
(select a.student_id,max(case when b.course_id = 101 then b.score else NULL end) aa,max(case when b.course_id = 102 then b.score else NULL end) bb
from T_student a, T_score b
where a.student_id = b.student_id 
group by a.student_id);       select student_id 学号
from v13
where aa > bb;
--解法三
select a.student_id 学号
from t_score a, t_score b
where a.student_id = b.student_id and a.course_id = '101'and b.course_id = '102' and a.score > b.score;

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

--解法一
select a.student_id 学号, a.student_name 姓名, count(nvl(b.course_id, 0)) 选课数, sum(b.score) 总成绩
from T_student a, T_score b
where a.student_id = b.student_id
group by a.student_id, a.student_name;

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

select student_id 学号,trunc(avg(score), 2) 平均成绩 
from T_score 
group by student_id
having avg(score) > 60;

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

select count(1) 姓葛老师的人数
from T_teacher
where t_name like '葛%'; 

5、查询没学过“五木”老师课的同学的学号、姓名

--解法一
create or replace view v51
as
select b.course_id
from T_teacher a, T_course b
where a.t_id = b.t_id and a.t_name = '五木老师';create or replace view v52
as
select a.student_id aa, sum(case when b.course_id = c.course_id then 1 else 0 end) bb,a.student_name cc
from T_student a left join T_score bon a.student_id = b.student_id ,v51 c
group by a.student_id, a.student_name;select aa 学号, cc  姓名
from v52
where bb = 0;
--解法二
create or replace view v53
as
select c.student_id
from T_teacher a, T_course b, T_score c
where a.t_id = b.t_id and b.course_id = c.course_id and a.t_name = '五木老师';select student_id 学号
from T_student a
where student_id not in (select student_id from v53)
--解法三
create or replace view v53
as
select c.student_id
from T_teacher a, T_course b, T_score c
where a.t_id = b.t_id and b.course_id = c.course_id and a.t_name = '五木老师';select student_id 学号
from T_student a
where not exists (select 1 from v53 where v53.student_id = a.student_id)

6、查询学过“101”并且也学过编号“102”课程的同学的学号、姓名

--方法一
select a.student_id 学号, c.student_name 姓名
from (select * from T_score where course_id = 101) a,(select * from T_score where course_id = 102) b, T_student c
where a.student_id = b.student_id and b.student_id = c.student_id;
--方法二
select a.student_id 学号, b.student_name 姓名
from (select student_id from T_score where course_id = 101intersectselect student_id from T_score where course_id = 102)a, T_student b
where a.student_id = b.student_id

7、查询学过“五木”老师所教的所有课的同学的学号、姓名

--v52请见第五题第二种解法的视图
select aa 学号, cc  姓名
from v52
where bb = 1;

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

select a.student_id 学号, c.student_name 姓名
from (select * from T_score where course_id = 101) a,(select * from T_score where course_id = 102) b, T_student c
where a.student_id = b.student_id and b.student_id = c.student_id and b.score < a.score

9、查询所有课程成绩小于60分的同学的学号、姓名

create or replace view v91
as
select student_id, sum(case when score < 85 then 1 else 0 end) aa,count(1) bb
from T_score
group by student_id;select b.student_id 学号,  b.student_name 姓名
from v91 a, T_student b
where a.student_id = b.student_id and a.aa = a.bb;

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

create or replace view v92
as
select student_id, count(1) aa
from T_score
group by student_id;select b.student_id 学号,  b.student_name 姓名
from T_student b left join v92 a on a.student_id = b.student_id 
where nvl(a.aa, 0) != 5;

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

create or replace view v112
as
select a.student_id
from T_score a
where a.student_id != 1 and a.course_id in (select course_id from T_score where student_id = 1)
group by a.student_id;select distinct a.student_id 学号, b.student_name 姓名
from v112 a, T_student b
where a.student_id = b.student_id;

12、查询至少学过学号为“001”同学所有课的其他同学学号和姓名

create or replace view v121
as
select count(1) aa from T_score where student_id = 1;create or replace view v122
as    select student_id aa, sum(case when course_id in (select course_idfrom T_scorewhere student_id = 1) then 1 else 0 end) bb
from T_score
where student_id != 1
group by student_id;select a.aa
from v122 a, v121 b
where bb = b.aa;

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

create or replace view v131
as  select course_idfrom T_teacher a, t_course bwhere a.t_id = b.t_id and t_name = '五木老师'; create or replace view v132
as  select avg(score) bbfrom T_score a, v131 bwhere a.course_id = b.course_id;update T_score a 
set score = (select * from v132)
where (select course_idfrom T_teacher a, t_course bwhere a.t_id = b.t_id and t_name = '五木老师') = a.course_id;select * from T_score
where course_id = 102;

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

create or replace view v141
as  select student_id, course_id, scorefrom T_scorewhere student_id = 2;create or replace view v142  
as  select * from T_scorewhere student_id != 2;create or replace view v143  
as 
selectstudent_id,sum(case when a.course_id in (select course_idfrom v141) then 1 else -1 end) aa
from v142 a
group by student_id;select a.student_id 学号, b.student_name 姓名
from v143 a, t_Student b
where a.student_id = b.student_id and a.aa = (select count(1) aa from v141);

15、删除学习“五木”老师课的SC表记录

create or replace view v151
as  select b.course_idfrom T_teacher a, T_course bwhere a.t_id = b.t_id and a.t_name = '五木老师';delete from T_score
where course_id in (select course_id from v151);select * from T_score;

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

create or replace view v161
as
select student_id, max(case when course_id = 103 then 1 else 0 end) aa
from T_score
group by student_id;create or replace view v162
as
select b.student_id
from T_student b left join v161 aon a.student_id = b.student_id 
where a.aa = 0 or a.aa is NULL;create or replace view v163
as
select trunc(avg(score), 2) a
from T_score
where course_id = 102insert into T_score  (select a.student_id, 103, b.afrom v162 a, v163 b)

17、按平均成绩从高到低显示所有学生的“数据库”、“Python”、两门的课程成绩,(数据库,Linux)按如下形式显示: 学生ID,数据库,Python,有效课程数,有效平均分

create or replace view v171
as   
select course_id
from t_course
where course_name = '数据库';create or replace view v172
as
select course_id
from t_course
where course_name = 'linux';select student_id 学号,sum(case when course_id = (select course_id from v171) then score else NULL end) 数据库,sum(case when course_id = (select course_id from v172) then score else NULL end) Linux,count(1) 有效课程数,avg(score) 有效平均分                                                                        
from T_score
where course_id in (select course_idfrom t_Coursewhere course_name = '数据库' or course_name = 'linux')
group by student_id
order by 5 desc;--根据第五列排序

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

select course_id 课程ID, max(score) 最高分, min(score) 最低分
from T_score
group by course_id;

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

select course_id 课程ID, trunc(avg(score), 2) 平均成绩,trunc(sum(case when score >= 60 then 1 else 0 end) / count(1), 2) 及格百分比
from T_score
group by course_id
order by 2 ASC, 3 DESC;

20、查询如下课程平均成绩和及格率的百分数(用"1行"显示):(001),(002),(003),(004)

--方法一  
create or replace view v201 
as
select
round(100*sum(case when s.score>=60 and c.course_id=101 then 1 else 0 end)/sum(case when c.course_id=101 then 1 else 0 end),0)||'%' "101科目及格率",
round(100*sum(case when s.score>=60 and c.course_id=101 then 1 else 0 end)/sum(case when c.course_id=102 then 1 else 0 end),0)||'%' "102科目及格率",
round(100*sum(case when s.score>=60 and c.course_id=101 then 1 else 0 end)/sum(case when c.course_id=103 then 1 else 0 end),0)||'%' "103科目及格率",
round(100*sum(case when s.score>=60 and c.course_id=101 then 1 else 0 end)/sum(case when c.course_id=104 then 1 else 0 end),0)||'%' "104科目及格率",
round(100*sum(case when s.score>=60 and c.course_id=101 then 1 else 0 end)/sum(case when c.course_id=105 then 1 else 0 end),0)||'%' "105科目及格率"    
from t_score s, t_course c
where s.course_id=c.course_id;select * from v201;
--方法二
create or replace view v202 
as
select course_id, count(1) aa
from T_score
group by course_id;create or replace view v203
as
select course_Id, count(1) aa
from T_score
where score >= 60
group by course_id
order by aa;select a.course_Id 学号, trunc(b.aa / a.aa * 100, 2) || '%' 及格率
from v202 a, v203 b
where a.course_id = b.course_id
order by b.aa/a.aa DESC

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

create or replace view v211
as
select course_id, trunc(avg(score) ,2) aa
from T_score   
group by course_id;select c.t_name 教师姓名, a.aa 平均分
from v211 a, t_course b, t_teacher c
where a.course_Id = b.course_id and b.t_id = c.t_id
order by a.aa DESC;

22.查询如下课程成绩第 3 名到第 6 名的学生成绩单:1号课程,2号课程,3号课程,4号课程 [学生ID],[学生姓名],1号课程,2号课程,3号课程,4号课程,平均成绩

create or replace view v221
as
select student_id,sum(case when course_id = 101 then score else 0 end) aa,sum(case when course_id = 102 then score else 0 end) bb,sum(case when course_id = 103 then score else 0 end) cc,sum(case when course_id = 104 then score else 0 end) dd,avg(score) ee
from T_score
group by student_id
order by ee DESC;select student_id, aa,  bb, cc, dd, rownum ff
from v221select a.student_id, a.student_name, b.bb, b.cc, b.dd, b.ff
from T_student a, (select student_id, aa,  bb, cc, dd, rownum fffrom v221) b
where a.student_id = b.student_id and b.ff between 3 and 6;

23、统计各分数段人数显示结果如下:课程ID,课程名,<60人数,60<=x<80,>=80

create or replace view v231
as
select course_id,sum(case when score < 60 then 1 else 0 end) aa,sum(case when score between 60 and 79  then 1 else 0 end) bb,sum(case when score > 80 then 1 else 0 end) cc     
from T_score
group by course_idselect a.course_id 课程ID, course_name 课程名, a.aa "<60", a.bb "60~80", a.cc ">=80"
from v231 a, T_course b
where a.course_id = b.course_id;

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

create or replace view v241
as 
select student_id,trunc(avg(score), 2) aa
from T_score
group by student_id
order by aa DESC;select a.student_id 学号, a.student_name 姓名, b.aa 平均成绩, b.bb 名次
from T_student a left join (select student_id, aa, rownum bbfrom v241) b
on a.student_id = b.student_id   

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

create or replace view v251
as  
select max(score) aa
from T_score
group by course_id;

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

--方法一
select course_id 课程号, count(1) 人数
from T_score
group by course_id;  
--方法二
select sum(case when course_id = 101 then 1 else 0 end) "101",sum(case when course_id = 102 then 1 else 0 end) "102",sum(case when course_id = 103 then 1 else 0 end) "103",sum(case when course_id = 104 then 1 else 0 end) "104",sum(case when course_id = 105 then 1 else 0 end) "105"
from T_score

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

create or replace view v271
as
select student_id, count(1) aa
from T_score
group by student_idselect a.student_id, a.student_name
from T_student a, (select * from v271) b
where a.student_id = b.student_id  and b.aa = 1; 

28、查询男生、女生人数

--解法一 case when 可与decode互换
select sum(case when student_sex = 'M' then 1 else 0 end) 男生人数,sum(case when student_sex = 'F' then 1 else 0 end) 女生人数
from T_student
--解法二
select sum(decode(student_sex,'M', 1, 0)) 男生人数,sum(decode(student_sex,'F', 1, 0)) 女生人数
from T_student

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

select *
from T_student
where student_name like '张%';

30、查询同名同姓学生名单,并统计同名人数

--方法一
select a.student_name 姓名, count(1) 人数
from  T_student a, T_student b
where a.student_name = b.student_name and a.student_id != b.student_id
group by a.student_name;
--方法二
select student_name 姓名, count(1) 人数
from T_student a
where student_name  in (select student_name from T_student where a.student_id != student_id)
group by student_name;  

30、1995年出生的学生名单(注:Student表中Sage列的类型是datetime)

--方法一
select To_Char (SYSDATE,'YYYY') "year" from dual;  
--方法二
select *
from T_student
where (to_char(SYSDATE,'YYYY') - 1995) = student_age;

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

select course_id 学号, trunc(avg(score), 2) 平均成绩
from T_score
group by course_id
order by 2 asc, course_id DESC

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

create or replace view v331
as
select student_id, trunc(avg(score), 2) aa
from T_score
group by student_idselect a.student_id,b.student_name, a.aa
from v331 a, T_student b
where a.student_id = b.student_id and aa > 85

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

--方法一
create or replace view v341
as
select student_id, score
from T_score
where course_id = (select course_idfrom T_course where course_name = '数据库') and score < 80;select b.student_name 姓名, a.score 成绩      
from v341 a, T_student b
where a.student_id = b.student_id;
--方法二
select c.student_name 姓名, b.score 成绩
from T_course a, T_score b, T_student c
where a.course_id = b.course_id and b.student_id = c.student_id and a.course_name = '数据库' and b.score < 80

35、查询所有学生的选课情况(学生姓名、课程名称)

create or replace view v351
as
select a.student_id,a.student_name,b.course_id
from T_student a left join T_score b
on a.student_id = b.student_idselect student_id,max(case when b.course_name = '数据库' then '√' else NULL end) 数据库,max(case when b.course_name = 'QT' then '√' else NULL end) QT,max(case when b.course_name = 'C++' then '√' else NULL end) "C++",max(case when b.course_name = 'C语言' then '√' else NULL end) "C语言",max(case when b.course_name = 'linux' then '√' else NULL end) linix
from v351 a left join T_course b
on a.course_id = b.course_id
group by student_id
order by student_id;

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

create or replace view v361
as 
select a.student_id
from (select student_id,sum(case when score > 70 then 1 else 0 end) aa ,count(1) bbfrom T_scoregroup by student_id) a, T_student b
where a.student_id = b.student_id and a.aa = a.bb;select d.student_name, c.course_name,b.score
from v361 a, T_score b, T_course c, T_student d
where a.student_id = b.student_id and b.course_id  = c.course_id and a.student_id = d.student_id

37、查询平均成绩不及格的课程,并按课程号从大到小排列

select course_Id 课程ID,trunc(avg(score), 2) 平均成绩
from T_score
group by course_id
having avg(score) < 80
order by course_id DESC

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

select a.student_id 学号, b.student_name 姓名
from T_score a, T_student b
where a.student_id = b.student_id and course_id = 103 and score > 80 

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

select  count(distinct student_id) 学生人数 from T_score

40、查询选修“五木”老师所授课程中,每门成绩最高的学生姓名及其成绩。按如下个格式输出 课程名称 学生姓名 成绩(假设每门课最高分只有一个人)

create or replace view v401
as 
select b.course_id
from T_teacher a, T_course b
where a.t_id = b.t_id and a.t_name = '五木老师';create or replace view v402
as 
select max(score) aa
from T_score a
where a.course_id in (select * from v401)
group by course_id;select c.course_name 课程名称, b.student_name 姓名, a.score 成绩
from T_score a, T_student b, T_course c
where a.student_id = b.student_id and c.course_id = a.course_id and a.score in (select * from v402)

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

create or replace view v411
as 
select course_id, count(1) aa
from T_score  
group by course_idselect a.course_name 课程名, b.aa 人数
from t_Course a, v411 b
where b.course_Id = a.course_id;

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

select a.student_id 学号, a.course_Id 课程号, a.score 成绩
from T_score a, T_score b
where a.student_id = b.student_id and a.course_id != b.course_id and a.score = b.score;

43、查询每门功成绩最好的前两名

select * from t_score t
where (select count(1) from t_score where course_id=t.course_id and score > t.score) <= 1
order by course_id,score desc;

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

select course_id 课程号, count(1) 人数
from T_score
group by course_id
having count(1) >= 10
order by 2 DESC, course_id ASC

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

select student_id 学号
from T_score
group by student_id
having count(1) >= 2
order by student_id

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

select b.course_id 课程ID, b.course_name 课程名
from T_score a, T_course b
where a.course_id = b.course_id
having count(1) = (select count(1) from T_student)
group by b.course_id, b.course_name

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

--解法一
create or replace view v472
as
select a.student_name,sum(case when course_id in (select * from v471) then 1 else 0 end) aa
from T_student a left join T_score bon a.student_id = b.student_id
group by a.student_id, a.student_nameselect student_name 学生姓名
from   v472
where aa = 0 or aa is NULL;
--解法二
create or replace view v473
as
select distinct student_id
from T_score b
where course_id in (select course_id from v471)select student_name
from T_student
where student_id not in(select * from v473)

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

---解法一
create or replace view v481
as
select student_id, sum(case when score < 70 then 1 else 0 end) aa,trunc(avg(score), 2) bb
from T_score
group by student_id;select student_id 学号, bb 平均成绩
from v481
where aa >= 2;
--解法二
create or replace view v482
as
select student_id
from T_score
where score < 70
group by student_id
having count(1) >= 2;select a.student_id 学号, trunc(avg(score), 2) 平均分
from T_score a, v482 b
where a.student_id = b.student_id
group by a.student_id;
--解法三
select a.student_id 学号, trunc(avg(score), 2) 平均分
from T_score a
where (select count(1)from T_scorewhere a.student_id = student_id and score < 70) >= 2
group by a.student_id;
--解法四
create or replace view v483
as
select  a.student_id 
from T_score a, T_score b
where a.course_id = b.course_id and a.score < 70 and b.score < 70 and a.score != b.score
group by a.student_id
having count(1) >= 2 * 2;select b.student_id 学号, trunc(avg(b.score), 2) 平均分 
from v483 a, T_score b
where a.student_id = b.student_id
group by b.student_id;

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

select student_id 学号, score 分数
from T_score
where course_Id = 104 and score < 70
order by score

50、删除“004”同学的“001”课程的成绩

delete from T_score where student_id = 4 and course_id = 101;  

把这些题多练习练习,大厂的sql题都难不倒你。加油!


http://chatgpt.dhexx.cn/article/eoDqDFKU.shtml

相关文章

数据库复习题选择题+判断题+填空题(考试续命必备

数据库复习题选择题判断题填空题(考试续命必备 一些选择题 1、从计算机数据管理的角度看&#xff0c;信息就是数据&#xff0c;数据就是信息。&#xff08;B &#xff09; A、对 B、错 (描述事物的符号记录称为数据 数据库的数据项之间无联系&#xff0c;记录之间存在联系。…

【课后习题】高等数学第七版上第二章 导数与微分 第五节 函数的微分

习题2-5 1. 已知 y x 3 − x yx^3-x yx3−x, 计算在 x 2 x2 x2 处当 Δ x \Delta x Δx 分别等于 1 , 0.1 , 0.01 1,0.1,0.01 1,0.1,0.01 时的 Δ y \Delta y Δy 及 d y \mathrm{d} y dy. 2. 设函数 y f ( x ) yf(x) yf(x) 的图形如下, 试在图(a)、(b)、&#xff0…

高等数学同济七版课后习题答案

高等数学同济七版课后习题答案上册下册,习题全解指南。 一、《高等数学》(第七版)下册习题全解 第八章 向量代数与空间解析几何 下载地址: 链接:https://pan.baidu.com/s/185C8RB4Y9pYO84V4Rup1Wg 提取码:p0o8 习题8-1 向量及其线性运算 习题8-2 数量积 向量积 *混合积…

高等数学(第七版)同济大学 习题10-3 (后6题)个人解答

高等数学&#xff08;第七版&#xff09;同济大学 习题10-3&#xff08;后6题&#xff09; 函数作图软件&#xff1a;Mathematica 10. 利 用 球 面 坐 标 计 算 下 列 三 重 积 分 &#xff1a; \begin{aligned}&10. \ 利用球面坐标计算下列三重积分&#xff1a;&\end…

高等数学(第七版)同济大学 习题1-8 个人解答

高等数学&#xff08;第七版&#xff09;同济大学 习题1-8 函数作图软件&#xff1a;Mathematica 1. 设 y f ( x ) 的图形如图 1 − 39 所示&#xff0c;试指出 f ( x ) 的全部间断点&#xff0c;并对可去间断点补充或 修改函数值的定义&#xff0c;使它成为连续点。 \begin{…

浅谈一下前端单元测试

关于单元测试这个概念&#xff0c;我想很多前端的小伙伴都知道&#xff0c;但是却并不一定能描述清楚。由于我开始接触单元测试还是在四个月前&#xff0c;当时也只是做了一些纯函数的单元测试。所以在这里只能说浅谈一下前端单元测试。 什么是单元测试&#xff1f; 我理解的…

web前端测试要点

【说明】 JS压缩&#xff1a; 目的&#xff1a; 1、减少JS代码容量&#xff0c;增加下载速度和执行速度&#xff1b; 2、压缩后的JS代码不具备可识性&#xff0c;在一定程度上达到加密效果&#xff0c;防止被人轻易使用。 常规Javascript压缩的原理&#xff1a; 1、压缩多余的…

为什么必须执行前端测试?

对于网站的真实前端测试&#xff0c;必须在不同的设备和浏览器(具有多个版本)上检查功能和性能。在不同浏览器、浏览器版本和操作系统上评估网站的过程称为跨浏览器测试&#xff0c;它被认为是每个前端网站测试计划的重要组成部分&#xff0c;用于评估你能够通过无缝UI和UX取悦…

前端接口测试

背景 由于需求不断更改&#xff0c;项目前后端分离后&#xff0c;后端开发人员经常需要改字段加字段&#xff0c;也有可能删掉接口&#xff0c;这时候前端如果没有相对应的调整&#xff0c;就容易造成bug。为了解决这个问题&#xff0c;我们需要对接口进行测试。 测试被调用接…

前端测试方法

最近在学校的《系统分析与设计》一课的大作业上&#xff0c;由于我担任的是测试工程师的角色&#xff0c;因此小小的研究了一些前端和后端的测试到底要怎么做。本文着重于前端测试方法。 1. 什么是测试&#xff1f; 我把测试定义成&#xff1a;是一段检测你的应用代码&#xf…

前端测试都要测什么

单元测试&#xff0c;Unit Testing&#xff0c;简称 UT&#xff0c;是指对软件中的最小可测试单元进行检查和验证&#xff0c;这是最低级别的测试活动&#xff0c;前端开发中单元可以是一个 function 也可以是一个 class&#xff0c;也可以是一个组件。对他们的输出做断言检查&…

前端测试开发工具--mock 的使用

目录 1. 背景 2. Mock是什么 3. Mock能做什么 4. Mock实现方式 5. Mock市面上常见的解决方案 6. Python下unittest.mock使用 1. 背景 在实际产品开发过程中&#xff0c;某个服务或前端依赖一个服务接口&#xff0c;该接口可能依赖多个底层服务或模块&#xff0c;或第三方…

浅谈前端测试

浅谈前端测试 浅谈 TDD 和 BDD TDD Test Driven Development (测试驱动开发) 一种使用自动化单元测试来推动软件设计并强制依赖关系解耦的技术。使用这种做法的结果是一套全面的单元测试&#xff0c;可随时运行&#xff0c;以提供软件可以正常工作的反馈。大概的流程是先针对…

前端测试介绍

测试,作为软件工程的一项重要环节,用来保证项目的正确性,完整性,安全性和可靠性。 前端测试是前端工程化的重要环节,根据测试的粒度可以分为单元测试,功能测试(E2E测试),集成测试。 前端测试框架 单元测试 - Mocha - Jasmine - Jest 断言库 - chai - Jest - …

前端测试接口,POSTMAN一键调试

前端测试接口&#xff0c;POSTMAN一键调试 当我们在开发中&#xff0c;遇到接口有问题时&#xff0c;一般都会找后端battle一下&#xff0c;在这之前我们需要先确认问题&#xff0c;当我们浏览器不方便调试时&#xff0c;我们一般会借助postman&#xff0c;因为比较方便也比较…

【测试数据准备-绕过后端,前端测试】

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、 Charles 简单介绍二、本文使用场景三、Charles 通过修改后端返回值&#xff0c;在前端展示。1.工具安装和注册2.使用端点功能修改接口返参数 总结 前言 测…

前端测试一共有哪几种?

前言 哈喽&#xff0c;大家好&#xff0c;我是海怪。 最近有不少朋友找到我聊了聊测试相关的内容&#xff0c;发现他们对测试的分类有些迷茫。实际上测试一共就 3 种&#xff1a;E2E&#xff0c;集成&#xff0c;单测&#xff0c;其它的功能测试、UI 测试、界面测试只是它们中…

聊聊前端测试那点事儿

虽然如今前端测试这个事已经被大家所认可了&#xff0c;但我见过做前端测试的团队并不多&#xff0c;能把前端测试做好的团队&#xff0c;就更加凤毛麟角了。 这个现象背后的逻辑是&#xff1a;编写前端测试其实非常困难。 在编写测试代码时&#xff0c;我们有很多事情要考虑…

前端测试如何做?

前端测试大家天天做。但是你知道前端测试是怎么做的吗&#xff1f; 什么是前端测试? 前端测试是测试图形用户界面(GUI)、web应用程序或软件的功能和可用性的一种测试技术。前端测试的目标是测试整体功能&#xff0c;以确保web应用程序或软件的表示层在连续更新中没有缺陷。 …

前端测试

1.什么是测试 测试是一种验证我们的代码是否可以按预期工作的方法。 换句话说就是写一些代码来验证一段代码是否能得到预期设计代码时所期望的结果。 被测试对象可以是样式&#xff0c;功能&#xff0c;流程&#xff0c;组件等。 2.前端测试的意义&#xff08;这里主要指单元…