sql查询题目及答案

2022-07-21 07:54:26   第一文档网     [ 字体: ] [ 阅读: ] [ 文档下载 ]
说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。下载word有问题请添加QQ:admin处理,感谢您的支持与谅解。点击这里给我发消息

#第一文档网# 导语】以下是®第一文档网的小编为您整理的《sql查询题目及答案》,欢迎阅读!
题目,答案,查询,sql



1、查询所有数学系学生的信息 --select * from s where ='数学' 2、查询李老师所教的课程号、课程名

--select 课程号,课程名 from c where 教师 like '%' 3、查询年龄大于20岁的女同学的学号和姓名。

--select 学号,姓名 from s where year(getdate())-year(出生日期)+1>20 and 性别='' 4、查询学号为‘H0301’所选修的全部课程成绩。 --select 成绩 from sc where 学号= 'H0301'

5、查询平均成绩都在80分以上的学生学号及平均成绩。

--select 学号,AVG(成绩) from sc group by 学号 having AVG(成绩)>=80 6、查询至少有6人选修的课程号。

--select 课程号 from sc group by 课程号 having count(*)>6 7、查询C02号课程得最高分的学生的学号

--select 学号 from sc where 课程号='c02' and 成绩=(select max(成绩) from sc where 课程='c02')

8、查询学号为’ J0101’的学生选修的课程号和课程名

--select 课程号,课程名 from c,sc where 学号 ='j0101' and c.课程号=sc.课程号 9、‘李小波’所选修的全部课程名称。

--Select c.课程名 from s,c,sc where s.学号=sc.学号 and c.课程号=sc.课程号 and 姓名='小波'

10、所有成绩都在70分以上的学生姓名及所在系。

--select 姓名, from s,sc where s.学号=sc.学号 group by 学号 having min(成绩)>=70 11英语成绩比数学成绩好的学生

--select sc2.学号 from c c1,c c2,sc sc1,sc sc2 where c1.课程名='英语' --and c2.课程名='数学' and sc1.成绩>sc2.成绩 and sc1.学号=sc2.学号 --and c1.课程号=sc1.课程号 and c2.课程号=sc2.课程号 12、至少选修了两门课及以上的学生的姓名和性别 select 姓名,性别 from s,sc

--where s.学号=sc.学号 group by 学号 having count(*)>=2 13、选修了李老师所讲课程的学生人数

--select count(*) from C,sc where 教师 like '%' and c.课程号=sc.课程号 group by sc.课程

14、‘操作系统’课程得最高分的学生的姓名、性别、所在系 --select 姓名,性别, from s,sc --where s.学号=sc.学号 and 成绩=

--(select max(成绩) from c,sc where sc.课程号=c.课程号 and 课程名='操作系统') 15、显示所有课程的选修情况。

--select * from c left join sc on c.课程号=sc.课程号

16、取出没有选修‘操作系统’课程的学生姓名和年龄

select 姓名,(year(getdate())-year(出生日期))as 年龄 from s,c,sc where sc.学号=s.学号 and c.课程号=sc.课程号 and 课程号 not in (select 课程号 from c where 课程名='操作系统')

17、没有选修李老师所讲课程的学生

--select 学号 from sc where 课程号 not in (select 课程号 from c where 教师 like '%') 18、取出选修了全部课程的学生姓名,性别。






Select s.姓名,s.性别 from s where not exists (select * from c where not exists(select * from sc Where sc.学号=s.学号 and sc.课程号 = c.课程号))

19、检索至少选修课程“数据结构”和“C语言”的学生学号。 use studentcourse

select sc.学号 ,c.课程名 from c,sc where c.课程名 = '数据结构' and c.课程号 = sc.课程号 and 学号 in ( select sc.学号 from sc ,c

where c.课程名 = 'C语言' and c.课程号 = sc.课程号) 20、检索学习课程号为C02的学生学号与姓名。

--select s.学号,姓名 from sc,s where 课程号='c02' and s.学号=sc.学号 21、检索选修课程号为C01C02的学生学号,姓名和所在系 --use studentcourse

--select s.学号, s.姓名,s. from s,sc,c where sc.课程号 ='C02' or sc.课程号 = 'C01' --and sc.课程号 = c.课程号 and s.学号 = sc.学号 group by s.学号, s.姓名,s. 22、检索至少选修课程号为C01C03的学生姓名。 --select s.姓名

--from sc,s where sc.学号=s.学号 and 课程号='c01' and s.学号 --in (select 学号

--from sc where 课程号='c03') 23、检索每个学生的年龄。

--select year(getdate())-year(出生日期) as 年龄 from s

24SSTUDENT_NAMEBIRTH_YEAR

select 姓名 as STUDENT_NAME,year(出生日期) as BIRTH_YEAR from s 25、向学生选课数据表SC中插入一个元组(S0404C0690 insert into sc values ('S0403','C06',90)

26、把课程名为VB的成绩从学生选课数据表SC中删除

delete from sC where 课程号 IN (SELECT 课程号 FROM C WHERE 课程名='VB') 27、把女同学的成绩提高10% --update sc

--set 成绩 = 成绩*1.1 where 学号 in (select 学号 from s where 性别='') 28、列出选修课程超过3门的学生姓名及选修门数。 select S.姓名,count(*) from S,SC,C where S.学号 =SC.学号 and SC.课程号=C.课程号

group by S.学号,S.姓名 having count(*)>3 29、求选修了各课程的学生的人数。

select 课程号,count(*) as 人数 from SC group by 课程号

30、在学生选课数据表SC中,求选修课程C01的学生的学号和得分,并将结果按分数降序排序。

select 学号,成绩 from sc where 课程号='c01' order by 成绩 desc 31、查找每个同学的学号及选修课程的平均成绩情况。 --select sc.学号,avg(sc.成绩) as 平均成绩 from sc,c --where sc.课程号 = c.课程号 --group by sc.学号

32、列出学生所有可能的选课情况。






--select * from c cross JOIN s

33、列出每个同学的学号、姓名及选修课程的平均成绩情况,没有选修的同学也列出。 select s.学号,s.姓名, avg(成绩) from sc right join s on sc.学号=s.学号 34、列出每个同学的学号及选修课程号,没有选修的同学也列出 --select s.学号,课程号 from sc right join s on s.学号 = sc.学号 --group by s.学号,课程号 order by s.学号,课程号

35、如果学号为J0404的学生的成绩少于90,则加上10分。

--update sc set 成绩 = 成绩 + 10 where 学号 = 'J0404' and 成绩 < 90 36、将成绩最低的学生的成绩加上10分。

--update sc set 成绩=成绩+10 where sc.成绩=(select min(成绩)from sc) 37、将前3名成绩最高的学生的成绩减去10分。

--update sc set 成绩=成绩 - 10 where 成绩 in (select top 3 成绩 from sc order by 成绩 DESC)

38、将前10%成绩最低的学生的成绩减去5分。 update SC set 成绩 = 成绩 - 5 where 成绩 in (select top 10 percent 成绩 from SC order by 成绩) 39、检索至少有两名男生选修的课程名。

--select 课程名 from s,c,sc where 性别='' and s.学号=sc.学号 and sc.课程号=c.课程号 group by 课程名 having count(*) >=2

40、检索学生基本信息表S中不姓“王”的学生记录。 --select * from s where 姓名 not like '%'

41、检索和“李军”同性别并同班的同学的姓名。 select ,姓名,性别 from s where 性别 in (select 性别 from s where 姓名 = '李军')

and in (select from s where 姓名 = '李军') 42、统计被学生选修的课程门数。

--select count(distinct 课程号) 课程门数 from sc group by 课程号 43、求选修C04课程的学生的平均年龄。

--select avg(year(getdate())-year(出生日期))as 平均年龄 from s,sc,c --where sc.课程号='C04'and s.学号=sc.学号 and sc.课程号=c.课程号 44、求刘老师所授课程的每门课程的学生平均成绩。 --select sc.课程号,avg(成绩) from sc,c

--where 教师 like '%' and sc.课程号 = c.课程号 group by sc.课程号 45、统计每门课程的学生选修人数(超过10人的课程才统计)

----要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同, ----按课程号升序排列。

--select 课程号,count(*)as 总人数 from sc --group by 课程号 --having count(*)>10

--order by 总人数 desc,课程号




本文来源:https://www.dywdw.cn/9dd51b03eff9aef8941e06ed.html

相关推荐
推荐阅读