题型:SQL语句的考察

一 学生 – 课程数据库

l 学生表:由学号、姓名、性别、年龄、系科五个属性组成,关系模式为:S(Sno,Sname,Ssex,Sage,Sdept).  其中 Sno 为主码。 

l 课程表:由课程号、课程名、先修课程号、学时、学分五个属性组成,关系模式为:C(Cno,Cname,Cpno,Ctime,Ccredit),  其中 Cno 为主码。

l 选修表:有学号、课程号、成绩三个属性组成,关系模式为:SC(Sno,Cno,Grade),其中(Sno,Cno)为主码。

1查询 7号课程没有考试成绩的学生学号

selectsno from sc where cno=’7’ and grade is null

2查询 7号课程成绩在90分以上或60分以下的学生学号

selectsno from sc where sno=’7’ and (grade>90 or grade<60)

3查询课程名以“数据”两个字开头的所有课程的课程号和课程名。

selectcno,cname from c where cname like ‘数据%’

4查询每个学生所有课程的平均成绩,输出学生学号、平均成绩

selectsno,avg(grade) from sc group by cno

5查询每门课程的选修人数,输出课程号、选修人数。

selectcno,count(*) from sc group by cno

6查询选修 7号课程的学生的学号、姓名、性别。

selects.sno,sname,ssex from s,sc where s,sno=sc.sno and cno=’7’

7查询选修7号课程学生的平均年龄。

selectavg(sage) from s,sc where s.sno=sc.sno and cno=’7’

8查询由30名以上学生选修的课程号。

selectcno from sc group by cno having count(*)>30

9查询至今没有考试不及格的学生学号

selectsno from student where not exist(

     select * from sc where grade<60 andsc.sno=student.sno)

二 下面查询基于的表同一。

1找出选修课程号为 C2 的学生学号与成绩。

selectsno,garde from sc where cno=’C2        ’

2找出选修课程号为C4 的学生学号与姓名

selects.sno,sname from s,sc where s.sno=sc.sno and cno=’C4’

3找出选修课程名为 Maths 的学生学号与姓名。

selects.sno,sname from s,sc where s.sno=sc.sno and cname=’Maths’

4找出选修课程号为C2或C4 的学生学号。

selectdistinct sno from sc where cno in(‘C2’,’C4’)

或select distinct sno fromsc where cno=’C2’ or cno=’C4’

5找出选修课程号为C2和C4 的学生学号。

selectsno from sc where cno=’C2’ and sno in (

    select sno from sc where sno=’C4’)

6找出不学C2课程的学生姓名和年龄

selectsname,sage from s where sno not in(select sno from sc where cno=’C2’)

7找出选修了数据库课程的所有学生姓名。(与3同)

selects.sno,sname from s,sc,c

  where s.sno=sc.sno and c.cno=sc.cno andcname=’数据库’

8找出数据库课程不及格的女生姓名

selectsname from s,sc,c

wheres.sno=sc.sno and c.cno=sc.cno and ssex=’女’and cname=’数据库’ and grade<60

9找出各门课程的平均成绩,输出课程名和平均成绩

selectsname,avg(grade) from sc,c

wheres.cno=sc.cno

groupby sc.cno

10找出各个学生的平均成绩,输出学生姓名和平均成绩

selectsname,avg(grade) from s,sc

wheres.sno=sc.sno

groupby sc.cno

11找出至少有30个学生选修的课程名

selectcname from c where cno in (select sno from sc group by cno havingxount(*)>=30 )

12找出选修了不少于3门课程的学生姓名。

selectsname from s where sno in(select sno from sc group by sno havingcount(*)>=3)

13找出各门课程的成绩均不低于90分的学生姓名。

selectsname from s where sno not in(select sno from sc where grade<90)

14*找出数据库课程成绩不低于该门课程平均分的学生姓名。

selectsname from s where sno in

(

select sno from sc,c wheresc.cno=c.cno and cname=’数据库’ and

grade>(select avg(grade) fromsc,c where sc.cno=c.cno and cname=’数据库’)

)

15找出各个系科男女学生的平均年龄和人数。

selectsdept,ssex,avg(sage),count(*) from s

groupby sdept,ssex

16找出计算机系(JSJ)课程平均分最高的学生学号和姓名。

selectsc.cno,sname from s,sc where s.sno=sc.sno and sdept=’JSJ’

groupby sc.sno

havingavg(grade)=

(select top 1 avg(grade) from sc,s where s.sno=sc.sno and sdept=’JSJ

 group by sc.cno

order by avg(grade)  DESC

)

17补充:查询每门课程的及格率。

三 客户 – 商品数据库中包括3按各表:

l  客户表:由客户号、姓名、地址、电话、邮编五个属性组成,关系模式为:KH(Kno,Kname,Addr,Phone,YZBM),  其中Kno 为主码。

l  发票表:由发票号、客户号、开票日期、发票金额、业务员号五个属性组成,关系模式为:FP(Fno,Kno, Fdate,Fmoney,Yno), 其中 Fno 为主码。

l  业务员表:由业务员号、姓名、性别、年龄、工资、办公室编号六个属性组成,关系模式为:YWY( Yno,Yname,Ysex,Yage,Salary,Ono),其中 Yno 为主码。

1查询工资在 1000 到3000元之间的男性业务员的姓名和办公室编号。

selectYname,Ono from YWY where salary between 1000 and 3000 and Ysex=’男’

2查询各个办公室的业务员人数,输出办公室编号和对应的人数。

selectOno,count(*) from YWY group by Ono

3查询每个客户在2002年5月购买的总金额,输出客户号和相应的总金额。

selectKno,sum(Fmoney) from FP where Fdate between ‘2002.5.1’ and ‘2002.5.31’

groupby Kno

4查询2002年5月购买次数超过5次的所有客户号,且按客户号升序排序。

selectKno from FP where Fdate between ‘2002.5.1’ and ‘2002.5.31’

groupby Kno having count(*)>5

orderby Kno ASC

5查询各办公室男性和女性业务员的平均工资。

selectOno,Ysex,avg(salary) from YWY group by Ono,Ysex

6查询2002年5月曾经在王海亮业务员手中购买过商品的客户号、客户姓名、联系电话。

selectKno,Kname,phone from KH where Kno in

(

   select Kno from FP where Fdate between‘2002.5.1’ and ‘2002.5.31’ and

   Yno=(select Yno from YWY where Yname=’王海亮’)

)

7查询所有工资比1538号业务员高的业务员的编号、姓名、工资。

selectYno,Yname,salary from YWY where salary>(select salary from YWY whereYno=’1538’)

8查询所有与1538号业务员在同一个办公室的其他业务员的编号、姓名。

selectYno,Yname from YWY where Yno<>’1538’ and Ono in(select Ono from YWY whereYno=’1538’)

9查询销售总金额最高的业务员的编号。

selectYno from FP

groupby Yno

havingsum(Fmoney)=(select top 1 sum(Fmoney) from FP group by Yno order by sum(Fmoney)DESC)

10查询所有业务员的编号、姓名、工资以及工资比他高的其他业务员的平均工资。

selecty1.Yno,y1.Yname,y1.salary,avg(y2.salary)

fromYWY y1,YWY y2

wherey1.Yno<>y2.Yno and y1.salary<y2.salry

groupby y1.Yno

五 数据库存放着某高校1990年以来英语四、六级的考试情况,且规定:

1 英语四、六级考试每年分别在6月和12月举行二次;

2 四级没有通过的学生不能报考六级;

3 某一级的考试只要没有通过可以反复参加考试;   

4 某一级的考试一旦通过就不能再报考同级的考试;

5 允许报了名但不参加考试。

该数据库中有二张表,相应的关系模式如下:

学生表:S(Sno, Sname, Ssex, Sage, Sdept),其中Sno为主码。

考试表:E(Sno, Year, Month, Level, Grade),学号、年、月、级别、成绩。

其中(Sno, Year, Month)为主码。

1.      找出各次四级和六级考试的参考人数和平均成绩(报了名但没有参加考试的不作统计)

selectyear,month,level,count(*),avg(grade)

group byyear,month,level

2.      找出各次四级考试中平均分最高的系科(报了名但没有参加考试的不作统计)。

selectsdept from s,e

wheres.sno=e.sno and level=4

group bysdept

havingavg(grade)>all(select avg(grade) from s,e where s.sno=e,sno and level=4group by sdept)

select top1 sdept from s,e

wheres.sno=e.sno

wherelevel=4

order byavg(grade) DESC

3.      找出已经通过英语六级考试的学生的学号、姓名和性别(用连接方法做)

selects.sno,sname ssex from s,e

wheres.sno=e.sno and level=6 and grade>=60

4.      找出在同一年中四、六级考试都参加了的学生的学号

select snofrom e

where(level=4 and grade>60) or level=6

group byyear

havingcount(*)>2

select snofrom e x where level=4 and grade>=60 and exist

(select *from e y where y.sno=x.sno and yaer=x.year and level=6)

5.      找出只参加一次考试就通过了英语六级考试的学生的学号

select snofrom e

fromlevel=6

group bysno

havingcount(*)=1 and max(grade)>=60

select snofrom w where level=6 and grade>=60 and sno in

(selectsno from e where level=6 group by sno having count(*)=1)

6.      找出至今没有通过英语四级考试的学生的学号(应包括至今还没有参加过考试或者是参加了但还没有通过两种)

select snofrom e

where level=4

group bysno

havingmax(grade)<60

union

select snofrom where sno not in(select sno from e)

7.  找出英语六级考试中合格人数最少的考试年份和月份(有并列的都要列出,用一句SQL语句)。

select year,month from e

where level=6 and grade>=60

group by year,month

having count(*)<=all(selectcount(*) from e where level=6 and grade>=60 group by year,month)


猜你喜欢

转载自blog.csdn.net/iwm_NeXT/article/details/7482935