DataWhale組隊(duì)學(xué)習(xí):SQL-TASK06

這次的任務(wù)是綜合練習(xí):
直接上題目和代碼~


image.png
#創(chuàng)建Employee 表看疙,包含所有員工信息威酒,每個(gè)員工有其對應(yīng)的 Id, salary 和 department Id。
USE shop;
CREATE TABLE `Employee` (
  `Id` INTEGER,
  `name` varchar(10),
  `salary` varchar(10),
  `department_id` INTEGER ,
  PRIMARY KEY (`Id`)
);
# 插入數(shù)據(jù)
insert  into `Employee`
(`Id`,`name`,`salary`,`department_id`)
 values
 (1,'Joe','7000',1),
 (2,'Henry','8000',2),
 (3,'Sam','6000',2),
 (4,'Max','9000',1);

#創(chuàng)建Department 表搔啊,包含公司所有部門的信息。
CREATE TABLE `Department` (
  `Id` INTEGER,
  `name` varchar(10),
  PRIMARY KEY (`Id`)
);
# 插入數(shù)據(jù)
insert into `Department`
(`Id`,`name`)
values
(1, 'IT'),
(2, 'Sales');

#編寫一個(gè) SQL 查詢,找出每個(gè)部門工資最高的員工
select
*
from
(select
 *
from(
select name,
       salary,
       department_id,
       rank() over (partition by department_id order by salary desc) as rank_salary
    from Employee)table1
    where rank_salary = 1)table2
    left join
    (select * 
    from Department)table3
    on table3.Id = table2.department_id;

結(jié)果


image.png

image.png

image.png
#創(chuàng)建seat 表
CREATE TABLE `seat` (
  `Id` INTEGER,
  `student` varchar(10),
  PRIMARY KEY (`Id`)
);
# 插入數(shù)據(jù)
insert into `seat`
(`Id`,`student`)
values
(1, 'Abbot'),
(2, 'Doris'),
(3, 'Emerson'),
(4, 'Green'),
(5, 'Jeames')
;

#其實(shí)改變的不是student迅脐,而是id愁溜,其中我使用了標(biāo)量子查詢添加了新的一列max_id, 通過取余數(shù)判斷奇數(shù)和偶數(shù)疾嗅,并使用case when  then else end語句來寫條件改變id,最后再order by id就可以了~

select
    case when t1.id < t1.max_id and t1.id%2 = 1 then t1.id+1 
         when t1.id < t1.max_id and t1.id%2 = 0 then t1.id-1 
         when t1.id = t1.max_id and t1.id%2 = 1 then t1.id
         when t1.id = t1.max_id and t1.id%2 = 0 then t1.id-1
         else null
    end as id,
          student
        from (
        select *,
        (select max(id) from seat) as max_id
        from seat
        )t1
        order by id;

結(jié)果


image.png
image.png

image.png
 #創(chuàng)建seat 表
CREATE TABLE `score` (
  `Id` INTEGER,
  `score` float,
  PRIMARY KEY (`Id`)
);
# 插入數(shù)據(jù)
insert into `score`
(`Id`,`score`)
values
(1, 3.50),
(2, 3.65),
(3, 4.00),
(4, 3.85),
(5, 4.00),
(6, 3.65)
;
##這個(gè)應(yīng)該考察的是dense_rank()
select score,
        dense_rank() over (order by score desc) as rank1
    from score;

結(jié)果:


image.png
image.png
## 創(chuàng)建ConsecutiveNums表   
CREATE TABLE `ConsecutiveNums` (
  `Id` INTEGER,
  `num` INTEGER,
  PRIMARY KEY (`Id`)
);
# 插入數(shù)據(jù)
insert into `ConsecutiveNums`
(`Id`,`num`)
values
(1, 1),
(2, 1),
(3, 1),
(4, 2),
(5, 1),
(6, 2),
(7, 2)
;
## 解題思路:利用移動(dòng)窗口 祝谚、case when宪迟、count、min交惯、max
select 
    case when 
    count(num) over (order by id rows between 0 preceding and 2 following) = 3 and 
    min(num) over(order by id rows between 0 preceding and 2 following) = max(num) over(order by id rows between 0 preceding and 2 following)
    then num
    end as ConsecutiveNums
    from ConsecutiveNums;

結(jié)果:


image.png
image.png

image.png

解題思路是:利用case when 判斷 root/inner/leaf 三種類型次泽,其中需要用到關(guān)聯(lián)子查詢以及謂詞in

## 樹節(jié)點(diǎn),根據(jù)節(jié)點(diǎn)標(biāo)識給出節(jié)點(diǎn)的類型
## 創(chuàng)建tree表
CREATE TABLE `tree` (
  `Id` INTEGER,
  `p_id` INTEGER,
  PRIMARY KEY (`Id`)
);
# 插入數(shù)據(jù)
insert into `tree`
(`Id`,`p_id`)
values
(1, null),
(2, 1),
(3, 1),
(4, 2),
(5, 2)
;
## 解題思路是:利用case when 判斷 root/inner/leaf 三種類型席爽,其中需要用到關(guān)聯(lián)子查詢以及謂詞in
select 
id,
case when p_id is null then 'Root'
when p_id is not null and id in (select distinct(p_id) from tree) then 'Inner' else 'Leaf' end as tree_Type
from tree;

結(jié)果:


image.png
image.png

利用謂詞和窗口函數(shù)

## 創(chuàng)建employee_2表
CREATE TABLE `employee_2` (
  `Id` INTEGER,
  `Name` varchar(10),
  `Department` varchar(10),
  `ManagerId` INTEGER,
  PRIMARY KEY (`Id`)
);
# 插入數(shù)據(jù)
insert into `employee_2`
(`Id`,`Name`,`Department`,`ManagerId`)
values
(101, 'John', 'A', null),
(102, 'Dan', 'A', 101),
(103, 'James', 'A', 101),
(104, 'Amy', 'A', 101),
(105, 'Anne', 'A', 101),
(106, 'Ron', 'B', 101)
;

###找出有5個(gè)下屬的主管意荤。利用謂詞和窗口函數(shù)
select 
 name
 from
 employee_2
 where id in (select distinct
case when count(ManagerId) over (partition by ManagerId) = 5 then ManagerId else null end as ManagerId
from employee_2);

結(jié)果:


image.png
image.png

考察的是rank()

select score,
        rank() over (order by score desc) as rank1
    from score;

結(jié)果:


image.png
image.png
## 創(chuàng)建question表
CREATE TABLE `question` (
  `uid` INTEGER,
  `action` varchar(10),
  `question_id` INTEGER,
  `answer_id` INTEGER,
  `q_num` integer,
  `timestamp` integer
);
# 插入數(shù)據(jù)
insert into `question`
(`uid`,`action`,`question_id`,`answer_id`,`q_num`,`timestamp`)
values
(5, 'show', 285, null, 1, 123),
(5, 'answer', 285, 124124, 1, 124),
(5, 'show', 369, null, 2, 125),
(5, 'skip', 369, null, 2, 126)
;

select * from question;
image.png
## 正常計(jì)算即可
select
t1.question_id,
max(t1.answer_rate) as answer_rate
from(
select
   question_id,
   count(answer_id)/count(question_id) as answer_rate
   from question
   group by question_id)t1

結(jié)果:


image.png
image.png
select * from employee;
insert into `employee` values (5,'Janet','69000',1),(6,'Randy','85000',1);
select * from employee;  
image.png

解法如下:利用rank() over( partition )先分組排序,left join加上部門信息只锻,之后再選出rank <= 3

select 
    t2.name as department,
    t1.name,
    t1.salary
    from(
    select 
        Department_id,
        Salary,
        Name,
        rank() over (partition by Department_id order by Salary desc) as rank1
    from employee)t1
    left join 
    (select * 
    from Department)t2
    on t2.Id = t1.department_id
where t1.rank1 <= 3

結(jié)果:


image.png

拓展:若要選出玖像,前n個(gè),就最后一行的rank <= n即可齐饮。

image.png
## 創(chuàng)建point_2d表
CREATE TABLE `point_2d` (
  `x` INTEGER,
  `y` INTEGER
);
# 插入數(shù)據(jù)
insert into `point_2d`
(`x`,`y`)
values
(-1, -1),
(0,0),
(-1, -2)
;

先加入序號

select
    row_number() over(order by x) as id,
    x,
    y
    from point_2d;
image.png

用此表自身兩次select * from t1,t2捐寥,當(dāng)序號不同時(shí),把列取出來祖驱。它自然就是笛卡爾積握恳。

select
t1.id as id,
t1.x as x1,
t1.y as y1,
t2.x as x2,
t2.y as y2
from
(select
    row_number() over(order by x) as id,
    x,
    y
    from point_2d) t1, (select
    row_number() over(order by x) as id,
    x,
    y
    from point_2d) t2
 where t1.id <> t2.id;
image.png
select
t1.id as id,
t1.x as x1,
t1.y as y1,
t2.x as x2,
t2.y as y2,
MIN(SQRT(POW((t1.x-t2.x),2)+POW((t1.y-t2.y),2))) shortest
from
(select
    row_number() over(order by x) as id,
    x,
    y
    from point_2d) t1, (select
    row_number() over(order by x) as id,
    x,
    y
    from point_2d) t2
 where t1.id <> t2.id;
image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市捺僻,隨后出現(xiàn)的幾起案子乡洼,更是在濱河造成了極大的恐慌,老刑警劉巖匕坯,帶你破解...
    沈念sama閱讀 211,817評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件束昵,死亡現(xiàn)場離奇詭異,居然都是意外死亡葛峻,警方通過查閱死者的電腦和手機(jī)锹雏,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,329評論 3 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來术奖,“玉大人礁遵,你說我怎么就攤上這事匿辩。” “怎么了榛丢?”我有些...
    開封第一講書人閱讀 157,354評論 0 348
  • 文/不壞的土叔 我叫張陵铲球,是天一觀的道長。 經(jīng)常有香客問我晰赞,道長稼病,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,498評論 1 284
  • 正文 為了忘掉前任掖鱼,我火速辦了婚禮然走,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘戏挡。我一直安慰自己芍瑞,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,600評論 6 386
  • 文/花漫 我一把揭開白布褐墅。 她就那樣靜靜地躺著拆檬,像睡著了一般。 火紅的嫁衣襯著肌膚如雪妥凳。 梳的紋絲不亂的頭發(fā)上竟贯,一...
    開封第一講書人閱讀 49,829評論 1 290
  • 那天,我揣著相機(jī)與錄音逝钥,去河邊找鬼屑那。 笑死,一個(gè)胖子當(dāng)著我的面吹牛艘款,可吹牛的內(nèi)容都是我干的持际。 我是一名探鬼主播,決...
    沈念sama閱讀 38,979評論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼哗咆,長吁一口氣:“原來是場噩夢啊……” “哼蜘欲!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起岳枷,我...
    開封第一講書人閱讀 37,722評論 0 266
  • 序言:老撾萬榮一對情侶失蹤芒填,失蹤者是張志新(化名)和其女友劉穎呜叫,沒想到半個(gè)月后空繁,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,189評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡朱庆,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,519評論 2 327
  • 正文 我和宋清朗相戀三年盛泡,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片娱颊。...
    茶點(diǎn)故事閱讀 38,654評論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡傲诵,死狀恐怖凯砍,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情拴竹,我是刑警寧澤悟衩,帶...
    沈念sama閱讀 34,329評論 4 330
  • 正文 年R本政府宣布,位于F島的核電站栓拜,受9級特大地震影響座泳,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜幕与,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,940評論 3 313
  • 文/蒙蒙 一挑势、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧啦鸣,春花似錦潮饱、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,762評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至中狂,卻和暖如春缕溉,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背吃型。 一陣腳步聲響...
    開封第一講書人閱讀 31,993評論 1 266
  • 我被黑心中介騙來泰國打工证鸥, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人勤晚。 一個(gè)月前我還...
    沈念sama閱讀 46,382評論 2 360
  • 正文 我出身青樓枉层,卻偏偏與公主長得像,于是被迫代替她去往敵國和親赐写。 傳聞我的和親對象是個(gè)殘疾皇子鸟蜡,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,543評論 2 349

推薦閱讀更多精彩內(nèi)容