数据库练习题,谁能帮我做下这些数据库题目。谢谢。
本篇文章给大家谈谈数据库练习题,以及谁能帮我做下这些数据库题目。谢谢。对应的知识点,文章可能有点长,但是希望大家可以阅读完,增长自己的知识,最重要的是希望对各位有所帮助,可以解决了您的问题,不要忘了收藏本站喔。
SQL练习题
一学生–课程数据库
1查询 7号课程没有考试成绩的学生学号
select sno from sc where cno=’7’ and grade is not 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)
我是从“上海全鼎软件学院”毕业的————————
谁能帮我做下这些数据库题目。谢谢。
###某学校数据库练习
#三个表:学生表: edu_students班级表: edu_class系表: edu_department
##(1)找出所有姓李的学生,并按其年龄由小到大排序
SELECT* FROM edu_students WHERE student_name LIKE'李%' ORDER BY age;
##(2)列出所有开设超过两个专业的系的名字
SELECT x.department_name FROM(
SELECT c.department_name,count(c.department_name) num
FROM edu_class c GROUP BY c.department_name) x
WHERE x.num>= 2;
##(3)列出学生数人数大于等于2的系的编号和名称
SELECT x.department_id,x.department_name FROM(
SELECT d.department_id,d.department_name,count(d.department_name) num FROM edu_students s
LEFT JOIN edu_class c ON s.class_id= c.class_id
LEFT JOIN edu_department d ON d.department_name= c.department_name
GROUP BY d.department_name) x
WHERE x.num>= 2;
##4、学校又新增加一个物理系,编号006
INSERT INTO edu_department(department_id,department_name) values('006','物理');
##5、学生张三转到化学系111班,请更新相关的表
UPDATE edu_students SET class_id= 111 WHERE student_name='张三';
数据库基础与应用课后习题答案
第
1
章练习题答案
一、选择题
题号
1
2
3
4
5
6
7
8
9
10
答案
B
A
A
D
C
B
B
B
A
A
二、填空题
1
、数据定义、数据操纵
2
、文件系统阶段、数据库系统阶段
3
、属性、字段、元组、记录
4
、关键字
5
、域
6
、属性、属性到域的映象
7
、关系、关系
8
、投影
9
、选择运算、投影运算
10
、连接运算、自然连接
三、简答题
1
、答:数据库(
DataBase
)是被长期存放在计算机内、有组织的、可以表现为多种形式的
可共享的数据集合。
数据库管理系统(
DataBase Management System
,简称
DBMS
)是计算机系统软件,它的职
能是有效地组织和存储数据、
获取和管理数据,
接受和完成用户提出的访问数据的各种请求。
数据库系统是指拥有数据库技术支持的计算机系统,
它可以实现有组织地、
动态地存储大量
相关数据,提供数据处理和信息资源共享服务。
数据库系统是指在计算机系统中引入数据库后的系统,
一般由数据库、
数据库管理系统
(及
其开发工具)
、应用系统、数据库管理员和用户构成。
2
、答:关系是一张二维表,每个关系有一个关系名。在计算机中,一个关系可以存储为一
个文件。在
Visual FoxPro
中,一个关系就是一个表文件。
元组是二维表中水平方向的行,有时也叫做一条记录。
属性是二维表中垂直方向的列,有时也叫做一个字段。
3
、答:数据库系统由四部分组成:硬件系统、系统软件、数据库应用系统和各类人员。
4
、答:常用的数据模型有层次模型、网状模型和关系模型。
层次模型的特点是:
形状象一棵倒立的树,
有且仅有一个结点无父结点,
这个结点称为根结
点,其他结点有且仅有一个父结点。
网状模型的特点是:
形状象一张网,
允许一个以上的结点无父结点,
一个结点可以有多于一
个的父结点。
关系模型的特点是:
在关系中,
数据的逻辑结构是一张二维表。
该表满足每一列中的分量是
类型相同的数据;
列的顺序可以是任意的;
行的顺序可以是任意的;
表中的分量是不可再分
割的最小数据项,即表中不允许有子表;表中的任意两行不能完全相同。
层次模型对具有一对多层次关系的数据描述非常自然、
直观、
容易理解;
网状模型主要是描
述具有多对多关系的数据。
关系模型具有严格的数学理论为基础,
在描述数据时使用简单灵
活、数据独立性强等特点,而被公认为是理想的数据的组织方式。
5
、答:不是,只有具备以下几个要求的二维表才被称为是关系:
(
1
)每一列中的分量是类型相同的数据;
(
2
)列的顺序可以是任意的;
附上出处链接:http://wenku.baidu.com/link?url=9i5NhjeDRi7MJL9lUqfAjg93SBGB9gzF92MagBRQD_SG2oXNthO3WjMdKZAaGOIE_sHRKyDsmITdFcgdoX20wNzmYAppzdU5-DeBlHr9Hcm
关于数据库练习题,谁能帮我做下这些数据库题目。谢谢。的介绍到此结束,希望对大家有所帮助。