Oracle函数练习

select * from StudentInfo;
select * from TeacherInfo;
select * from StudentExam;
练习二:单行函数练习
1、查询所有学员从入学到今天,一共度过了多少天
select months_between(sysdate,stuJoinTime)*31 from StudentInfo;
2、查询每月2号入学的学员信息
select * from StudentInfo where extract(day from stuJoinTime)=2;
3、查询所有学员的毕业日期,假定按每个学员入学时间1年半之后将毕业。
select stuJoinTime,add_months(stuJoinTime,18) from studentinfo;
4、查询星期四入学的学员姓名,性别,年龄,班级编号
select * from StudentInfo where to_char(stuJoinTime,'d')=5;
select * from studentinfo where to_char(stuJoinTime,'day')='星期四';
5、查询‘2007-3-10’之前入学的学员信息
select * from StudentInfo where stuJoinTime<to_date('2007-3-10','yyyy-mm-dd');
6、查询所有学员姓名的长度
select length(stuName) from StudentInfo;
7、查询身份证中第9,10位为‘89’的学员信息(要求使用字符串函数)
select * from StudentInfo where substr(stuCard,9,2)='89';
8、修改班主任信息,将邮箱中的‘yahoo’替换为‘accp’
update TeacherInfo set TeacherEmail=replace(TeacherEmail,'yahoo','accp');
9、查询所有班主任的邮箱的用户名
select substr(TeacherEmail,1,instr(TeacherEmail,'@')-1) from TeacherInfo;
10、查询所有班主任的邮箱的所属网站
select substr(TeacherEmail,instr(TeacherEmail,'@')+1,instr(TeacherEmail,'.')-1-instr(TeacherEmail,'@')) from TeacherInfo;
提示:如果邮箱为qtz@yahoo.com,用户名即qtz,所属网站即yahoo。可先查找出‘@’和‘.’的下标,再截取
11、编写查询语句去掉字符串‘   爱你  要你  我  爱  你   ’中的空格
select replace('   爱你  要你  我  爱  你   ',' ') from dual;
12、计算每个学员身份证中字符‘1’出现的次数
select length(stuCard)-length(replace(stuCard,'1')) from StudentInfo;
13、求小于-58.9的最大整数
select floor(-58.9) from dual;
14、求大于78.8的最小整数
select ceil(78.8) from dual;
15、求64除以7的余数
select mod(89,7) from dual;
16、查询所有学员入学时间,要求显示格式为‘2007年03月02日’
select to_char(stuJoinTime,'yyyy"年"mm"月"dd"日"') 入学时间 from Studentinfo;
17、查询当前时间,要求显示格式为‘22时57:37’
select to_char(sysdate,'hh24"时"mi:ss') from dual;
18、查询2007年入学的学员信息
select * from StudentInfo where to_char(stuJoinTime,'yyyy')='2007';

设置语言
select to_char(sysdate,'day','NLS_DATE_LANGUAGE=AMERICAN') day from dual;

练习三:分组函数练习
1、查询所有学员的平均年龄(要求保留两位小数)
select round(avg(stuAge),2) from StudentInfo;
2、查询所有考试的总成绩
select sum(examResult) from StudentExam;
3、查询SQL考试的最低分数
select min(examResult) from StudentExam where examSubject='SQL';
4、查询Java考试成绩最高的学员姓名
select t.StuName from
(select StuName,EStuid,ExamResult from studentExam e,StudentInfo s where e.estuid=s.stuid and  examSubject='Java' order by ExamResult desc) t
where rownum=1;
5、查询学员’火云邪神’一共参加了几次考试
select count(*) from Studentexam where EstuID=(select stuID from StudentInfo where stuName='火云邪神');
6、查询各科目的平均成绩
select ExamSubject,avg(ExamResult) from StudentExam group by ExamSubject;
7、查询每个班级学员的最小年龄
select min(stuAge),SclassID from studentinfo group by sclassid;
8、查询考试不及格的人数
select count(*) from StudentExam where ExamResult<60;
9、查询各学员的总成绩,要求筛选出总成绩在140分以上的。
select EStuID,sum(examResult) from studentExam group by Estuid having sum(examResult)>140;
10、查询男女学员的平均年龄
select stuSex,avg(stuAge) from studentInfo group by stuSex;
11、查询每门功课的平均分,要求显示平均分在80分以上的
select ExamSubject,avg(ExamResult) from studentexam group by ExamSubject having avg(ExamResult)>80;
12、按班主任姓名分组,查所带班级的总成绩(假定每个班主任只带一个班级)(提示:4表连接)
select TeacherName,sum(ExamResult) from Teacherinfo t,StudentInfo s,StudentExam e,ClassInfo c
where t.teacherid=c.cteacherid
and s.sclassid=c.classid
and s.stuid=e.estuid
group by TeacherName;

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值