(1)select cname,score
from stc,courses
where stc.cno=courses.cno
and sc.sno='201715121'
(2)select cname,score
from stc NATURAL JOIN courses
where sc.sno='201715121'
(3)select cname,score
from stc JOIN courses on sc.cno=courses.cno
where sc.sno='201715121'
select students.sno,sname
from students,stc,courses
where students.sno=stc.sno
and stc.cno=courses.cno
and cname='计算机导论';
select students.sno,sname
from students NATURAL join stc
NATURAL join courses
where cname='计算机导论';
或者:
select students.sno,sname
from students join stc on students.sno=stc.sno
join courses on stc.cno=courses.cno
and cname='计算机导论';
select students.sno,sname
from students
where sno in (
select sno from stc
where cno in (
select cno from courses
where cname='计算机导论' ) );
或者:
select students.sno,sname
from students
where exists (
select * from stc,courses
where stc.cno=courses.cno
and students.sno=stc.sno
and cname='计算机导论') ;
(1)用外连接实现
select students.*
from students left join stc on students.sno=stc.sno
where stc.sno is null;
(2)用嵌套查询实现
select students.*
from students
where sno not in (
select distinct sno
from stc);
提示:课程名可能不唯一,但课程号唯一。
select courses.cno,cname,count(*)
from stc join courses on stc.cno=courses.cno
group by courses.cno having count(*)>=3;
group by子句也可写成:
group by courses.cno,cname having count(*)>=3;
select distinct sno from stc
where cno='b001'
and sno in (
select sno from stc
where cno='b002' );
select distinct sno
from stc
where cno in ( select cno from courses where cname='计算机导论' )
and sno in ( select sno from stc where cno in (
select cno from courses where cname='高等数学' )
) ;
提示:
(1)学分=学时/16
(2)选课但没有参加考试或考试不及格的课程,总学分均不统计。
select students.sno,sname,sum(chour)/16
from students,stc,courses
where students.sno=stc.sno
and stc.cno=courses.cno
and score>=60
group by students.sno;
update stc
set score =score+10
where sno in (
select sno from students
where ssex='女' and grade='2016' )
and cno =(select cno from courses --也可以将等号(=)换成in
where cname='C程序设计');
提示:STC_C表尚不存在。
create table stc_c
AS SELECT * FROM stc ---AS后面也可以用连接查询
where cno in (
select cno from courses
where cname='C程序设计');
insert into stc_c
SELECT * FROM stc
where cno in (
select cno from courses
where cname='高等数学'
);
13. (简答题)将所有女生的选课成绩置空。
update stc
set score =NULL
where sno in (
select sno from students
where ssex='女');
delete from courses
where cno not in (
select cno distinct from stc);