侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Oracle的四道经典面试题分享

数据库  /  管理员 发布于 6年前   158

前言

本文整理了4道Oracle 经典面试题,与大家分享学习。这也许是你一直期待的文章,下面话不多说了,来一起看看详细的介绍吧

第一题

create table test( id number(10) primary key, type number(10) , t_id number(10), value varchar2(6));insert into test values(100,1,1,'张三');insert into test values(200,2,1,'男');insert into test values(300,3,1,'50');insert into test values(101,1,2,'刘二');insert into test values(201,2,2,'男');insert into test values(301,3,2,'30');insert into test values(102,1,3,'刘三');insert into test values(202,2,3,'女');insert into test values(302,3,3,'10');select * from test;

代码生成表格如:

根据以上代码生成的表写出一条查询语句,查询结果如下:

姓名 性别 年龄
张三 男 50
刘二 男 30
刘三 女 10

/*根据表格可以分析出type列中1代表姓名、2代表性别、3代表年龄,而t_id中id一样的为同一个人的属性查询结果中列依次为姓名、性别、年龄,而type列决定姓名、性别、年龄*//*使用分组,先对t_id进行分组,然后用decode函数过滤数据,例:decode(type, 1, value) type=1就显示为value由于分组后select后面的列字段只能是分组的字段或者组函数,所有使用max()。同一个人的type没有重复数值所以 decode(type, 1, value)返回的值只有一个,最大值也就是这个值*/select max(decode(type, 1, value)) "姓名",  max(decode(type, 2, value)) "性别",  max(decode(type, 3, value)) "年龄" from test group by t_id;/*使用连表,通过where过滤生成3张type分别等于1(姓名)、2(性别)、3(年龄)的3张虚拟表 如:再通过where 连接条件 三张表t_id相等的为同一个人或者说同一条记录(行)*/select t1.value "姓名",t2.value "性别",t3.value "年龄" from (select value,t_id from test where type=1) t1,(select value,t_id from test where type=2) t2,(select value,t_id from test where type=3) t3where t1.t_id=t2.t_id and t1.t_id=t3.t_id;

第二题

/*2.一道SQL语句面试题,关于group by表内容:2005-05-09 胜2005-05-09 胜2005-05-09 负2005-05-09 负2005-05-10 胜2005-05-10 负2005-05-10 负如果要生成下列结果, 该如何写sql语句?   胜 负2005-05-09 2 22005-05-10 1 2------------------------------------------create table tmp(rq varchar2(10),shengfu varchar2(5));insert into tmp values('2005-05-09','胜');insert into tmp values('2005-05-09','胜');insert into tmp values('2005-05-09','负');insert into tmp values('2005-05-09','负');insert into tmp values('2005-05-10','胜');insert into tmp values('2005-05-10','负');insert into tmp values('2005-05-10','负');select * from tmp;*/--使用分组--按日期分组,用conut函数计算次数select rq "日期",  count(decode(shengfu, '胜', 1)) "胜",  count(decode(shengfu, '负', 1)) "负" from tmp group by rq order by rq;--使用连表--这道题本身就需要分组,不建议使用连表做--以下使用的是SQL1999的连表方式,语法不一样效果与第一题使用的SQL1992的一样select t1.rq,t1.胜, t2.负 from(select count(decode(shengfu, '胜', 1)) "胜", rq from tmp group by rq) t1join(select count(decode(shengfu, '负', 1)) "负", rq from tmp group by rq) t2on t1.rq=t2.rq;

第三题

/*3.生成题目所需的表create table STUDENT_SCORE( name VARCHAR2(20), subject VARCHAR2(20), score NUMBER(4,1));insert into student_score (NAME, SUBJECT, SCORE) values ('张三', '语文', 78.0);insert into student_score (NAME, SUBJECT, SCORE) values ('张三', '数学', 88.0);insert into student_score (NAME, SUBJECT, SCORE) values ('张三', '英语', 98.0);insert into student_score (NAME, SUBJECT, SCORE) values ('李四', '语文', 89.0);insert into student_score (NAME, SUBJECT, SCORE) values ('李四', '数学', 76.0);insert into student_score (NAME, SUBJECT, SCORE) values ('李四', '英语', 90.0);insert into student_score (NAME, SUBJECT, SCORE) values ('王五', '语文', 99.0);insert into student_score (NAME, SUBJECT, SCORE) values ('王五', '数学', 66.0);insert into student_score (NAME, SUBJECT, SCORE) values ('王五', '英语', 91.0);3.1得到类似下面的结果姓名 语文 数学 英语王五 89 56 89李四 xx xx xxselect * from STUDENT_SCORE;3.2有一张表,里面有3个字段:语文,数学,英语。其中有3条记录分别表示语文70分,数学80分,英语58分,请用一条sql语句查询出这三条记录并按以下条件显示出来(并写出您的思路): 大于或等于80表示优秀,大于或等于60表示及格,小于60分表示不及格。   显示格式:   语文    数学    英语   及格    优秀    不及格 ------------------------------------------*/--3.1--使用分组select name "姓名",  max(decode(subject, '语文' ,score)) "语文",  max(decode(subject, '数学' ,score)) "数学",  max(decode(subject, '英语' ,score)) 英语 from STUDENT_SCORE group by name;--使用连表select t1.name 姓名, t1.score 语文, t2.score 数学, t3.score 英语 from(select name,score from STUDENT_SCORE where subject='语文') t1join(select name,score from STUDENT_SCORE where subject='数学') t2on t1.name=t2.namejoin(select name,score from STUDENT_SCORE where subject='英语') t3on t1.name=t3.name;--3.2--在3.1的基础上使用 case when then esle endselect t.姓名,(case when t.语文>=80 then '优秀'   when t.语文>=60 then '及格'   else '不及格' end) 语文,(case when t.数学>=80 then '优秀'   when t.数学>=60 then '及格'   else '不及格' end) 数学,(case when t.英语>=80 then '优秀'   when t.英语>=60 then '及格'   else '不及格' end) 英语 from (select t1.name 姓名, t1.score 语文, t2.score 数学, t3.score 英语 from(select name,score from STUDENT_SCORE where subject='语文') t1join(select name,score from STUDENT_SCORE where subject='数学') t2on t1.name=t2.namejoin(select name,score from STUDENT_SCORE where subject='英语') t3on t1.name=t3.name) t;

第四题(这道题难度相对较高)

/*4.请用一个sql语句得出结果从table1,table2中取出如table3所列格式数据,注意提供的数据及结果不准确,只是作为一个格式向大家请教。table1月份mon 部门dep 业绩yj-------------------------------一月份  01  10一月份  02  10一月份  03  5二月份  02  8二月份  04  9三月份  03  8table2部门dep  部门名称dname--------------------------------  国内业务一部  国内业务二部  国内业务三部  国际业务部table3 (result)部门dep 一月份  二月份  三月份--------------------------------------  10  null  null  10   8  null  null  5  8  null  null  9------------------------------------------create table yj01(  month varchar2(10),  deptno number(10),  yj number(10))insert into yj01(month,deptno,yj) values('一月份',01,10);insert into yj01(month,deptno,yj) values('二月份',02,10);insert into yj01(month,deptno,yj) values('二月份',03,5);insert into yj01(month,deptno,yj) values('三月份',02,8);insert into yj01(month,deptno,yj) values('三月份',04,9);insert into yj01(month,deptno,yj) values('三月份',03,8);create table yjdept(  deptno number(10),  dname varchar2(20))insert into yjdept(deptno,dname) values(01,'国内业务一部');insert into yjdept(deptno,dname) values(02,'国内业务二部');insert into yjdept(deptno,dname) values(03,'国内业务三部');insert into yjdept(deptno,dname) values(04,'国际业务部');*/select * from yj01;select * from yjdept;--使用分组select deptno,max(decode(month,'一月份',yj)) 一月份, max(decode(month,'二月份',yj)) 二月份, max(decode(month,'三月份',yj)) 三月份 from yj01 group by deptnoorder by deptno;--这道题给出了两张表,而用分组做,使用yj01表就能做出来了,所以这道题考察的应该是连表的知识/*这两张表中有的月份有的部门业绩是空的,而用前几道题的做法,不匹配条件的值会被过滤掉,例如month=一月份的只有1部门,形成的表里deptno只有1和二月份、三月份形成的表中的deptno无法匹配而yjdept表中包含了所有部门编号deptno,这时就可以用到外连接的特性(在满足一张表的内容都显示的基础上,连接另外一张表,如果连接匹配则正常显示,连接不匹配,另外一张表补null)*/select t1.deptno, t1.yj 一月份, t2.yj 二月份, t3.yj 三月份from(select y2.deptno,y1.yj from(select yj, deptno from yj01 where month='一月份') y1 right join yjdept y2 on y1.deptno=y2.deptno)t1join(select y2.deptno,y1.yj from(select yj, deptno from yj01 where month='二月份') y1 right join yjdept y2 on y1.deptno=y2.deptno)t2on t1.deptno=t2.deptnojoin(select y2.deptno,y1.yj from(select yj, deptno from yj01 where month='三月份') y1 right join yjdept y2 on y1.deptno=y2.deptno)t3on t1.deptno=t3.deptnoorder by t1.deptno;

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对AIDI的支持。


  • 上一条:
    Oracle 实现 一个关键字 匹配多个 字段的方法
    下一条:
    Oracle备库宕机启动的完美解决方案
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客