目录

  • 表结构
  • 创建表
  • 练习题
    • 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题都难不倒你。加油!

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

  1. 50道经典SQL练习题和答案-1-10

    50个常用sql语句 一,创建表 Student(SId,Sname,Sage,Ssex) 学生表 Course(CId,Cname,TId) 课程表 SC(SId,CId,score) 成绩表 Te ...

  2. 50道MySQL经典练习题

    这个是网上流传的50道SQL练习题,最近拿来练习,刚做完,这里把我做的答案给大家做个参考,如有错误,还请告知. 另外,做题的时候先不要看答案(答案不唯一,只要满足要求即可),要有自己的一个思考过程,这 ...

  3. 深夜小酌,50道经典SQL题,真香~

      晚上听说我们村子快解封了,居家办公的日子已不多,久久不能平息~~   蹲坑之余,在网上找到了50道所谓经典SQL题,这不就是深夜必备小菜?我用脚叼起拖鞋,从冰箱拿出封印已久的半瓶可乐,打开数日未见 ...

  4. 50道基础sql语句

    sql语句练习50题 创建表cmd命令dos命令直接粘贴即可 注意中文一般没有办法存到表中,需要自行更改中文 如果在cmd命令中进行查询语句 1. 语句中有中文 将表改成utf8格式 命令为 set ...

  5. 面试必练:50道经典SQL练习

    –1.学生表 - Student(S,Sname,Sage,Ssex) –S 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别 –2.课程表 – Course(C,Cname,T ...

  6. 50 道 Python 基础练习题(附答案详解)

    作者:Amo Xiang https://blog.csdn.net/xw1680/article/details/103546693 1.两个变量的交换 # -*- coding: utf-8 -* ...

  7. 50道C++编程练习题及解答-c-编程例题

    截止到目前我已经写了 600多道算法题,其中部分已经整理成了pdf文档,目前总共有1000多页(并且还会不断的增加),大家可以免费下载 下载链接:https://pan.baidu.com/s/1hj ...

  8. Python学习,我带着练习题来了,50道基础入门练习题(附答案)

    实例001:数字组合 题目 有四个数字:1.2.3.4,能组成多少个互不相同且无重复数字的三位数?各是多少? 程序分析 遍历全部可能,把有重复的剃掉. 1 num=0 2 for a in range ...

  9. 如何快速掌握MYSQL?附牛客网精选的50道SQL题目详解【入门推荐】

    大家早上好,本人姓吴,如果觉得文章写得还行的话也可以叫我吴老师.欢迎大家跟我一起走进数据分析的世界,一起学习! 感兴趣的朋友可以关注我的数据分析专栏,里面有许多优质的文章跟大家分享哦. 另外也欢迎大家 ...

最新文章

  1. 月饼哪家强?Python 告诉你
  2. golang sync.Mutex 互斥锁 使用实例
  3. linux 自学系列:chown 目录权限设置
  4. vtun 接收和发送数据流程图
  5. 【Jenkins持续集成】docker部署+配置+操作Jenkins
  6. 云评测 | 开发者最有用的开源云监控工具有哪些呢? 这7款神器总有一款适合你!...
  7. 马斯克:全力支持狗狗币主要持有者出售货币 持仓太集中是问题
  8. 支付宝认错,回应央行 18 万行政罚单!
  9. obendclean php命令,ob_end_clean
  10. 小程序文档整理之 -- API(开放接口)
  11. Android添加大图通知栏消息
  12. Unity2D游戏制作常用方法
  13. 【3-hexo】为啥我又搭建了一个博客?【服务器自建博客】
  14. 【图解CAN总线】-8-CANFD总线网络“负载率”计算
  15. 北大惠普金融指数-匹配企业绿色创新指数2011-2020年:企业名称、年份、行业分类等多指标数据
  16. MyBatis12-分页插件
  17. 智能风控平台核心之风控决策引擎(三)
  18. 关于a:visited稀奇古怪直接生效或不生效的问题
  19. Google SEO入门教程,入门看这篇文章就行了
  20. 磨金石摄影技能干货分享|优秀纪实摄影作品欣赏—北京记事

热门文章

  1. CloudSim介绍和使用,CloudSim下载,CloudSim在IDEA中配置,CloudSim源码解读
  2. 简述微型计算机存储器的分类及各自的特点,存储器分类及各自特点有哪些
  3. python学爬虫书籍_Python3实战爬虫之爬取京东图书的图文详解
  4. ZCMU--1862: zbj的狼人杀
  5. 四维轻云地理空间数据在线管理平台这些新功能已上线,欢迎试用!
  6. 微波雷达人体感应开关模块 智能感应探测器 XBG-M555
  7. Android设备安装150个应用后开机耗时分析优化
  8. 如何在地址栏增加小图标
  9. 别再眼高手低了! 这些Linq方法都清楚地掌握了吗?
  10. Java学生管理系统和数据库idea