练习主要涉及四张表,分别如下:


student(sid,sname,sage,ssex) 学生表 

course(cid,cname,tid) 课程表 

score(sid,cid,score) 成绩表 

teacher(tid,tname) 教师表


首先建立表结构


CREATE TABLE student 

  ( 

     sid    INT, 

     sname varcha, 

     sage  INT, 

     ssex  varchar (8) 

  ); 

 

CREATE TABLE course 

  ( 

     cid    INT, 

     cname varcha 

     tid    INT 

  ); 

 

CREATE TABLE score 

  ( 

     sid    INT, 

     cid    INT, 

     score INT 

  ); 

 

CREATE TABLE teacher 

  ( 

     tid    INT, 

     tname varchar(16) 

  );

插入数据


--mysql、oracle

insert into student select 1,'刘一',18,'男' FROM dual union all

 select 2,'钱二',19,'女' FROM dual union all

 select 3,'张三',17,'男' FROM dual union all

 select 4,'李四',18,'女' FROM dual union all

 select 5,'王五',17,'男' FROM dual union all

 select 6,'赵六',19,'女' FROM dual; 

 

 insert into teacher select 1,'叶平' FROM dual union all

 select 2,'贺高' FROM dual union all

 select 3,'杨艳' FROM dual union all

 select 4,'周磊' FROM dual;

 

 insert into course select 1,'语文',1 FROM dual union all

 select 2,'数学',2 FROM dual union all

 select 3,'英语',3 FROM dual union all

 select 4,'物理',4 FROM dual;

 

 insert into score

 select 1,1,56 FROM dual union all 

 select 1,2,78 FROM dual union all 

 select 1,3,67 FROM dual union all 

 select 1,4,58 FROM dual union all 

 select 2,1,79 FROM dual union all 

 select 2,2,81 FROM dual union all 

 select 2,3,ROM dual union all 

 select 2,4,68 FROM dual union all 

 select 3,1,91 FROM dual union all 

 select 3,2,47 FROM dual union all 

 select 3,3,88 FROM dual union all 

 select 3,4,56 FROM dual union all 

 select 4,2,88 FROM dual union all 

 select 4,3,90 FROM dual union all 

 select 4,4,93 FROM dual union all 

 select 5,1,46 FROM dual union all 

 select 5,3,78 FROM dual union all 

 select 5,4,53 FROM dual union all 

 select 6,1,35 FROM dual union all 

 select 6,2,68 FROM dual union all 

 select 6,4,71 FROM dual;

 

 

 

--mysql

insert into student values (1,'刘一',18,'男'),

 (2,'钱二',19,'女'),

 (3,'张三',17,'男'),

 (4,'李四',18,'女'),

 (5,'王五',17,'男'),

 (6,'赵六',19,'女'); 

 

 insert into teacher values (1,'叶平'),

 (2,'贺高'),

 (3,'杨艳'),

 (4,'周磊');

 

 insert into course values (1,'语文',1),

 (2,'数学',2),

 (3,'英语',3),

 (4,'物理',4);

 

 insert into score values

 (1,1,56), 

 (1,2,78), 

 (1,3,67), 

 (1,4,58), 

 (2,1,79), 

 (2,2,81), 

 (2,3,92), 

 (2,4,68), 

 (3,1,91), 

 (3,2,47), 

 (3,3,88), 

 (3,4,56), 

 (4,2,88), 

 (4,3,90), 

 (4,4,93), 

 (5,1,46), 

 (5,3,78), 

 (5,4,53), 

 (6,1,35), 

 (6,2,68), 

 (6,4,71);

练习题Beginning


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


select a.sid from (select sid,score from score where cid=001) a,(select sid,score from score where cid=002) b where a.sid=b.sid and a.score>b.score;

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


select sid,avg(ifnull(score,0)) from score group by sid having avg(score)>60;

 

--补充:MySQL中如何查询的字段中可能存在null值,可以做一些处理

--1.isnull(exper) 判断exper是否为空,是则返回1,否则返回0

--2.ifnull(exper1,exper2)判断exper1是否为空,是则用exper2代替

--3.nullif(exper1,exper2)如果expr1= expr2 成立,那么返回值为NULL,否则返回值为expr1。

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


select t1.sid,t1.sname,count(t2.cid),sum(t2.score) from student t1 left join score t2 on t1.sid=t2.sid group by t1.sid,sname;

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


select count(distinct tname) from teacher where tname like '李%';

select count(distinct tname) from teacher where substr(tname,0,1)='李';

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


select sid,sname from student where sid not in 

(select distinct t1.sid from score t1 left join course t2 on t1.cid=t2.cid where t2.tid=(select tid from teacher where tname='叶平'));

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


select sid,sname from student where sid in (select sid from score where cid=001 or cid=002 group by sid having count(cid)>1);

select t1.sid,t1.sname from student t1 left join score t2 on t1.sid=t2.sid where t2.cid=001 and exists (select * from score t3 where t2.sid=t3.sid and t3.cid=002);

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


select sid,sname from student where sid in 

(select sid from score t1,course t2,teacher t3 where t1.cid=t2.cid and t2.tid=t3.tid and t3.tname='叶平' group by t1.sid having count(distinct t2.cid)=

(select count(distinct course.cid) from course left join teacher on course.tid=teacher.tid where teacher.tname='叶平'));

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


SELECT a.sid,a.sname from 

(select t1.sid,t1.sname,t2.score from student t1,score t2 where t1.sid=t2.sid and t2.cid=001) a,

(select t1.sid,t1.sname,t2.score from student t1,score t2 where t1.sid=t2.sid and t2.cid=002) b

where a.sid=b.sid and b.score < a.score;

select sid,sname from (

select t1.sid,t1.sname,t2.score,(select t3.score from score t3 where t1.sid=t3.sid and t3.cid=002) score2

from student t1,score t2 where t1.sid=t2.sid and t2.cid=001) where score2 < score;

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


select t1.sid,t1.sname from student t1 left join score t2 on t1.sid=t2.sid group by t1.sid,sname having max(t2.score)<60;

(2)查询所有课程中,存在成绩小于60分的同学的学号、姓名


select sid,sname from student where sid not in (select t1.sid from student t1 left join score t2 on t1.sid=t2.sid and t2.score>60);

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


select t1.sid,t1.sname from student t1 left join score t2 on t1.sid=t2.sid group by t1.sid,sname having count(t2.cid)<(select count(cid) from course);

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


select distinct t1.sid,t1.sname from student t1 left join score t2 on t1.sid=t2.sid where t2.cid in (select distinct cid from score where sid=001) and t1.sid <>001;

12.把“score”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩 


update score set score=(select avg(score) from score where cid=

(select t1.cid from course t1,teacher t2 where t1.tid=t2.tid and t2.tname='叶平')) where cid=

(select t1.cid from course t1,teacher t2 where t1.tid=t2.tid and t2.tname='叶平');

13.查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名; 


SELECT t1.sid,t1.sname from student t1,score t2 where t1.sid=t2.sid and t2.cid in 

(select distinct cid from score where sid=002) and t1.sid != 002 group by t1.sid,t1.sname 

having count(distinct t2.cid) = (select count(distinct cid) from score where sid=002);

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


delete from score where cid in (select t1.cid from course t1 left join teacher t2 on t1.tid=t2.tid where t2.tname='叶平'); 

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


insert into score(sid,cid,score) select sid,'002',(select avg(score) from score where cid='002') from student 

where sid not in (select sid from score where cid='003');

16.按平均成绩从高到低显示所有学生的“语文”、“数学”、“英语”、“物理”四门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,物理,有效课程数,有效平均分 


select t.sid 学生ID,

(select t1.score from score t1 left join course t2 on t1.cid=t2.cid where t1.sid=t.sid and t2.cname='语文') 语文,

(select t1.score from score t1 left join course t2 on t1.cid=t2.cid where t1.sid=t.sid and t2.cname='数学') 数学,

(select t1.score from score t1 left join course t2 on t1.cid=t2.cid where t1.sid=t.sid and t2.cname='英语') 英语,

(select t1.score from score t1 left join course t2 on t1.cid=t2.cid where t1.sid=t.sid and t2.cname='物理') 物理,

count(*) 有效课程数,avg(t.score) 有效平均分 from score t group by t.sid order by avg(t.score) desc;

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


select cid,max(score),min(score) from score group by cid;

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


select t.cid,avg(t.score),100*((select count(*) from score t1 where t1.cid=t.cid and score >= 60)/

(select count(*) from score t2 where t.cid=t2.cid)) 及格率 from score t 

group by t.cid order by avg(t.score) asc,及格率 desc;

select t.cid,avg(t.score),100*(sum(case when score >= 60 then 1 else 0 end)/count(*)) 及格率 from score t 

group by t.cid order by avg(t.score) asc,及格率 desc;

19.查询如下课程平均成绩和及格率的百分数(用"1行"显示): 语文001,数学002,英语003,物理004


select (select avg(score) from score where cid=001) avg1,

100*((select count(*) from score where cid=001 and score >= 60)/(select count(*) from score where cid=001)) pass1,

(select avg(score) from score where cid=002) avg2,

100*((select count(*) from score where cid=002 and score >= 60)/(select count(*) from score where cid=002)) pass2,

(select avg(score) from score where cid=003) avg3,

100*((select count(*) from score where cid=003 and score >= 60)/(select count(*) from score where cid=003)) pass3,

(select avg(score) from score where cid=004) avg4,

100*((select count(*) from score where cid=004 and score >= 60)/(select count(*) from score where cid=004)) pass4 from dual;

select sum(case when cid=001 then score else 0 end)/sum(case when cid=001 then 1 else 0 end) avg1,

100*(sum(case when score >= 60 and cid=001 then 1 else 0 end)/sum(case when cid=001 then 1 else 0 end)) pass1,

sum(case when cid=002 then score else 0 end)/sum(case when cid=002 then 1 else 0 end) avg2,

100*(sum(case when score >= 60 and cid=002 then 1 else 0 end)/sum(case when cid=002 then 1 else 0 end)) pass2,

sum(case when cid=003 then score else 0 end)/sum(case when cid=003 then 1 else 0 end) avg2,

100*(sum(case when score >= 60 and cid=003 then 1 else 0 end)/sum(case when cid=003 then 1 else 0 end)) pass3,

sum(case when cid=004 then score else 0 end)/sum(case when cid=004 then 1 else 0 end) avg2,

100*(sum(case when score >= 60 and cid=004 then 1 else 0 end)/sum(case when cid=004 then 1 else 0 end)) pass4 from score;

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


select t2.tid,t2.tname,t1.cid,t1.cname,avg(t3.score) from course t1,teacher t2,score t3 

where t1.tid=t2.tid and t1.cid=t3.cid group by t2.tid,t2.tname,t1.cid,t1.cname order by avg(t3.score) desc;

21.查询如下课程成绩第 3 名到第 6 名的学生成绩单:语文(001),数学(002),英语(003),物理(004) 

    [学生ID],[学生姓名],语文,数学,英语,物理,平均成绩 


--mysql

select t1.sid,t1.sname,

(select a.score from score a where a.sid=t1.sid and a.cid=001) score1,

(select b.score from score b where b.sid=t1.sid and b.cid=002) score2,

(select c.score from score c where c.sid=t1.sid and c.cid=003) score3,

(select d.score from score d where d.sid=t1.sid and d.cid=004) score4,

(select avg(e.score) from score e where e.sid=t1.sid and e.cid in (001,002,003,004)) avg_all

from student t1 order by avg_all desc limit 2,4;

 

--oracle

select sid,sname,score1,score2,score3,score4,avg_all from (

select rownum n,t.* from (

select t1.sid,t1.sname,

(select a.score from score a where a.sid=t1.sid and a.cid=001) score1,

(select b.score from score b where b.sid=t1.sid and b.cid=002) score2,

(select c.score from score c where c.sid=t1.sid and c.cid=003) score3,

(select d.score from score d where d.sid=t1.sid and d.cid=004) score4,

(select avg(e.score) from score e where e.sid=t1.sid and e.cid in (001,002,003,004)) avg_all

from student t1 order by avg_all desc) t ) where n between 3 and 6;

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


select t2.cid 课程ID,t2.cname 课程名称,sum(case when t1.score > 85 then 1 else 0 end)  '[100-85]',

sum(case when t1.score between 70 and 85 then 1 else 0 end) '[85-70]',

sum(case when t1.score between 60 and 70 then 1 else 0 end) '[70-60]',

sum(case when t1.score <60  then 1 else 0 end) '[ <60]' from score t1,course t2 

where t1.cid=t2.cid group by t2.cid,t2.cname;

23.查询学生平均成绩及其名次


select 1+(select count(distinct avg_score) from 

(select avg(score) avg_score from score group by sid) t1 where t1.avg_score > t2.avg_score2) 名次,

t2.sid,t2.avg_score2 from 

(select sid,avg(score) avg_score2 from score group by sid) t2 order by t2.avg_score2 desc;

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


--注意:MySQL中不可以用top N ,limit也不能直接用于in/any/all等子查询中

select t1.sid,t1.cid,t1.score from score t1 where exists  

(select count(1) from score where t1.cid=score.cid and t1.score < score having count(1) < 3) 

order by t1.cid,t1.score desc;

select t1.sid,t1.cid,t1.score from score t1 where  

(select count(sid) from score where t1.cid=score.cid and t1.score < score) < 3

order by t1.cid,t1.score desc;

25.查询每门课程被选修的学生数


select cid,count(sid) from score group by cid;

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


select sid,sname from student where sid in 

(select sid from score group by sid having count(cid) = 1);

27.查询男生、女生人数


select ssex,count(sid) from student group by ssex;

28.查询姓“张”的学生名单


select sid,sname from student where sname like '张%';

select sid,sname from student where substr(sname,1,1)='张';

--注意,在MySQL中substr( )函数是从1开始计算,而Oracle是从0开始

29.查询同名同姓学生名单,并统计同名同姓人数


select sname,count(sid) from student group by sname having count(sid)>1;

30.2001年出生的学生名单(注:student表中Sage列的类型是int)


select sid,sname from student where year(now())-2001+1=sage;

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


select cid,avg(score) from score group by cid order by avg(score),cid desc;

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


select t1.sid,t1.sname,avg(t2.score) from student t1,score t2 

where t1.sid=t2.sid group by t1.sid having avg(t2.score) >85;

33.查询课程名称为“物理”,且分数低于60的学生姓名和分数


select t1.sname,ifnull(t2.score,0) from student t1,score t2,course t3 where t1.sid=t2.sid and t2.cid=t3.cid

and t3.cname='物理' and t2.score < 60;

34.查询所有学生的选课情况


select t1.sid,t1.sname,t3.cid,t3.cname from student t1,score t2,course t3 

where t1.sid=t2.sid and t2.cid=t3.cid;

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


select t1.sname,t3.cname,t2.score from student t1,score t2,course t3

where t1.sid=t2.sid and t2.cid=t3.cid and t2.score >= 70;

36.查询不及格的课程,并按课程号从大到小排列

https://www.xiaohongshu.com/discovery/item/5d7490d300000000020192bc

select cid from score where score < 60 order by cid desc;

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


select t1.sid,t1.sname from student t1,score t2 where t1.sid=t2.sid and t2.cid=003 and t2.score >= 80;

38.求选了课程的学生人数


select count(t.sid) from (

select sid from score group by sid having count(cid) > 0) t;

select count(distinct sid) from score;

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

郑州不孕不育医院:http://www.zzchyy110.com/

select t1.sname,max(t2.score) from student t1,score t2,course t3,teacher t4 

where t1.sid=t2.sid and t2.cid=t3.cid and t3.tid=t4.tid and t4.tname='叶平';

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


select t1.sid,t1.cid,t1.score,t2.sid,t2.cid,t2.score from score t1,score t2 

where t1.cid <> t2.cid and t1.score=t2.score; 

41.查询每门功成绩最好的前两名


--与24题雷同

select t1.cid,t1.sid,t1.score from score t1 where 

(select count(t2.sid) from score t2 where t1.cid=t2.cid and t1.score < t2.score) < 2 

order by t1.cid,t1.score desc;

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


select cid,count(sid) from score group by cid having count(sid) > 4 

order by count(sid) desc,cid asc;

43.检索至少选修两门课程的学生学号


select sid from score group by sid having count(cid) >=2;

44.查询全部学生都选修的课程的课程号和课程名


select t2.cid,t2.cname from score t1,course t2 

where t1.cid=t2.cid group by t2.cid,t2.cname having count(t1.sid)=(select count(*) from student);

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


select t1.sname from student t1,score t2 where t1.sid=t2.sid and t2.cid in 

(select t1.cid from course t1,teacher t2 where t1.tid=t2.tid and t2.tname='叶平');

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


select sid,avg(ifnull(score,0)) from score where sid in (

select sid from score where score < 60 group by sid having count(cid) > 1) group by sid;

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


select sid from score where cid=004 and score < 60 order by score desc;

48.删除“002”同学的“001”课程的成绩


delete from score where sid=002 and cid=001;

以上代码是自己敲的,如有不合适或者更好的表达方式,麻烦留言,一起学习,谢谢!