PostgreSQL练习

学生表 Student
create table Student(Sid varchar(6), Sname varchar(10), Sage datetime, Ssex varchar(10));
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女')
成绩表 SC
create table SC(Sid varchar(10), Cid varchar(10), score decimal(18,1));
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98)
课程表 Course
create table Course(Cid varchar(10),Cname varchar(10),Tid varchar(10));
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03')
教师表 Teacher
create table Teacher(Tid varchar(10),Tname varchar(10));
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五')


练习题:
1.查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数
select s.*,score
from sdb."Student" s,sdb.sc
where s.Sid=sc.Sid
and s.Sid in(select a.Sid
from sdb.sc a,sdb.sc b
where a.Sid=b.Sid
and a.score>b.score
and a.Cid='01'
and b.Cid='02');

2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
select s.Sid,Sname,avg(score)
from sdb.sc,sdb."Student" as s
where s.Sid=sc.Sid
group by s.Sid,Sname
having avg(score)>=60;

3.查询在 SC 表存在成绩的学生信息
select *
from sdb.sc,sdb."Student" as s
where s.Sid=sc.Sid
and sc.score is not null;

4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
select s.Sid,Sname,count(Cid),sum(score)
from sdb."Student" s left join sdb.sc
on s.Sid=sc.Sid
group by s.Sid,Sname;

5.查询「李」姓老师的数量
select count(Tname)
from sdb.teacher as t
where t.Tname like '李%';

6.查询学过「张三」老师授课的同学的信息
select *
from sdb."Student" as s,sdb.course as c,sdb.teacher as t,sdb.sc
where s.Sid=sc.Sid
and sc.Cid=c.Cid
and c.Tid=t.Tid
and Tname='张三';

7.查询没有学全所有课程的同学的信息
select *
from sdb."Student" as s
where s.Sid in (select sc.Sid from sdb.sc group by sc.Sid having count(Cid)<3 )

select s.Sid
from sdb."Student" as s
left join sdb.sc
on s.Sid=sc.Sid
group by s.Sid
having count(sc.Cid)<(select count(Cid) from sdb.course);

8.查询和" 01 "号的同学学习的课程完全相同的其他同学的信息
select s.Sid,Sname,Sage,Ssex
from sdb."Student" s,sdb.sc
where s.Sid=sc.Sid
and s.Sid !='01'
group by s.Sid,Sname,Sage,Ssex
having count(Cid)=(select count(Cid) from sdb.sc where Sid='01');

9.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
select *
from sdb."Student" s
where Sid in (select Sid
from sdb.sc
where Cid in(select Cid
from sdb.sc
where Sid='01'));

10.查询没学过"张三"老师讲授的任一门课程的学生姓名
select Sname
from sdb."Student" s
where Sid not in(
select s.Sid
from sdb."Student" as s,sdb.course as c,sdb.teacher as t,sdb.sc
where s.Sid=sc.Sid
and sc.Cid=c.Cid
and c.Tid=t.Tid
and Tname like '张三');

11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select s.Sid,Sname,avg(score)
from sdb."Student" s,sdb.sc
where s.Sid=sc.Sid
and s.Sid not in (select distinct Sid
from sdb.sc
where score>=60)
group by s.Sid,Sname;

12.检索" 01 "课程分数小于 60,按分数降序排列的学生信息
select s.*
from sdb."Student" s,sdb.sc
where s.Sid=sc.Sid
and Cid='01'
and score<60
order by score desc;

13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select score,avg(score)
from sdb.sc
group by score
order by avg(score) desc;

select Sid,
sum(case when Cid='01' then score else null end) score_01,
sum(case when Cid='02' then score else null end) score_02,
sum(case when Cid='03' then score else null end) score_03,
avg(score)
from sdb.sc
group by Sid
order by avg(score) desc;

14.查询各科成绩最高分、最低分和平均分,以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率(及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90)。
select sc.Cid ,Cname 课程名称,avg(score) 平均分,max(score) 最高成绩,min(score) 最低成绩,
(sum(case when score>=60 then 1 else 0 end)*100/count(score)) 及格率,
(sum(case when score>=70 and score<80 then 1 else 0 end)*100/count(score)) 中等率,
(sum(case when score>=80 and score<90 then 1 else 0 end)*100/count(score)) 良好率,
(sum(case when score>=90 then 1 else 0 end )*100/count(score)) 优秀率
from sdb.sc,sdb.course c
where sc.Cid=c.Cid
group by sc.Cid,Cname

15.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比
select sc.Cid 课程编号,Cname 课程名称,
(sum(case when score>=0 and score<=60 then 1 else 0 end)*1.00/count(score)) as 不及格0_60,
(sum(case when score>60 and score<=70 then 1 else 0 end)*1.00/count(score)) as 及格60_70,
(sum(case when score>70 and score<=85 then 1 else 0 end)*1.00/count(score)) as 良好70_85,
(sum(case when score>85 and score<=100 then 1 else 0 end)*1.00/count(score)) as 优秀85_100
from sdb.sc,sdb.course c
where sc.Cid=c.Cid
group by sc.Cid,Cname

16.查询各科成绩前三名的记录
select Cid,Sid,score
from sdb.sc
where (select count(*)
from sdb.sc a
where a.Cid=sc.Cid
and sc.score>a.score)<3
order by Cid,score desc;

select * from (select *,rank() over(partition by Cid order by score desc) as graderank from sdb.sc) a
where a.graderank <=3;

17. 查询出只选修两门课程的学生学号和姓名
select s.Sid,Sname
from sdb."Student" s,sdb.sc
where s.Sid=sc.Sid
group by s.Sid,Sname
having count(Cid)=2;

18.查询名字中含有「风」字的学生信息
select *
from sdb."Student" s
where Sname like '%风%';

19.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
select s.Sid,Sname,Ssex,Sage,score
from sdb."Student" as s,sdb.sc
where s.Sid=sc.Sid
group by s.Sid,Sname,Ssex,Sage,score
having max(score)=(select max(score)
from sdb."Student" as s,sdb.course as c,sdb.teacher as t,sdb.sc
where s.Sid=sc.Sid
and sc.Cid=c.Cid
and c.Tid=t.Tid
and Tname like '张三');

posted on 2019-10-18 17:49  一只小白two  阅读(889)  评论(0编辑  收藏  举报