较难SQL语句集

以下SQL语句均在Oracle坏境下测试过:

1 为student表添加列“班级号” (10个长度定长字符串)

alter table student add class char(10);
2 将每个同学的班级号前面/后面加上“T”
update student set class=concat('T', trim(class));
update student set class=concat(trim(class), 'T');
3 删除班级号前面/后面的“T”
update student set class=substr(class, 2, length(trim(class)));
update student set class=substr(class, 0, length(trim(class))-1);
4 查询选修了课程名中包含了“语言”的课程的学生,列出学号,课程号,课程名和成绩
select sno, course.cno, cname, grade from course, sc where cname like '%语言%';
5 检索学分为5分的课程,成绩高于90分的学生的学号和姓名
select sno, sname from student where exists(select * from sc, course where sc.cno=course.cno and sno=student.sno and grade>90);
6 查询比C语言学分高的课程
select c2.cname fromCourse c1, Course c2 where c1.cname='C语言'and c2.credit>c1.credit;

7 查询平均年龄在19岁以上的系别

select sdept,avg(sage) as avgsage from Student group by sdept having avg(sage)>19;
8 查询CS系成绩最高的学生的性别
select ssex from Student, SC where Student.sno=SC.sno and sdept='CS' and grade in (select max(grade) from SC, Student where Student.sno=SC.sno and sdept='CS');
9 查询选课最少的学生
select sno, count(*) from sc group by sno having count(*) <= all(select count(*) from sc group by sno);
10 将教师列修改为非空列
alter table Course alter column teacher varchar(20) not null;
11 求每一个系的最高年龄和最低年龄
select sdept, max(sage) as maxage, min(sage) as minage from Student group by sdept;
12 检索“张三”选修的课程名及学分
select cname,credit from course where cno in (select cno from sc, student where sc.sno=student.sno and sname='张三');
select cname, credit from course where exists (select * from student ,sc where student.sno=sc.sno and student.sname='张三' and cno=course.cno);
13 列出没有学生选择的课程

select cno from Course where cno not in (select cno from SC);
select cno from course where not exists (select * from sc where cno=course.cno);

14 列出选课门数最多的学生所在系的所有学生的姓名

select sname from student where exists(select * from student s, (select sno from sc group by sno having count(*) >= all(select count(*) from sc group by sno)) maxsno where s.sno=maxsno.sno and s.sdept=student.sdept);
15 查询E系女同学的年龄比女同学的平均年龄高的学生
select sno from Student where sdept='E' and ssex='女' and sage>(select avg(sage) from Student where sdept='E' and ssex='女')
16 查询既选修了C01又选修了C02的学生
select Student.sno from Student, SC sc1, SC sc2 where sc1.sno=Student.sno and sc2.sno=Student.sno and sc1.cno='C01' and sc2.cno='C02';
17 查询不及格的门数在5门以上的学生所修的总学分。并按总学分降序排列
select sno, sum(credit) from course,sc where sc.cno=course.cno and grade>=60 and sno in (select sno from SC where grade<60 group by sno having count(*)>5) group by sno order by sum(credit) desc;
18 检索所有5学分的课程都选了的学生
Select sname from student where not exists (select * from course where credit=5 and not exists (select * from sc where sno=student.sno and cno=course.cno));
19 查询年龄高于其所在系的平均年龄的学生姓名
select sname from student where sage > (select avg(sage) from student s where student.sdept=s.sdept)
20 查询每位同学的选课中成绩最高的课程对应的学号,姓名,课程名,成绩
select student.sno,sname,cname,grade from sc,student,course where sc.sno=student.sno and sc.cno=course.cno and grade=(select max(grade) from sc where sc.sno=student.sno)  
21 为E系学生选修必修课C01
insert into SC select sno, 'C01', null from Student where sdept='E';
22 将E系,数据库课程学生的成绩加10分
update sc set grade=grade+10 where sno in (select sno from student where sdept='A') and cno=(select cno from course where cname='数据库');
23 将每位同学的最低分加10分
update sc set grade=grade+10 where grade=(select min(grade) from sc s where sc.sno=s.sno);

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
第二天 1、查询客户表,统计每个机构2000年之前开户数、2000~2005开户数(含头不含尾)、2005~2010开户数(含头不含尾)、2010之后开户数 展示字段:机构号、2000年之前开户数、2000~2005年开户数、2005~2010年开户数、2010年之后开户数 2、查询客户表,按年份统计,每年、每个机构开户数占全年开户数的占比 展示字段:年份、机构号、开户数、开户占比百分比(百分比) 3、统计所有客户的客户号、存款账户数、2011.12.31日的存款余额、2011.12存款月日均、贷款账户数、2011.12.31日的贷款余额、2011.12贷款月日均 备注:null置为0 第三天 1、统计所有客户的2011.12.31日的存款余额、存款比上日余额、存款比上月余额、、存款比上年余额 备注:存款比上日余额 = 2011.12.31日的存款余额-2011.12.30日的存款余额 存款比上月余额 = 2011.12.31日的存款余额-2011.11.30日的存款余额 存款比上年余额 = 2011.12.31日的存款余额-2010.12.31日的存款余额 只有2011.12.31这个日期可以写死,其他日期要通过2011.12.31这个日期来生成。 2、统计所有2011年存款年日均大于100的客户号、客户名称、存款账户数、2011年年日均 第四天 1、统计所有2011年存款年日均和2011年贷款年日均都大于100的客户号、存款账户数、2011年存款年日均、贷款账户数、2011年贷款年日均 2、统计所有客户的客户号、 存款标志(有存款账户的客户置为1、没存款账户的客户置为0)、 贷款标志(有贷款借据的客户置为1、没贷款借据的客户置为0)、 存款质量分类(2011年存款年日均>=10000置为优质、2011年存款年日均>=1000<10000 置为良好、2011年存款年日均=10000置为优质、2011年贷款年日均>=1000<10000 置为良好、2011年贷款年日均<1000置为普通)、 2011年贷款年日均 第五天 1、根据《事件表.xlsx》来建表,然后将excel中的数据导入到目标表中 2、根据客户表、存款信息表、事件表,统计每个客户2017年的客户号、 交易账户数(客户下有多少个账户有交易就是多少)、 当年有交易的天数(如果2017年有5天有过交易,则有交易天数为5)、 当年有交易总月数(如果2017的1、3、5月有交易,则有交易总月数为3)、 最大的月交易总金额(按月统计交易金额,存放最大的月交易金额)、 最大月交易金额的月份(按月统计交易金额,存放交易金额最大的月份)、 年总交易金额、 年交易金额排名(按客户排名,如果总交易金额为0,则不参与排名,排名置为9999)、 年总手续费、 年总手续费排名(按客户排名,如果总手续费为0,则不参与排名,排名置为9999)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值