SQL数据库题目!!!急急急!!!

SQL数据库题目!!!急急急!!!,第1张

1)Students表:包含SNo,SName,SSex,SClass,SAge 其中SNo为主键

2)Courses表:包含CNo,CName,CGrade(开设学期),CScore(学分)、IsProfession(是否是专业课) 其中CNo为主键

3)Scores表:包含SNo,CNo,Score 其中SNo和CNo分别与Students中的SNo和Courses中的CNo有外键关系。

1)查询全班年龄在20岁至24岁之间的所有学生的信息。

select from Students where SAge>=20 and SAge<=24 (是否包含20和24,修改相关=号)

2)查询开设学期为2的所有的专业课程号、课程名称、和学分。

select CNo,CName,CScore from Courses where CGrade=2 and IsProfession=1(假设专业课是此值为1)

3)查询学号为000004学生的所有课程的成绩

select ScoresSNo,ScoresCNo,CoursesCName,ScoresScore from Scores INNER JOIN Courses ON ScoresCNo=CoursesCNo where ScoresSNo='000004'

4)查询姓名为“王明”的学生的所有及格课程的课程名称和成绩。

select ScoresSNo,ScoresCNo,CoursesCName,ScoresScore from Scores INNER Join

Courses ON ScoresCNo=CoursesCNo where ScoresSNo in (select SNo from Students where SName='王明') and ScoresScore>=60

5)查询班级为“0401”的所有学生的课程名为“SQL数据库管理”的成绩,按成绩降序的方式排列,如果成绩相同,则按照学号进行排列。

select ScoresSNo,ScoresCNo,CoursesCName,ScoresScore from Scores INNER Join

Courses ON ScoresCNo=CoursesCNo where ScoresSNo in (select SNo from Students

where SClass='0401' order by ScoresScore

你还有马甲。。。

第一个不对,更新的是学生的成绩,而不是c001得成绩

1对选修了课程号为c001的学生成绩提高10%

此题本人做的答案请检查:updata 选课表 set 成绩=成绩11 where 课程号='c001'

UPDATE 选课表 SET set 成绩=成绩11 WHERE 学生号 IN (SELECT 学生号 FROM 选课表 WHERE 课程号='c001')

2查询没有学生选修的课程名。

此题本人做的答案请检查:select 课程名 from 课程 where 课程名 not exists(select from 选课表 where 选课表课程号=课程表课程号)

SELECT 课程名 from 课程 WHERE 课程号 NOT IN (SELECT DISTINCT 课程号 FROM 选课表)

3 correct

4查询每个学生选修的门数及平均成绩。

此题本人做的答案请检查:select AVG(成绩),姓名 from 学生,选课 where 学生学生号=选课学生号 group by 姓名

SELECT COUNT(c课程号), AVG(c成绩)

FROM 学生 stu LEFT JOIN 选课 c ON stu学生号 = c学生号

GROUP BY stu学生号

5查询2003年以前借书的读者借书证号,姓名和单位。

此题本人做的答案请检查:select 借书证号,姓名,单位 from 读者 where 借书日期<=2003 and 读者借书证号=借书借书证号

SELECT 借书证号,姓名,单位 FROM 读者 WHERE IN (SELECT 借书证号 FROM 借书 WHERE 借书日期<=2003)

6查询电子系没有借书的读者姓名

此题本人做的答案请检查: select 姓名,职称 from 读者 where 职称='电子系' and 借书证号 NOT exists(select 借书证号 from 借书 where 读者借书证号=借书借书证号)

此题我一直有一个疑惑 我目前查询电子系没有借书的读者姓名 没有借书 也就是借书证号为空或NULL 那么读者也有一个借书证号 借书也有一个借书证号 2个是相对应的,

但是我没用到借书表 只有了读者表的借书证号 需要在where语句后写入:借书证号=借书借书证号 这个条件吗?

SELECT 姓名

FROM 读者 t

WHERE 单位 = '电子系' AND NOT EXISTS(SELECT 1 FROM 借书 WHERE 借书证号 = t借书证号)

7查询出至少有两名学生所选修的全部课程。

此题我做的不怎么好:select 姓名 from 学生 where 课程名 in (select 课程名,count() as 课程数 group by 姓名 having count()>=2

SELECT FROM 课程 WHERE 课程号 IN (SELECT 课程号 FROM 选课 GROUP BY 课程号 HAVING COUNT() >=2)

8

@a, @b, @c是参数, 譬如@a = '123', @b = 'c001', @c = 90

然后begin后面是执行一个update *** 作,将参数对应起来就可以了。

真个存储过程对例子要做的就是

UPDATE 选课

set 成绩=90

where 学号= 123 and 课程号='c001'

明白了么

两个难点:

1、如何让Sql Server取到字符串中的数字,比如1、2、4和11、12、4虽然都是三位数字,但前者只有个位,后者含有十位和个位,用substring肯定是不行了。

2、如果取出了数字,还要依次遍历整张表,循环取ID,并去除重复ID

总得说来第一条智能取数比较折磨人。

标记一下,看有无高人进来

--------------------------------

仔细思考了下,还是做出来了,真的很折磨人。

下面的语句全部一下执行就可以看到结果了。

其中@strIn就是需要查找的字符串,如果要查看其他数据,修改这个值就可以了。

思路是把一个字段值变成多行数据,把','变为' union all'拼接语句实现。

--建立测试变量表@T,并赋值

declare @T table(id int,strnum varchar(50))

insert into @T

select 1,'1,2,4,5,8,9'

union all

select 2,'1,3,5,6,7,8,9,11'

union all

select 3,'3,4,5,6,7'

union all

select 4,'1,5,7,8,9'

union all

select 5,'3,5,6,9,11,12'

union all

select 6,'2,3,4,5,7'

union all

select 7,'3,4,5,6,7,8'

union all

select 8,'1,2,3,4,5,6,7,8,9,10'

union all

select 9,'1,7,8,9'

--@strIn查找的字符串

declare @strIn varchar(1000)

set @strIn = '1,11,12'

declare @strtable table(strSql varchar(4000))

insert into @strtable

Select 'select '''+cast(id as varchar(3))+''' as id,'''+replace(strnum,',',''' as num Union all Select ' + ''''+cast(id as varchar(3))+''' as id, ''') + ''' as num'

from @T

declare @comtable table(strSql varchar(4000))

insert into @comtable

select 'select '''+replace(@strIn,',',''' as num Union all Select ''' )+ ''' as num'

declare @textsql varchar(4000)

set @textsql = ''

Select @textsql=@textsql+strSql+' Union all ' from @strtable

set @textsql = substring(@textsql,1,len(@textsql)-10)

declare @comsql varchar(4000)

set @comsql = ''

Select @comsql=@comsql+strSql+' Union all ' from @comtable

set @comsql = substring(@comsql,1,len(@comsql)-10)

set @textsql = 'declare @Rtexttable table(id int,num varchar(3)) insert into @Rtexttable '+@textsql+

' declare @Rcomtable table(num varchar(3)) insert into @Rcomtable '+@comsql+' select distinct id from @Rtexttable a join @Rcomtable b on anum = bnum'

exec (@textsql)

-------------------------

看了WHITE_WIN的,醍醐灌顶,豁然开朗。本人不喜欢用函数就改成存储过程了

declare @T table(id int,strnum varchar(50))

insert into @T

select 1,'1,2,4,5,8,9'

union all

select 2,'1,3,5,6,7,8,9,11'

union all

select 3,'3,4,5,6,7'

union all

select 4,'1,5,7,8,9'

union all

select 5,'3,5,6,9,11,12'

union all

select 6,'2,3,4,5,7'

union all

select 7,'3,4,5,6,7,8'

union all

select 8,'1,2,3,4,5,6,7,8,9,10'

union all

select 9,'1,7,8,9'

declare @strIn varchar(100)

set @strIn = '1,11,12'

declare @RT table (strnum varchar(30))

while charindex(',',@strIn)>0

begin

insert into @RT select left(@strIn,charindex(',',@strIn)-1)

set @strIn = substring(@strIn,charindex(',',@strIn)+1,len(@strIn))

end

If(len(@strIn)>0)

begin

insert into @RT select @strIn

end

select aid from @T a, @RT b where ','+astrnum+',' like '%,'+bstrnum+',%'

group by aid

order by aid

1.select X商品代号,分类名,数量,品牌 from商品表1 X,商品表2 Y where X.商品代号=Y.商品代号

找出商品库里面所有的商品信息

2.select 专业,count() as 专业人数 from学生 group by专业 order by专业人数 desc

找出每个专业的专业人数,并且降序排列

3.select课程.课程号,课程.课程名,count() as 选课人数 from课程,选课 where课程.课程号一选课.课程号 group by课程.课程号,课程.课程名

找出每门课程的选修人数

4

SELECT 商品代号 from 商品表1 group by 商品代号 HAVING SUM(数量) > 10

5

SELECT FROM 商品表1 WHERE 单价 > (SELECT AVG(单价) FROM 商品表1)

4Select a 课程名 a学时 a学分

From KC as a join XS_KC as b

On a课程号= b课程号

Where 课程号 in(‘101','102','302')

Go

1

select XS_QK学号,成绩 from XS_KC inner join XS_QK on XS_KC学号=XS_QK学号

where 课程号='103' order by 成绩

2

select 课程号,average(成绩) as 平均成绩 from XS_QK group by 课程号

3

select 姓名,性别,宿舍电话 from XS_KC inner join XS_QK on XS_KC学号=XS_QK学号

where 课程号='103' and 成绩<60

一 学生 – 课程数据库

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 ssno, sname,ssex from s , sc where ssno=scsno and cno = ‘7’

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

Select avg(sage) from s , sc where ssno=scsno 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 ssno , sname from s,sc where ssno=scsno and cno=’C4’

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

Select ssno ,sname from s,sc,c

where ssno=scsno and ccno=sccno 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 scsno=ssno and cno=’C2’ )

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

Select ssno ,sname from s,sc,c

where ssno=scsno and ccno=sccno 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 ssno=scsno and ccno=sccno and ssex=’女’ and cname = ‘数据库’ and grade<60

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

select cname , avg(grade) from sc , c where ccno =sccno group by sccno

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

select sname , avg(grade) from s , sc where ssno=scsno group by scsno

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 sccno=ccno and cname=’数据库’ and

Grade > (Select avg(grade) from sc , c where sccno=ccno and cname=’数据库’ ) )

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

Select sdept,ssex , avg(sage) , count() from s

Group by sdept , ssex

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

Select scsno , sname from s, sc where ssno=scsno and sdept=’JSJ’

Group by scsno Having avg(grade) =

( Select top 1 avg(grade) from sc, s where ssno=scsno and sdept=’JSJ’

group by scsno 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 ‘200251’ and ‘2002531’

Group by Kno

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

Select Kno from FP where fdate between ‘200251’ and ‘2002531’

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 ‘200251’ and ‘2002531’ 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 y1Yno ,y1Yname ,y1salary , avg( y2 salary) from YWY y1 , YWY y2

Where y1Yno<>y2Yno and y1salary < y2salary

Group by y1Yno

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 abjdm,axsxm , akm,acj , bkm,bcj , ckm,ccj from sc a , sc b , sc c

where abjdm=bbjdm and abnxh= bbnxh and bbjdm=cbjdm and bbnxh= cbnxh

and akm=’语文’ and bkm=’数学’ and ckm=’外语’

方法三:利用存储过程(略)

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 scbjdm,scxsxm ,max(cj) , avg_cj from sc , V1

where scbjdm=v1bjdm and scbnxh=V1bnxh and cj<60

group by scbjdm,scxsxm

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 scbjdm,scxsxm , avg(cj) from sc , v1

where scbjdm=v1bjdm and scbnxh=v1bnxh and sccj <> v1min_cj

group by bjdm,bnxh

方法二:

select scbjdm,scxsxm , ( 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 sckm , avg(cj) from sc , v1

where sckm=v1km and sccj <> v1min_cj

group by sckm

方法二:

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 ssno=esno

Where level=4

Group by sdept

Having avg(grade)>=ALL(

Select avg(grade) from s , e where ssno=esno where level=4 Group by sdept )

B: Select top 1 sdept from s , e where ssno=esno

Where level=4

Group by sdept

Order by (avg(grade) desc

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

select ssno,sname,ssex from s,e

where ssno=esno 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 Ysno=Xsno and year=Xyear 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数据库题目!!!急急急!!!全部的内容,包括:SQL数据库题目!!!急急急!!!、SQL的查询 考试题目求教 题目已写答案求判断!、很有意思的SQL查询题目,看看大家如何解决等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10197468.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-06
下一篇 2023-05-06

发表评论

登录后才能评论

评论列表(0条)

保存