设为首页 加入收藏

TOP

Oracle 经典面试题(附代码及详细注释)(一)
2019-09-17 18:28:55 】 浏览:43
Tags: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) t3
where 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 2
2005-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) t1
join
(select count(decode(shengfu, '', 1)) "负", rq from tmp group by rq) t2
on 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 (
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Mac-搭建Hadoop集群 下一篇redis通用命令

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目