查看完整版本: 【SQL Server】每日一题,做对加10—50学分(題2)

Stone 2008-6-13 09:17

【SQL Server】每日一题,做对加10—50学分(題2)

[font=SimSun][size=10.5pt]设有学生选修课程数据库,[/size][/font][font=SimSun][size=10.5pt] [/size][/font]
[font=SimSun][size=10.5pt]学生表(学号,姓名,年龄,性别,所在系,地址,出生日期) [/size][/font][font=SimSun][size=10.5pt][/size][/font]
[font=SimSun][size=10.5pt]选课表(学号,课程号,成绩) [/size][/font][font=SimSun][size=10.5pt][/size][/font]
[font=SimSun][size=10.5pt]课程表(课程号,课程名称,教师姓名)[/size][/font][font=SimSun][size=10.5pt] [/size][/font]
[font=SimSun][size=10.5pt]用[/size][/font][font=SimSun][size=10.5pt]SQL[/size][/font][font=SimSun][size=10.5pt]语言查询下列问题:[/size][/font][font=SimSun][size=10.5pt] [/size][/font]
[font=SimSun][size=10.5pt]1[/size][/font][font=SimSun][size=10.5pt])李老师所教的课程号、课程名称。[/size][/font][font=SimSun][size=10.5pt] [/size][/font]
[font=SimSun][size=10.5pt]2[/size][/font][font=SimSun][size=10.5pt])年龄大于[/size][/font][font=SimSun][size=10.5pt]23[/size][/font][font=SimSun][size=10.5pt]岁的女学生的学号和姓名。[/size][/font][font=SimSun][size=10.5pt] [/size][/font]
[font=SimSun][size=10.5pt]3[/size][/font][font=SimSun][size=10.5pt])[/size][/font][font=SimSun][size=10.5pt]“[/size][/font][font=SimSun][size=10.5pt]李小波[/size][/font][font=SimSun][size=10.5pt]”[/size][/font][font=SimSun][size=10.5pt]所选修的全部课程名称。[/size][/font][font=SimSun][size=10.5pt] [/size][/font]
[font=SimSun][size=10.5pt]4[/size][/font][font=SimSun][size=10.5pt])所有成绩都在[/size][/font][font=SimSun][size=10.5pt]80[/size][/font][font=SimSun][size=10.5pt]分以上的学生姓名及所在系。[/size][/font][font=SimSun][size=10.5pt] [/size][/font]
[font=SimSun][size=10.5pt]5[/size][/font][font=SimSun][size=10.5pt])没有选修[/size][/font][font=SimSun][size=10.5pt]“[/size][/font][font=SimSun][size=10.5pt]操作系统[/size][/font][font=SimSun][size=10.5pt]”[/size][/font][font=SimSun][size=10.5pt]课的学生姓名。[/size][/font][font=SimSun][size=10.5pt] [/size][/font]
[font=SimSun][size=10.5pt]6[/size][/font][font=SimSun][size=10.5pt])英语成绩比数学成绩好的学生。[/size][/font][font=SimSun][size=10.5pt] [/size][/font]
[font=SimSun][size=10.5pt]7[/size][/font][font=SimSun][size=10.5pt])至少选修两门以上课程的学生姓名、性别。[/size][/font][font=SimSun][size=10.5pt] [/size][/font]
[font=SimSun][size=10.5pt]8[/size][/font][font=SimSun][size=10.5pt])选修了李老师所讲课程的学生人数。[/size][/font][font=SimSun][size=10.5pt] [/size][/font]
[font=SimSun][size=10.5pt]9[/size][/font][font=SimSun][size=10.5pt])没有选修李老师所讲课程的学生。[/size][/font][font=SimSun][size=10.5pt][/size][/font]
[font=SimSun][size=10.5pt]10[/size][/font][font=SimSun][size=10.5pt])[/size][/font][font=SimSun][size=10.5pt]“[/size][/font][font=SimSun][size=10.5pt]操作系统[/size][/font][font=SimSun][size=10.5pt]”[/size][/font][font=SimSun][size=10.5pt]课程得最高分的学生姓名、性别、所在系。[/size][/font][font=SimSun][size=10.5pt][/size][/font]

cwctt 2008-6-13 10:51

先在沙发上躺会~马上跟出答案

飘飘飞雪 2008-6-13 14:34

用select  格式的语句表  select    查询信息   from     表名
where   子句   
and    order  by或 group   by  
或having  子句
记住一定要链接表呀

飘飘飞雪 2008-6-13 14:35

给我加10分

奋斗的狼 2008-6-16 21:39

select tno,tname
   from t
   where tname = '李老师‘

   
select sno,sname
      from s
      where age>23 and sex='女'

select tname
from t
where tno in(
select tno
from c
where sno in(
select sname
from s
where
sname ='李小波'));


select sname ,mager
from s
where sno in (
select sno
from c
where grade>80);


select snme
from s
where not exists(
select tname
from t
where tname ='操作系统'

slect sname
from s
where sno in (
select sno
from c
where cno in(
select cno
from t
where tname ='英语'and grad >any(
select tname
from t
tname='数学'))

第六题忘记在写了 接着下面的

select sname ,sex
from s
where sno in(
select sno
from c
group by sno having count(*)>2;


select count(distinct sno )
from c
where cno in (
select cno
from t
where tname='李老师'


select sno
from c
where cno in(
select cno
from t
where tname='李老师'is null);

select sname,sex,mager
from s
where sno in(
select sno
from c
where cno in (
select cno
from t
where tname='操作系统'and grade>all));

Stone 2008-6-17 08:18

1)select 课程号, 课程名称

from 课程表

where  教师姓名='李老师'



2)

select 学号, 姓名 from 学生表

where (性别='女') and (年龄>23)



3)

select 课程名称 from 课程表

where 课程号 in

(select 选课表.课程号 from 选课表,学生表

where  (选课表.学号=学生表.学号) and (学生表.姓名='李小波'))



4)

select 姓名, 所在系 from 学生表 where  学号 in

(select distinct 学号from 选课表 where  成绩 >= 80)



5)没有选修“操作系统”课的学生姓名。

select distinct 学生表.学号, 姓名

from 学生表, 选课表, 课程表

where (选课表.学号=学生表.学号) and (选课表.课程号=课程表.课程号)

       and (课程名称 <>'操作系统')

                                       

6)英语成绩比数学成绩好的学生。

create function 课程成绩(@课程名 nchar(255), @学号 char(6))  

returns numeric as  

begin

  declare @i numeric

  select @i=成绩 from [选课表], [课程表]

  where (学号 = @学号) and ([选课表].[课程号] =[课程表].[课程号]) and ([课程名称] = @课程名)

  return @i

end
select 学号, 姓名,

  英语成绩= dbo.课程成绩('英语',学号), 数学成绩= dbo.课程成绩('数学',学号)

  from 学生信息表

  where dbo.课程成绩('英语',学号)>dbo.课程成绩('数学',学号)



7)至少选修两门以上课程的学生姓名、性别。

select [姓名], [性别] from [学生表]

where [学号] in

(SELECT [学号] FROM [选课表]

group by [学号] having count([学号])>1)



8)选修了李老师所讲课程的学生人数。

select count(学号)

from 选课表, 课程表

where (选课表.课程号=课程表.课程号) and (教师姓名='李老师')



9)没有选修李老师所讲课程的学生,

select distinct 学生表.学号, 学生表.姓名

from 学生表, 选课表, 课程表

where (选课表.学号=学生表.学号) and (选课表.课程号=课程表.课程号)

and (教师姓名<>'李老师')



10)“操作系统”课程得最高分的学生姓名、性别、所在系。

select top 1 学生表.学号, 姓名, 所在系

from 学生表, 选课表, 课程表

where (选课表.学号=学生表.学号) and (选课表.课程号=课程表.课程号)

       and (课程名称 = '操作系统')

order by 成绩 desc
页: [1]
查看完整版本: 【SQL Server】每日一题,做对加10—50学分(題2)