)
)
and t.sno = s.sno
;
select t.* ,s.cno,s.score from student t, score s
where s.cno in
(
select distinct cno from course c,teacher t
where c.tno =
(
select tno from teacher where tname='王燕'
)
)
and t.sno = s.sno
;
☞ 第二種方式
[sql] view plaincopyprint?
select * from student st
where st.sno in
(
select distinct sno from score s join course c
on s.cno=c.cno
join teacher t on c.tno=t.tno
where tname='王燕'
)
;
select * from student st
where st.sno in
(
select distinct sno from score s join course c
on s.cno=c.cno
join teacher t on c.tno=t.tno
where tname='王燕'
)
;
6、查詢學過“c001”并且也學過編號“c002”課程的同學的學號、姓名
[sql] view plaincopyprint?
--通過連接的方式實現
select * from score s
join score a on s.sno = a.sno
join student st on st.sno = s.sno
where s.cno='C001' and a.cno = 'C002'
and st.sno = s.sno
;
--通過連接的方式實現
select * from score s
join score a on s.sno = a.sno
join student st on st.sno = s.sno
where s.cno='C001' and a.cno = 'C002'
and st.sno = s.sno
;
7、查詢課程編號‘COO2’的成績比課程編號為'C001'的成績低的學生的所有信息。
呃,是不是有種似曾相識的感覺呢,和第一題沒有區別嘛,不過我們采用子查詢的
方式來實現。
[sql] view plaincopyprint?
select * from student t
join score a on t.sno = a.sno
join score b on t.sno = b.sno
where a.cno = 'C002'
and b.cno ='C001'
and a.score <= b.score
;
select * from student t
join score a on t.sno = a.sno
join score b on t.sno = b.sno
where a.cno = 'C002'
and b.cno ='C001'
and a.score <= b.score
;
哈哈使用連接的方式看起來更加簡單吧!
8、查詢所有課程成績都小于60分的學生的學號等信息
先來看看一種經常誤以為是正確的查詢吧!小生是在網上找的題庫
答案什么的感覺感覺有些問題啊,還是自己推敲吧
錯誤的查詢:
[sql] view plaincopyprint?
select st.*,s.score from student st
join score s on st.sno=s.sno
join course c on s.cno=c.cno
where s.score <60
select st.*,s.score from student st
join score s on st.sno=s.sno
join course c on s.cno=c.cno
where s.score <60
很容易的可以知道這個查詢只要有小于60分的課程都會查到,這并不符合題目的要求
下一種查詢方式:
思考所有的課程小于60,就是不存在某個學生的某門課程大于60分
[sql] view plaincopyprint?
select t.* from student t
where
not exists
(
select * from score s
where s.score >60.9 and t.sno = s.sno
)
and t.sno in
(
select sno from score
)
;
select t.* from student t
where
not exists
(
select * from score s
where s.score >60.9 and t.sno = s.sno
)
and t.sno in
(
select sno from score
)
;
9、查詢沒有學完所有課程的學生的信息
思考::
1、我們應該知道總共的課程數
2、再在score表中查詢,按照sno分組、并
去重,添加having子句
[sql] view plaincopyprint?
select t.sno,t.sname from student t
left join score on t.sno=score.sno
group by t.sno,t.sname
having count(score.cno)<
(
select count(distinct cno) from course
)
;
select t.sno,t.sname from student t
left join score on t.sno=score.sno
group by t.sno,t.sname
having count(score.cno)<
(
select count(distinct cno) from course
)
;
10、查詢至少有一門課與學號為‘S001’所選的課一樣的
原文轉自:http://blog.csdn.net/kiritor/article/details/8805310