SQL Server練習題一則

來源:互聯網
上載者:User

整理了一則SQL SERVER練習題,學習了這個大家可以熟悉SQL SERVER相關知識。

問題描述:
已知關係模式:
s (sno,sname) 學生關係。

sno 為學號,

sname 為姓名

c (cno,cname,cteacher) 課程關係。

cno 為課程號,

cname 為課程名,

cteacher 為任課教師
sc(sno,cno,scgrade) 選課關係。

scgrade 為成績

要求實現如下5個處理:
1. 找出沒有選修過“李明”老師講授課程的所有學生姓名
2. 列出有二門以上(含兩門)不及格課程的學生姓名及其平均成績
3. 列出既學過“1”號課程,又學過“2”號課程的所有學生姓名
4. 列出“1”號課成績比“2”號同學該門課成績高的所有學生的學號
5. 列出“1”號課成績比“2”號課成績高的所有學生的學號及其“1”號課和“2”號課的成績

1. 找出沒有選修過“李明”老師講授課程的所有學生姓名

--實現代碼:
select sname from s
where not exists(
select * from sc,c
where sc.cno=c.cno
and c.cteacher='李明'
and sc.sno=s.sno)

2. 列出有二門以上(含兩門)不及格課程的學生姓名及其平均成績

--實現代碼:
select s.sno,s.sname,avg_scgrade=avg(sc.scgrade)
from s,sc,(
select sno
from sc
where scgrade<60
group by sno
having count(distinct cno)>=2
)a where s.sno=a.sno and sc.sno=a.sno
group by s.sno,s.sname

3. 列出既學過“1”號課程,又學過“2”號課程的所有學生姓名

--實現代碼:
select s.sno,s.sname
from s,(
select sc.sno
from sc,c
where sc.cno=c.cno
and c.cname in('1','2')
group by sno
having count(distinct cno)=2
)sc where s.sno=sc.sno

4. 列出“1”號課成績比“2”號同學該門課成績高的所有學生的學號

--實現代碼:
select s.sno,s.sname
from s,sc sc1,sc sc2
where sc1.cno='1'
and sc2.sno='2'
and sc1.cno=s.cno
and sc1.scgrade>sc2.scgrade

5. 列出“1”號課成績比“2”號課成績高的所有學生的學號及其“1”號課和“2”號課的成績

--實現代碼:
select sc1.sno,[1號課成績]=sc1.scgrade,[2號課成績]=sc2.scgrade
from sc sc1,sc sc2
where sc1.cno='1'
and sc2.cno='2'
and sc1.sno=sc2.sno
and sc1.scgrade>sc2.scgrade

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.