SQL 练习题
一 学生 – 课程数据库
1 查询 7号课程没有考试成绩的学生学号
select sno from sc where cno=’7’ and grade is null
2 查询 7号课程成绩在90分以上或60分以下的学生学号
select sno from sc where grade>90 or grade<60
3 查询课程名以“数据”两个字开头的所有课程的课程号和课程名。
Select cno,cname from c where cname like ‘数据%’
4 查询每个学生所有课程的平均成绩,输出学生学号、平均成绩
select sno,avg(grade) from sc group by sno
5 查询每门课程的选修人数,输出课程号、选修人数。
Select cno,count(*) from sc group by cno
6 查询选修 7号课程的学生的学号、姓名、性别。
Select s.sno, sname,ssex from s , sc where s.sno=sc.sno and cno = ‘7’
7 查询选修7号课程学生的平均年龄。
Select avg(sage) from s , sc where s.sno=sc.sno and cno = ‘7’
8 查询由30名以上学生选修的课程号。
Select sno from sc group by cno having count(*)>30
9 查询至今没有考试不及格的学生学号
a: select sno from s where sno not in ( select sno from sc where grade<60 )
b: select sno from sc group by sno having min(grade)>=60
二
1 找出选修课程号为 C2 的学生学号与成绩。
Select sno,grade from sc where cno=’C2’
2 找出选修课程号为C4 的学生学号与姓名。
Select s.sno , sname from s,sc where s.sno=sc.sno and cno=’C4’
3 找出选修课程名为 Maths 的学生学号与姓名。
Select s.sno ,sname from s,sc,c
where s.sno=sc.sno and c.cno=sc.cno and cname = ‘Maths’
4找出选修课程号为C2或C4 的学生学号。
Select distinct sno from sc where cno in (‘C2’,’C4’)
或: Select distinct sno from sc where cno=’C2’ or cno =’C4’
5找出选修课程号为C2和C4 的学生学号。
Select sno from sc where cno =’C2’ and sno in (
select sno from sc where cno = ‘C4’ )
6 找出不学C2课程的学生姓名和年龄
select sname , sage from s where sno not in ( select sno from sc where cno=’C2’ )
或:
select sname , sage from s where not exists ( select * from sc where sc.sno=s.sno and cno=’C2’ )
7 找出选修了数据库课程的所有学生姓名。(与3同)
Select s.sno ,sname from s,sc,c
where s.sno=sc.sno and c.cno=sc.cno and cname = ‘数据库’
8 找出数据库课程不及格的女生姓名
嵌套:
select sname from s where ssex = ‘女’ and sno in ( select sno from sc where grade<60 and cno in ( select cno from c where cname=’数据库’) )
连接:
Select sname from s,sc,c
where s.sno=sc.sno and c.cno=sc.cno and ssex=’女’ and cname = ‘数据库’ and grade<60
9 找出各门课程的平均成绩,输出课程名和平均成绩
select cname , avg(grade) from sc , c where c.cno =sc.cno group by sc.cno
10找出各个学生的平均成绩,输出学生姓名和平均成绩
select sname , avg(grade) from s , sc where s.sno=sc.sno group by sc.sno
11 找出至少有30个学生选修的课程名
select cname from c where cno in ( select cno from sc group by cno having count(*)>=30 )
12 找出选修了不少于3门课程的学生姓名。
Select sname from s where sno in ( select sno from sc group by sno having count(*)>=3)
13 找出各门课程的成绩均不低于90分的学生姓名。
Select sname from s where sno not in ( select sno from sc where grade<90)
14* 找出数据库课程成绩不低于该门课程平均分的学生姓名。
Select sname from s where sno in (
Select sno from sc , c where sc.cno=c.cno and cname=’数据库’ and
Grade > (Select avg(grade) from sc , c where sc.cno=c.cno and cname=’数据库’ ) )
15 找出各个系科男女学生的平均年龄和人数。
Select sdept,ssex , avg(sage) , count(*) from s
Group by sdept , ssex
16 找出计算机系(JSJ)课程平均分最高的学生学号和姓名。
Select sc.sno , sname from s, sc where s.sno=sc.sno and sdept=’JSJ’
Group by sc.sno Having avg(grade) =
( Select top 1 avg(grade) from sc, s where s.sno=sc.sno and sdept=’JSJ’
group by sc.sno order by avg(grade) DESC )
三 客户 – 商品数据库中包括3按各表:KH,FP,YWY
1 查询工资在 1000 到3000 元之间的男性业务员的姓名和办公室编号。
Select Yname , Ono from YWY where salary between 1000 and 3000 and Ysex=’男’
2 查询各个办公室的业务员人数,输出办公室编号和对应的人数。
Select Ono , count(*) from YWY group by Ono
3 查询每个客户在2002年5月购买的总金额,输出客户号和相应的总金额。
Select Kno,sum(Fmoney) from FP where fdate between ‘2002.5.1’ and ‘2002.5.31’
Group by Kno
4 查询2002年5月购买次数超过5次的所有客户号,且按客户号升序排序。
Select Kno from FP where fdate between ‘2002.5.1’ and ‘2002.5.31’
Group by Kno having count(*)>5
Order by Kno ASC
5 查询各办公室男性和女性业务员的平均工资。
Select Ono,Ysex ,avg(salary) from YWY group by Ono , Ysex
6 查询2002年5月曾经在王海亮业务员手中购买过商品的客户号、客户姓名、联系电话。
Select Kno,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号业务员高的业务员的编号、姓名、工资。
Select yno ,Yname, salary from YWY where salary >
( Select salary from YWY where Yno=’1538’ )
8 查询所有与1538号业务员在同一个办公室的其他业务员的编号、姓名。
Select Yno , Yname from YWY where Yno<>’1538’ and Ono in (
Select Ono from YWY where Yno=’1538’ )
9 查询销售总金额最高的业务员的编号。
Select Yno from FP Group By Yno Having sum(Fmoney) =
(Select top 1 sum(Fmoney) from FP group by Yno ORDER BY sum(Fmoney) DESC)
10 查询所有业务员的编号、姓名、工资以及工资比他高的其他业务员的平均工资。
利用自身连接
Select y1.Yno ,y1.Yname ,y1.salary , avg( y2. salary) from YWY y1 , YWY y2
Where y1.Yno<>y2.Yno and y1.salary < y2.salary
Group by y1.Yno
Sno salary sno salary
1 100 1 100
2 120 2 120
3 90 3 90
4 110 4 110
四 某中学数据库中由一张表:
学生选课表:由板及代码、班内学号、姓名、科目、成绩五个属性组成,关系模式为
SC(BJDM,BNXH,XSXM,KM,CJ) ,其中(BJDM,BNXH)为主码。
说明:每个学生每门科目存放一个记录,科目有“语文”、“数学”、“外语”三门。
1 找出每个班级的班级代码、学生人数、平均成绩。
Select BJDM,count(*) ,avg(CJ) from SC group by BJDM
2 找出每个学生的班级代码、学生姓名、考试科目数、总成绩。
Select BJDM,XSXM,count(*) , sum(CJ) from SC
Group by BNXH
3 输出一张表格,每位学生对应一条记录,包括:班级代码、姓名、语文成绩、数学成绩、外语成绩。
方法一:利用视图
create view v1 (bjdm,xsxm, yw,sx,wy ) AS
select bjdm , xsxm , cj , 0,0 from sc where km=’语文’
union
select bjdm , xsxm , 0 , cj,0 from sc where km=’数学’
union
select bjdm , xsxm , 0,0,cj from sc where km=’外语’
select bjdm, xsxm , sum(yw) as 语文, sum(sx) as 数学, sum(wy) as 外语 from v1 group by bjdm, xsxm
方法二:自身连接
select a.bjdm,a.xsxm , a.km,a.cj , b.km,b.cj , c.km,c.cj from sc a , sc b , sc c
where a.bjdm=b.bjdm and a.bnxh= b.bnxh and b.bjdm=c.bjdm and b.bnxh= c.bnxh
and a.km=’语文’ and b.km=’数学’ and c.km=’外语’
方法三:利用存储过程(略)
4 输出一张表格:由成绩低于60分的每位学生对应一条记录,包括字段:班级代码、姓名、最低成绩。
Select bjdm,xsxm ,min(CJ) from sc where grade<60 group by bjdm,xsxm
5输出一张表格:由成绩低于60分的每位学生对应一条记录,包括字段:班级代码、姓名、最高成绩、平均成绩。
得到平均成绩:create view V1 (bjdm,bnxh ,avg_cj) AS
select bjdm,bnxh ,avg(cj) from sc where bjdm , bnxh
select sc.bjdm,sc.xsxm ,max(cj) , avg_cj from sc , V1
where sc.bjdm=v1.bjdm and sc.bnxh=V1.bnxh and cj<60
group by sc.bjdm,sc.xsxm
6输出一张表格:所有成绩不低于60分的每位学生对应一条记录,包括字段:班级代码、姓名、平均成绩。
select bjdm, xsxm , avg(cj) from sc
where sno not in ( select sno from sc where grade<60)
group by bjdm, xsxm
7输出一张表格:每一位学生对应一条记录,包括字段:班级代码、姓名、去掉一个最低分后的平均成绩。
方法一:
得到每个学生的最低分:
create view V1 (bjdm,bnxh ,min_cj) as
select bjdm,bnxh,min(cj) from sc group by bjdm,bnxh
select sc.bjdm,sc.xsxm , avg(cj) from sc , v1
where sc.bjdm=v1.bjdm and sc.bnxh=v1.bnxh and sc.cj <> v1.min_cj
group by bjdm,bnxh
方法二:
select sc.bjdm,sc.xsxm , ( sum(cj) – min(cj) ) / count(*) from sc
group by bjdm , bnxh
8输出一张表格:每门科目对应一条记录,包括字段:科目、去掉一个最低分后的平均成绩。
方法一:
得到每门课的最低分:
create view V1 ( km, min_cj) as
select km,min(cj) from sc group by km
select sc.km , avg(cj) from sc , v1
where sc.km=v1.km and sc.cj <> v1.min_cj
group by sc.km
方法二:
select km , (sum( cj) – min(cj) )/count(*) from sc
group by km
补充9:输出表格:每门科目对应一条记录,包括字段:科目、去掉一个最低分和最高分后的平均成绩。
select km , (sum( cj) – min(cj) – max(cj) )/count(*) from sc
group by km
五 数据库存放着某高校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. 找出各次四级和六级考试的参考人数和平均成绩(报了名但没有参加考试的不作统计)
select year , month,level ,count(*) , avg(grade)
group by year,month , level
2. 找出各次四级考试中平均分最高的系科(报了名但没有参加考试的不作统计)。
A: Select sdept from s , e where s.sno=e.sno
Where level=4
Group by sdept
Having avg(grade)>=ALL(
Select avg(grade) from s , e where s.sno=e.sno where level=4 Group by sdept )
B: Select top 1 sdept from s , e where s.sno=e.sno
Where level=4
Group by sdept
Order by (avg(grade) desc
3. 找出已经通过英语六级考试的学生的学号、姓名和性别(用连接方法做)
select s.sno,sname,ssex from s,e
where s.sno=e.sno and level=6 and grade>=60
4. 找出在同一年中四、六级考试都参加了的学生的学号
1) select sno from E
where (level=4 and grade>=60) or level=6
group by year having count(*)>=2
2) select sno from E X where level=4 and grade>=60 and exists (
select * from E Y where Y.sno=X.sno and year=X.year and level=6 )
5. 找出只参加一次考试就通过了英语六级考试的学生的学号
select sno from E
where level=6
group by sno
having count(*)=1 错,想想为何?
1) select sno from E
where level=6
group by sno
having count(*)=1 and max(grade)>=60
2) select sno from E where level=6 and grade>=60 and sno in (
select sno from E where level=6 group by sno having count(*)=1)
6. 找出至今没有通过英语四级考试的学生的学号(应包括至今还没有参加过考试或者是参加了但还没有通过两种)
select sno from E where level=4
group by sno
having max(grade)<60
Union
Select sno from s 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
(Select count(*) from E where level=6 and grade>=60 group by year , month )
补充:查询每门课程的及格率。
1 得到每门课的选修人数 2 得到每门课及格人数 3每门课的及格人数/每门课选修人数
1create view v_all( cno , cnt) as select cno , count(*) from sc group by cno
2create view v_pass( cno , cnt_pass) as select cno , count(*) from sc where grade>=60
group by cno
3 select cno , cnt_pass*1.0 / cnt from v_all , v_pass where v_all.cno = v_pass.cno
--转自