数据库常用面试题

解答: 

--1
select * from students where sid in
(select r_sid from sresult where r_cid=(select cid from Course where cname='税收基础'))
--2
select * from students where sid in
(select r_sid from SResult where r_cid =2)
--3
select * from students where sid not in
(select r_sid from SResult where r_cid =5)
--4
select * from dbo.Students where sid in
(
select r_sid from  SResult group by r_sid having count(*) =
(select count(*) from Course)
)
--5
select count(distinct r_sid) from sresult
--select count(*) from
--(select r_sid from sresult
--group by r_sid) a
--6

select * from dbo.Students where sid in
(
select r_sid from  SResult group by r_sid having count(*) >5
)

解答:

--1
select * from students where st_id in
(select r_stid from Results where r_tid not in
(select t_id from dbo.Teachers where t_name='李明'))
--2
select a.st_name,b.fenshu from Students a,
(select r_stid,avg(r_fenshi) as fenshu from Results where r_fenshi <60 group by r_stid having count(*) >=2) b
where a.st_id = b.r_stid
--3

select * from Students where st_id in
(
select r_stid from Results where r_tid in
(select t_id from Teachers where t_lesson='数学' or t_lesson='英语')
group by r_stid having count(*) =2
)
--4
select r_stid from Results
where r_fenshi >
(select r_fenshi from dbo.Results where r_tid='t001' and r_stid='st002')
and r_tid ='t001'

--5
--select * from dbo.Students
--select * from dbo.Teachers
select * from (select * from Results where r_tid ='t001') a,(select * from Results where r_tid ='t002') b
where
 a.r_stid =b.r_stid
and a.r_fenshi >b.r_fenshi

--select * from Results where (r_tid ='t001' or r_tid ='t002') and  r_stid in ('st001','st002','st003') order by r_stid
--select * from Results where r_tid ='t002' order by r_stid

原文地址:https://www.cnblogs.com/jasonjiang/p/1763779.html