【SQL】5.test练习题及答案(2)

18、假设使用如下命令建立了一个grade表:

create table grade(low number(3,0),up number(3),rank char(1));

insert into grade values(90,100,’A’);

insert into grade values(80,89,’B’);

insert into grade values(70,79,’C’);

insert into grade values(60,69,’D’);

insert into grade values(0,59,’E’);

现查询所有同学的Sno、Cno和rank列

select sc.sno,sc.cno,g.rank from score sc
join grade g
on/where sc.degree between g.low and g.up;
题目18

19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录

select a.* from score a where a.cno='3-105' and a.degree>all(select degree
from score b where b.sno='109' and b.cno='3-105');
题目19

20、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录

select * from score sc where degree<(select max(degree) from score) 
group by sno having count(sno)>1
order by degree;
题目20

21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录
<b>同19题</b>
22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列

select sno,sname,sbirthday from student where year(sbirthday)=
(select year(sbirthday) from student where sno='108');
题目22

23、查询“张旭“教师任课的学生成绩

select sc.sno,sc.degree from score sc
join(teacher t,course c)
on sc.cno=c.cno and t.tno=c.tno
where t.tname='张旭';
题目23

24、查询选修某课程的同学人数多于5人的教师姓名

 select t.tname from teacher t join (course c,score sc)
 on (t.tno=c.tno and c.cno=sc.cno)
 group by sc.cno having count(sc.cno) >5;
题目24

25、查询95033班和95031班全体学生的记录

select * from student
where class in ('95033','95031');
题目25

26、查询存在有85分以上成绩的课程Cno

解法一
select distinct cno from score where degree > 85;
解法二
 select cno from score group by cno having max(degree)>85;
题目26

27、查询出“计算机系“教师所教课程的成绩表

解法一
select sc.* from score sc
join (teacher t,course c)
on sc.cno = c.cno and t.tno=c.tno
where t.depart = '计算机系';
解法二
select * from score where cno in
(select c.cno from course c join teacher t
on c.tno=t.tno and t.depart='计算机系');
题目27

28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof

 select tname,prof from teacher 
where depart='计算机系' and prof not in 
(select prof from teacher where depart='电子工程系');
题目28

29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。

select cno,sno,degree from score
where degree >=(select min(degree) from score where cno='3-245')
<!-- any(select degree)-->
and cno='3-105'
order by degree desc;
题目29

30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.

select cno,sno,degree from score
where degree >=(select max(degree) from score where cno='3-245')
and cno='3-105'
order by degree desc;
题目30

31、查询所有教师和同学的name、sex和birthday.

<!-- as 可以去掉-->
select sname as name,ssex as sex,sbirthday as birthday from student
union
select tname as name,tsex as sex,tbirthday as birthday from teacher;
题目31

32、查询所有“女”教师和“女”同学的name、sex和birthday.

<!-- as 可以去掉-->
select sname as name,ssex as sex,sbirthday as birthday from student
where ssex = '女'
union
select tname as name,tsex as sex,tbirthday as birthday from teacher
where tsex = '女';
题目32

33、查询成绩比该课程平均成绩低的同学的成绩表。

select sc.* from score sc 
where degree <(select avg(degree) from score sc2 
where sc.cno=sc2.cno);
题目33

34、查询所有任课教师的Tname和Depart.

select t.tname,t.depart from teacher t 
join course c on t.tno=c.tno;
题目34

35、查询所有未讲课的教师的Tname和Depart.

关联字段名相同可用using,等同于 a.字段1=b.字段1
select tname,depart from teacher t 
left join course c using(tno) where isnull (c.tno);
题目35

36、查询至少有2名男生的班号。

select class from student s where ssex='男'
group by class having count(ssex)>1;
题目36

37、查询Student表中不姓“王”的同学记录

select * from student where sname not like '王%';
题目37

38、查询Student表中每个学生的姓名和年龄

 select sname,(year(now())-year(sbirthday)) age from student;
题目38

39、查询Student表中最大和最小的Sbirthday日期值

select sname,sbirthday from student where sbirthday = (select max(sbirthday) from student) 
union
select sname,sbirthday from student where sbirthday = (select min(sbirthday) 
from student);
题目39

40、以班号和年龄从大到小的顺序查询Student表中的全部记录

select class,(year(now())-year(sbirthday)) age from student 
order by class desc,age desc;
题目40

41、查询“男”教师及其所上的课程

select t.tname,c.cname from teacher t 
join course c using(tno) where t.tsex='男';
题目41

42、查询最高分同学的Sno、Cno和Degree列

select sno,cno,degree from score 
where degree = (select max(degree) from score);
题目42

43、查询和“李军”同性别的所有同学的Sname

select sname from student
where ssex = (select ssex from student where sname='李军');
select s.sname from student s
where ssex = (select s2.ssex from student s2 where s2.sname='李军');
题目43

44、查询和“李军”同性别并同班的同学Sname.

select sname from student 
where ssex=(select ssex from student where sname='李军')
and class=(select class from student where sname='李军');
select a.sname from student a
where ssex=(select b.ssex from student b where b.sname='李军')
and class=(select c.class from student c where c.sname='李军');
题目44

45、查询所有选修“计算机导论”课程的“男”同学的成绩表

解法1
select * from score 
where sno in(select sno from student where ssex='男') 
and cno=(select cno from course where cname='计算机导论');
解法2
select sc.* from score sc
join (student s,course c) using(sno,cno)
where s.ssex='男'and c.cname='计算机导论';
题目45
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,393评论 5 467
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,790评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,391评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,703评论 1 270
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,613评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,003评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,507评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,158评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,300评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,256评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,274评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,984评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,569评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,662评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,899评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,268评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,840评论 2 339

推荐阅读更多精彩内容

  • 1、 查询Student表中的所有记录的Sname、Ssex和Class列。 2、 查询教师所有的单位即不重复的D...
    一曈阅读 1,125评论 0 0
  • 表结构: 题目:1、 查询Student表中的所有记录的Sname、Ssex和Class列。2、 查询教师所有的单...
    danr小胖阅读 518评论 0 0
  • 原文:https://www.cnblogs.com/aqxss/p/6563625.html 一、设有一数据库,...
    名门翘楚C阅读 1,080评论 0 0
  • 从网上找了一些习题,下面结合网友的建表SQL语句及workbench的使用,将过程分享一下 第一步:建test数据...
    一曈阅读 1,198评论 0 0
  • 拥有一颗高傲的心 却想成为一个闲云野鹤的诗人 想在人前人后落落大方 却不喜觥筹交错阔论高谈 昨天我想给你打下一片江...
    素絢阅读 188评论 0 2