首页数据库数据库练习 SQL、关系代数练习

数据库练习 SQL、关系代数练习

编程之家2023-10-22199次浏览

本篇文章给大家谈谈数据库练习,以及SQL、关系代数练习对应的知识点,文章可能有点长,但是希望大家可以阅读完,增长自己的知识,最重要的是希望对各位有所帮助,可以解决了您的问题,不要忘了收藏本站喔。

数据库练习 SQL、关系代数练习

数据库练习题

创建一个选择查询,按系别统计各自男女学生的平均年龄

SELECT

系,

性别,

AVG(年龄) AS平均年龄

FROM

数据库练习 SQL、关系代数练习

GROUP BY

系,

性别

上面是用 SQL的处理方法。

楼主要求“最好不是SQL啊”,不知道楼主希望是用什么?

数据库练习 SQL、关系代数练习

一步一步操作?

那要说明是什么数据库啊.

如果是 Access的话,操作顺序是这样的:

1、创建一个查询

2、在《显示表》窗口里面,把那个表选中,按添加按钮。

3、关闭《显示表》窗口,进入设计窗口。

4、在表中,双击系,性别,年龄这3列,加到下面的列表中。

5、在下面列表的地方,鼠标右键,在弹出窗口,选择“汇总”

6、修改年龄下面的 Group By,变成“计算”注意,不是“总计”。

7、运行查询。

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)

我是从“上海全鼎软件学院”毕业的————————

SQL、关系代数练习

一、

--1.建立学生表Student,其中以学号为主码,以系号为外码,姓名不可以为空值,性别取值为“男”或“女”

CREATETABLEStudent

(

Sno INT,

Name VARCHAR(20) NOTNULL,

Gender CHAR(2),

Class VARCHAR(20),

Major VARCHAR(20),

Dno CHAR(2)

CONSTRAINTpk_Student_SnoPRIMARYKEY(Sno),

CONSTRAINTfk_Student_DnoFOREIGNKEY(Dno)REFERENCESDept(Dno),

CONSTRAINTchk_Student_GenderCHECK(GenderIN('男','女'))

)

--2.检索与“王强”在同一个班级的学生的学号、姓名、性别

SELECTSno,Name,Gender

FROMStudent

WHEREClass=(SELECTClassFROMStudentWHEREName='王强')

--3.检索管理学院没有选修“电子商务”课程的学生的学号、姓名、班级、专业

SELECTS1.Sno,Name,Class,Major

FROMStudentS1

JOINDeptD

ONS1.Dno=D.Dno

WHEREDname='管理学院'

ANDSnoNOTIN

( SELECTS2.SnoFROMStudyS2

JOINCourseC

ONS2.Cno=C.Cno

WHERESubject='电子商务')

--4.检索选修了“C语言”且C语言考试成绩高于这门课平均成绩的学生的学号、姓名

SELECTS1.Sno,Name

FROMStudentS1

JOINStudyS2

ONS1.Sno=S2.Sno

JOINCourseC1

ONS2.Cno=C1.Cno

WHERESubject='C语言'

ANDScore>( SELECTAVG(Score)

FROMStudyS3

WHERES3.Cno=S2.Cno)

--5.在院系表(Dept)中插入一条记录,系号为06,系名为“物理系”,电话为8470660

INSERTINTODempVALUES('06','物理系','84706600')

--6.将“数据库原理”课程的学时改为64,学分改为4

UPDATECourse

SETTime=64,

Credit=4

WHERESubject='数据库原理'

--7.建立由班级、学号、姓名、课程号、课程名、学分、成绩七个数据项组成的视图

CREATEVIEWVW_SC

AS

SELECTClass,Sno,Name,Cno,Subject,Credit,Score

FROMStudentS1

JOINStudyS2

ONS1.Sno=S2.Sno

JOINCourseC

ONS2.Cno=C.Cno

二、

2.

好了,关于数据库练习和SQL、关系代数练习的问题到这里结束啦,希望可以解决您的问题哈!

服务器被ddos怎么办?服务器被ddos攻击了怎么办人力资源管理系统数据库设计(如何规划,设计人力资源管理信息系统)