'分享Oracle的四道經典面試題,值得收藏'

人生第一份工作 收藏 SQL 波波說運維 2019-08-27
"

概述

今天主要整理了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;
"

概述

今天主要整理了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;
分享Oracle的四道經典面試題,值得收藏

2、需求:

根據以上的表寫出一條查詢語句,查詢結果如下:

"

概述

今天主要整理了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;
分享Oracle的四道經典面試題,值得收藏

2、需求:

根據以上的表寫出一條查詢語句,查詢結果如下:

分享Oracle的四道經典面試題,值得收藏

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) t3
where 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;
"

概述

今天主要整理了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;
分享Oracle的四道經典面試題,值得收藏

2、需求:

根據以上的表寫出一條查詢語句,查詢結果如下:

分享Oracle的四道經典面試題,值得收藏

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) t3
where 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;
分享Oracle的四道經典面試題,值得收藏

2、需求:

如果要生成下列結果, 該如何寫sql語句?

"

概述

今天主要整理了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;
分享Oracle的四道經典面試題,值得收藏

2、需求:

根據以上的表寫出一條查詢語句,查詢結果如下:

分享Oracle的四道經典面試題,值得收藏

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) t3
where 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;
分享Oracle的四道經典面試題,值得收藏

2、需求:

如果要生成下列結果, 該如何寫sql語句?

分享Oracle的四道經典面試題,值得收藏

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) t1
join
(select count(decode(shengfu, '負', 1)) "負", rq from tmp group by rq) t2
on 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;
"

概述

今天主要整理了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;
分享Oracle的四道經典面試題,值得收藏

2、需求:

根據以上的表寫出一條查詢語句,查詢結果如下:

分享Oracle的四道經典面試題,值得收藏

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) t3
where 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;
分享Oracle的四道經典面試題,值得收藏

2、需求:

如果要生成下列結果, 該如何寫sql語句?

分享Oracle的四道經典面試題,值得收藏

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) t1
join
(select count(decode(shengfu, '負', 1)) "負", rq from tmp group by rq) t2
on 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;
分享Oracle的四道經典面試題,值得收藏

2、需求:

有一張表,裡面有3個字段:語文,數學,英語。其中有3條記錄分別表示語文70分,數學80分,英語58分,請用一條sql語句查詢出這三條記錄並按以下條件顯示出來(並寫出您的思路): 大於或等於80表示優秀,大於或等於60表示及格,小於60分表示不及格。

"

概述

今天主要整理了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;
分享Oracle的四道經典面試題,值得收藏

2、需求:

根據以上的表寫出一條查詢語句,查詢結果如下:

分享Oracle的四道經典面試題,值得收藏

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) t3
where 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;
分享Oracle的四道經典面試題,值得收藏

2、需求:

如果要生成下列結果, 該如何寫sql語句?

分享Oracle的四道經典面試題,值得收藏

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) t1
join
(select count(decode(shengfu, '負', 1)) "負", rq from tmp group by rq) t2
on 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;
分享Oracle的四道經典面試題,值得收藏

2、需求:

有一張表,裡面有3個字段:語文,數學,英語。其中有3條記錄分別表示語文70分,數學80分,英語58分,請用一條sql語句查詢出這三條記錄並按以下條件顯示出來(並寫出您的思路): 大於或等於80表示優秀,大於或等於60表示及格,小於60分表示不及格。

分享Oracle的四道經典面試題,值得收藏

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='語文') t1
join
(select name,score from STUDENT_SCORE where subject='數學') t2
on t1.name=t2.name
join
(select name,score from STUDENT_SCORE where subject='英語') t3
on t1.name=t3.name;

--在分組的基礎上使用 case when then esle end
select 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='語文') t1
join
(select name,score from STUDENT_SCORE where subject='數學') t2
on t1.name=t2.name
join
(select name,score from STUDENT_SCORE where subject='英語') t3
on 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;
"

概述

今天主要整理了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;
分享Oracle的四道經典面試題,值得收藏

2、需求:

根據以上的表寫出一條查詢語句,查詢結果如下:

分享Oracle的四道經典面試題,值得收藏

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) t3
where 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;
分享Oracle的四道經典面試題,值得收藏

2、需求:

如果要生成下列結果, 該如何寫sql語句?

分享Oracle的四道經典面試題,值得收藏

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) t1
join
(select count(decode(shengfu, '負', 1)) "負", rq from tmp group by rq) t2
on 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;
分享Oracle的四道經典面試題,值得收藏

2、需求:

有一張表,裡面有3個字段:語文,數學,英語。其中有3條記錄分別表示語文70分,數學80分,英語58分,請用一條sql語句查詢出這三條記錄並按以下條件顯示出來(並寫出您的思路): 大於或等於80表示優秀,大於或等於60表示及格,小於60分表示不及格。

分享Oracle的四道經典面試題,值得收藏

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='語文') t1
join
(select name,score from STUDENT_SCORE where subject='數學') t2
on t1.name=t2.name
join
(select name,score from STUDENT_SCORE where subject='英語') t3
on t1.name=t3.name;

--在分組的基礎上使用 case when then esle end
select 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='語文') t1
join
(select name,score from STUDENT_SCORE where subject='數學') t2
on t1.name=t2.name
join
(select name,score from STUDENT_SCORE where subject='英語') t3
on 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;
分享Oracle的四道經典面試題,值得收藏

2、需求:

從前面兩個表中取出如下圖所列格式數據。

"

概述

今天主要整理了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;
分享Oracle的四道經典面試題,值得收藏

2、需求:

根據以上的表寫出一條查詢語句,查詢結果如下:

分享Oracle的四道經典面試題,值得收藏

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) t3
where 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;
分享Oracle的四道經典面試題,值得收藏

2、需求:

如果要生成下列結果, 該如何寫sql語句?

分享Oracle的四道經典面試題,值得收藏

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) t1
join
(select count(decode(shengfu, '負', 1)) "負", rq from tmp group by rq) t2
on 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;
分享Oracle的四道經典面試題,值得收藏

2、需求:

有一張表,裡面有3個字段:語文,數學,英語。其中有3條記錄分別表示語文70分,數學80分,英語58分,請用一條sql語句查詢出這三條記錄並按以下條件顯示出來(並寫出您的思路): 大於或等於80表示優秀,大於或等於60表示及格,小於60分表示不及格。

分享Oracle的四道經典面試題,值得收藏

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='語文') t1
join
(select name,score from STUDENT_SCORE where subject='數學') t2
on t1.name=t2.name
join
(select name,score from STUDENT_SCORE where subject='英語') t3
on t1.name=t3.name;

--在分組的基礎上使用 case when then esle end
select 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='語文') t1
join
(select name,score from STUDENT_SCORE where subject='數學') t2
on t1.name=t2.name
join
(select name,score from STUDENT_SCORE where subject='英語') t3
on 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;
分享Oracle的四道經典面試題,值得收藏

2、需求:

從前面兩個表中取出如下圖所列格式數據。

分享Oracle的四道經典面試題,值得收藏

3、實現:

--使用分組
select deptno,
max(decode(month,'一月份',yj)) 一月份,
max(decode(month,'二月份',yj)) 二月份,
max(decode(month,'三月份',yj)) 三月份
from yj01 group by deptno
order 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)t1
join
(select y2.deptno,y1.yj from
(select yj, deptno from yj01 where month='二月份') y1 right join yjdept y2 on y1.deptno=y2.deptno)t2
on t1.deptno=t2.deptno
join
(select y2.deptno,y1.yj from
(select yj, deptno from yj01 where month='三月份') y1 right join yjdept y2 on y1.deptno=y2.deptno)t3
on t1.deptno=t3.deptno
order by t1.deptno;

這四個表都是有點難度的,值得深入學習一下。後面會分享更多devops和DBA方面的內容,感興趣的朋友可以關注一下~

"

概述

今天主要整理了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;
分享Oracle的四道經典面試題,值得收藏

2、需求:

根據以上的表寫出一條查詢語句,查詢結果如下:

分享Oracle的四道經典面試題,值得收藏

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) t3
where 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;
分享Oracle的四道經典面試題,值得收藏

2、需求:

如果要生成下列結果, 該如何寫sql語句?

分享Oracle的四道經典面試題,值得收藏

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) t1
join
(select count(decode(shengfu, '負', 1)) "負", rq from tmp group by rq) t2
on 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;
分享Oracle的四道經典面試題,值得收藏

2、需求:

有一張表,裡面有3個字段:語文,數學,英語。其中有3條記錄分別表示語文70分,數學80分,英語58分,請用一條sql語句查詢出這三條記錄並按以下條件顯示出來(並寫出您的思路): 大於或等於80表示優秀,大於或等於60表示及格,小於60分表示不及格。

分享Oracle的四道經典面試題,值得收藏

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='語文') t1
join
(select name,score from STUDENT_SCORE where subject='數學') t2
on t1.name=t2.name
join
(select name,score from STUDENT_SCORE where subject='英語') t3
on t1.name=t3.name;

--在分組的基礎上使用 case when then esle end
select 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='語文') t1
join
(select name,score from STUDENT_SCORE where subject='數學') t2
on t1.name=t2.name
join
(select name,score from STUDENT_SCORE where subject='英語') t3
on 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;
分享Oracle的四道經典面試題,值得收藏

2、需求:

從前面兩個表中取出如下圖所列格式數據。

分享Oracle的四道經典面試題,值得收藏

3、實現:

--使用分組
select deptno,
max(decode(month,'一月份',yj)) 一月份,
max(decode(month,'二月份',yj)) 二月份,
max(decode(month,'三月份',yj)) 三月份
from yj01 group by deptno
order 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)t1
join
(select y2.deptno,y1.yj from
(select yj, deptno from yj01 where month='二月份') y1 right join yjdept y2 on y1.deptno=y2.deptno)t2
on t1.deptno=t2.deptno
join
(select y2.deptno,y1.yj from
(select yj, deptno from yj01 where month='三月份') y1 right join yjdept y2 on y1.deptno=y2.deptno)t3
on t1.deptno=t3.deptno
order by t1.deptno;

這四個表都是有點難度的,值得深入學習一下。後面會分享更多devops和DBA方面的內容,感興趣的朋友可以關注一下~

分享Oracle的四道經典面試題,值得收藏


"

相關推薦

推薦中...