sql练习题(student,teacher,course,sc)

create table student(
 sno varchar2(10) primary key, --学生编号
 sname varchar2(20),-- 学生姓名
 sage number(2), --年龄
 ssex varchar2(5)--性别
 );
 create table teacher(
 tno varchar2(10) primary key, -- 教师编号
 tname varchar2(20)-- 教师姓名
 );
 create table course(
 cno varchar2(10), --课程编号
 cname varchar2(20), --课程名称
 tno varchar2(20), --教师编号
 constraint pk_course primary key (cno,tno)
 );
 create table sc(
 sno varchar2(10), -- 学生编号
 cno varchar2(10), --课程编号
 score number(4,2), -- 分数
 constraint pk_sc primary key (sno,cno)
 );
 /******学生表******/
insert into student values ('s001','张三',23,'男');
insert into student values ('s002','李四',23,'男');
insert into student values ('s003','吴鹏',25,'男');
insert into student values ('s004','琴沁',20,'女');
insert into student values ('s005','王丽',20,'女');
insert into student values ('s006','李波',21,'男');
insert into student values ('s007','刘玉',21,'男');
insert into student values ('s008','萧蓉',21,'女');
insert into student values ('s009','陈萧晓',23,'女');
insert into student values ('s010','陈美',22,'女');
commit;
 /******************教师表***********************/
insert into teacher values ('t001', '刘阳');
insert into teacher values ('t002', '谌燕');
insert into teacher values ('t003', '胡明星');
commit;
 /***************课程表****************************/
insert into course values ('c001','J2SE','t002');
 insert into course values ('c002','Java Web','t002');
 insert into course values ('c003','SSH','t001');
 insert into course values ('c004','Oracle','t001');
 insert into course values ('c005','SQL SERVER 2005','t003');
 insert into course values ('c006','C#','t003');
 insert into course values ('c007','JavaScript','t002');
 insert into course values ('c008','DIV+CSS','t001');
 insert into course values ('c009','PHP','t003');
 insert into course values ('c010','EJB3.0','t002');
 commit;
 /***************成绩表***********************/
insert into sc values ('s001','c001',78.9);
 insert into sc values ('s002','c001',80.9);
 insert into sc values ('s003','c001',81.9);
 insert into sc values ('s004','c001',60.9);
 insert into sc values ('s001','c002',82.9);
 insert into sc values ('s002','c002',72.9);
 insert into sc values ('s003','c002',81.9);
 insert into sc values ('s001','c003',59);
 commit;

select avg(score) from sc;

select min(score) from sc;
select sum(score) from sc;
select min(score),avg(score),sum(score) from sc;

-- 6.查询课程名称为"数据库",且分数低于60 的学生姓名和分数
select c.cname,sc.score,st.sname
from student st,course c,sc
where st.sno=sc.sno and c.cno=sc.cno and c.cname='J2SE' and sc.score>60

--7.查询所有学生的选课情况;
select st.sname,sc.cno,c.cname from student st,sc,course c
where sc.sno=st.sno and sc.cno=c.cno;
--8.查询任何一门课程成绩在70 分以上的姓名.课程名称和分数;
select st.sname,c.cname,sc.score
from student st,course c,sc
where st.sno=sc.sno and c.cno=sc.cno and sc.score>70
--9.查询不及格的课程,并按课程号从大到小排列
select DISTINCT c.cname, c.cno,sc.score
from course c,sc
where c.cno=sc.cno  and sc.score>60
order by cno desc
--10.查询没学过"谌燕"老师讲授的任一门课程的学生姓名
 select st.sno,st.sname
 from student st
 where st.sno not in
 (select sc.sno from sc,course c,teacher t
 where sc.cno=c.cno and c.tno=t.tno and t.tname='谌燕');
--11.查询两门以上不及格课程的同学的学号及其平均成绩
select st.sno,avg(sc.score)
from student st,sc
where st.sno in
(select sno from sc where score<60
group by sno
having count(sno)>1
)
and st.sno=sc.sno
group by st.sno
--13.删除"s002"同学的"c001"课程的成绩
delete from sc
where sno='s002' and cno='c001';
--15.查询平均成绩大于60 分的同学的学号和平均成绩;
select sc.sno,avg(sc.score)
from sc
group by sc.sno
having avg(sc.score)>60;
--查询每个同学选课总数与总分数(不包含没有选课的同学)
select a.*,s.sname
from
(select sno,sum(score),count(cno) from sc group by sno) a ,student s
where a.sno=s.sno;
--包含没有选课的同学
select a.*,s.sname
from
(select sno,sum(score),count(cno) from sc group by sno) a
right join student s
on a.sno=s.sno;
--17.查询姓"刘"的老师的个数;
select count(*) from teacher t
where t.tname like '刘%'
-- 18.查询没学过"谌燕"老师课的同学的学号.姓名;
select st.sno,st.sname
from student st
where st.sno not in
(
select sc.sno from sc,course c,teacher t
where sc.cno=c.cno and c.tno=t.tno and t.tname='谌燕'
);
--19.查询学过"c001"并且也学过编号"c002"课程的同学的学号.姓名;
select st.sno,st.sname
from student st,sc
where st.sno in
(
select st.sno
from student st,sc
where st.sno=sc.sno and sc.cno='c001'
)
and sc.sno=st.sno and sc.cno='c002'
--或者
 select st.* from sc a
 join sc b on a.sno=b.sno
 join student st
 on st.sno=a.sno
 where a.cno='c001' and b.cno='c002' and st.sno=a.sno;
--20.查询学过"谌燕"老师所教的课的同学的学号:姓名;
 select distinct st.*,s.*,c.*,t.* from student st join sc s on st.sno=s.sno
 join course c on s.cno=c.cno
 join teacher t on c.tno=t.tno
 where t.tname='谌燕';
--21.查询课程编号"c002"的成绩比课程编号"c001"课程低的所有同学的学号.姓名;
select st.sno,st.sname
from student st
join sc a on st.sno=a.sno
join sc b on a.sno=b.sno
where a.cno='c002' and b.cno='c001' and a.score--2.查询所有课程成绩小于60 分的同学的学号.姓名;
select * from student st join
(
select sno,max(score)  from sc
having max(sc.score)<70
group by sc.sno
) a on st.sno=a.sno
--23.查询没有学全所有课的同学的学号.姓名;
 select stu.sno,stu.sname,count(sc.cno) from student stu
 left join sc on stu.sno=sc.sno
 group by stu.sno,stu.sname
 having count(sc.cno)<(select count(distinct cno)from course)
-- 24.查询至少有一门课与学号为"s001"的同学所学相同的同学的学号和姓名;
select distinct st.sno,st.sname from student st
join sc on st.sno=sc.sno
where sc.cno in
(
select cno from sc where sno='s001'
)
--25.查询至少学过学号为"s001"同学所有一门课的其他同学学号和姓名;

你可能感兴趣的:(sql学习之路-sql练习)