分享Oracle的四道经典面试题,值得收藏

原创 波波说运维 2019-07-20 00:02:00
https://www.toutiao.com/i6713901660919300621/

概述

今天主要整理了4道Oracle 经典面试题,与大家分享学习。下面一起看看详细的介绍吧


第一题

1、测试数据

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');commit;

select * from test;

2、需求:

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

3、实现:

/*根据表格可以分析出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;

第二题

1、测试数据

create table tmp(rq varchar2(10),shengfu varchar2(5));

insert into tmp values('2019-07-09','胜');insert into tmp values('2019-07-09','胜');insert into tmp values('2019-07-09','负');insert into tmp values('2019-07-09','负');insert into tmp values('2019-07-10','胜');insert into tmp values('2019-07-10','负');insert into tmp values('2019-07-10','负');commit;

select * from tmp;

2、需求:

如果要生成下列结果, 该如何写sql语句?

3、实现:

--使用分组--按日期分组,用conut函数计算次数select rq "日期", count(decode(shengfu, '胜', 1)) "胜", count(decode(shengfu, '负', 1)) "负" from tmp group by rq order by rq;

--使用连表--这道题本身就需要分组,不建议使用连表做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;

第三题

1、测试数据

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);commit;select * from STUDENT_SCORE;

2、需求:

有一张表,里面有3个字段:语文,数学,英语。其中有3条记录分别表示语文70分,数学80分,英语58分,请用一条sql语句查询出这三条记录并按以下条件显示出来(并写出您的思路): 大于或等于80表示优秀,大于或等于60表示及格,小于60分表示不及格。

3、实现

--使用分组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;

--在分组的基础上使用 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;

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

1、测试数据:

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;

2、需求:

从前面两个表中取出如下图所列格式数据。

3、实现:

--使用分组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;

[转帖]分享Oracle的四道经典面试题,值得收藏的更多相关文章

  1. [转帖]超详细的PostgreSQL体系结构总结,值得收藏

    超详细的PostgreSQL体系结构总结,值得收藏 https://www.toutiao.com/i6715390855772897800/ 原创 波波说运维 2019-07-26 00:03:00 ...

  2. Linux大牛分享的7道经典面试题和秒收 offer 的技巧

    笔者其实没有想到去面试,只是在智联上更新了一下简历,就陆陆续续接到很多猎头的邮件和电话,闲话少说,下面就分享给大家Linuxer的面试经历: 首先,猎头或者公司人资会把公司的介绍及岗位要求发到你邮箱( ...

  3. Oracle数据库的经典问题 snapshot too old是什么原因引起的

    Oracle数据库的经典问题 snapshot too old是什么原因引起的 ORACLE经典错误求解:ORA-1555错误(Snapshot too old ) - ... 书上说是因为the r ...

  4. Oracle编程入门经典 第12章 事务处理和并发控制

    目录 12.1          什么是事务处理... 1 12.2          事务处理控制语句... 1 12.2.1       COMMIT处理... 2 12.2.2       RO ...

  5. Oracle编程入门经典 第11章 过程、函数和程序包

    目录 11.1          优势和利益... 1 11.2          过程... 1 11.2.1       语法... 2 11.2.2       建立或者替换... 2 11.2 ...

  6. [转帖]Oracle数据库lob大对象数据类型字段总结,值得收藏

    Oracle数据库lob大对象数据类型字段总结,值得收藏 原创 波波说运维 2019-07-11 00:02:00 https://www.toutiao.com/i67108943269703357 ...

  7. 李洪强iOS经典面试题153- 补充

    李洪强iOS经典面试题153- 补充   补充 有空就来解决几个问题,已经懒癌晚期没救了... UML 统一建模语言(UML,UnifiedModelingLanguage)是面向对象软件的标准化建模 ...

  8. 李洪强iOS经典面试题141-报错警告调试

    李洪强iOS经典面试题141-报错警告调试   报错警告调试 你在实际开发中,有哪些手机架构与性能调试经验 刚接手公司的旧项目时,模块特别多,而且几乎所有的代码都写在控制器里面,比如UI控件代码.网络 ...

  9. iOS经典面试题(转)

    iOS经典面试题   前言 写这篇文章的目的是因为前两天同学想应聘iOS开发,从网上找了iOS面试题和答案让我帮忙看看.我扫了一眼,倒吸了一口冷气,仔细一看,气的发抖.整篇题目30多个没有一个答案是对 ...

随机推荐

  1. TensorFlow(九):卷积神经网络

    一:传统神经网络存在的问题 权值太多,计算量太大 权值太多,需要大量样本进行训练 二:卷积神经网络(CNN) CNN通过感受野和权值共享减少了神经网络需要训练的参数个数. 三:池化 四:卷积操作 五: ...

  2. 【概率论】5-4:泊松分布(The Poisson Distribution)

    title: [概率论]5-4:泊松分布(The Poisson Distribution) categories: - Mathematic - Probability keywords: - Po ...

  3. Codeforces 1182D Complete Mirror [树哈希]

    Codeforces 中考考完之后第一个AC,纪念一下qwq 思路 简单理解一下题之后就可以发现其实就是要求一个点,使得把它提为根之后整棵树显得非常对称. 很容易想到树哈希来判结构是否相同,而且由于只 ...

  4. 使用Spring Ehcache二级缓存优化查询性能

    最近在对系统进行优化的时候,发现有些查询查询效率比较慢,耗时比较长, 通过压测发现,主要耗费的性能 消耗在 查询数据库,查询redis 数据库:连接池有限,且单个查询不能消耗大量的连接池,占用大量IO ...

  5. NOIP1998提高组 题解报告

    T1 进制位 题目大意:自己看吧 首先让我们来看两个引理: 如果有解,则进制一定为\(n - 1\) 如果有解,则字母一定表示\(0\) 至 \(n - 1\) 的数 证明如下: 因为有 \(n - ...

  6. Arts打卡第8周

    Algorithm.主要是为了编程训练和学习. 每周至少做一个 leetcode 的算法题(先从Easy开始,然后再Medium,最后才Hard). 进行编程训练,如果不训练你看再多的算法书,你依然不 ...

  7. 5分钟学会如何创建spring boot项目

    上一篇博客说了如何创建spring boot项目,但是有些同学会觉得有点麻烦,有没有什么快速学会能快速创建spring boot项目的方法,答案是肯定的.接下来我们就一起来快速创建一个spring b ...

  8. Kettle中ETL的效率优化

    ETL效率优化 开启数据库日志记录及性能监控 如果我们想要优化一个ETL(KTR或者KJB)的性能,我们首先需要知道的就是它的瓶颈在哪里.而这些信息一般只能在ETL运行的步骤度量中看到,并且是不会持久 ...

  9. [RK3399] /bin/sh: 1: lz4c: not found

    CPU:RK3399 系统:Android 8.1 第一次在 RK3399 编译 Android 8.1 的系统,编译内核过程中报错如下: /bin/sh: : lz4c: not found mak ...

  10. Echarts常用API(echarts和echartsInstance)

    一.echarts上的方法 一般在项目中引入echarts之后,可以获得一个全局的echarts对象. 1.下面是几个比较常用的echarts方法 echarts.init() 创建一个echarts ...