DataWhale组队学习:SQL-TASK06

这次的任务是综合练习:
直接上题目和代码~


image.png
#创建Employee 表,包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id。
USE shop;
CREATE TABLE `Employee` (
  `Id` INTEGER,
  `name` varchar(10),
  `salary` varchar(10),
  `department_id` INTEGER ,
  PRIMARY KEY (`Id`)
);
# 插入数据
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);

#创建Department 表,包含公司所有部门的信息。
CREATE TABLE `Department` (
  `Id` INTEGER,
  `name` varchar(10),
  PRIMARY KEY (`Id`)
);
# 插入数据
insert into `Department`
(`Id`,`name`)
values
(1, 'IT'),
(2, 'Sales');

#编写一个 SQL 查询,找出每个部门工资最高的员工
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;

结果


image.png

image.png

image.png
#创建seat 表
CREATE TABLE `seat` (
  `Id` INTEGER,
  `student` varchar(10),
  PRIMARY KEY (`Id`)
);
# 插入数据
insert into `seat`
(`Id`,`student`)
values
(1, 'Abbot'),
(2, 'Doris'),
(3, 'Emerson'),
(4, 'Green'),
(5, 'Jeames')
;

#其实改变的不是student,而是id,其中我使用了标量子查询添加了新的一列max_id, 通过取余数判断奇数和偶数,并使用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;

结果


image.png
image.png

image.png
 #创建seat 表
CREATE TABLE `score` (
  `Id` INTEGER,
  `score` float,
  PRIMARY KEY (`Id`)
);
# 插入数据
insert into `score`
(`Id`,`score`)
values
(1, 3.50),
(2, 3.65),
(3, 4.00),
(4, 3.85),
(5, 4.00),
(6, 3.65)
;
##这个应该考察的是dense_rank()
select score,
        dense_rank() over (order by score desc) as rank1
    from score;

结果:


image.png
image.png
## 创建ConsecutiveNums表   
CREATE TABLE `ConsecutiveNums` (
  `Id` INTEGER,
  `num` INTEGER,
  PRIMARY KEY (`Id`)
);
# 插入数据
insert into `ConsecutiveNums`
(`Id`,`num`)
values
(1, 1),
(2, 1),
(3, 1),
(4, 2),
(5, 1),
(6, 2),
(7, 2)
;
## 解题思路:利用移动窗口 、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;

结果:


image.png
image.png

image.png

解题思路是:利用case when 判断 root/inner/leaf 三种类型,其中需要用到关联子查询以及谓词in

## 树节点,根据节点标识给出节点的类型
## 创建tree表
CREATE TABLE `tree` (
  `Id` INTEGER,
  `p_id` INTEGER,
  PRIMARY KEY (`Id`)
);
# 插入数据
insert into `tree`
(`Id`,`p_id`)
values
(1, null),
(2, 1),
(3, 1),
(4, 2),
(5, 2)
;
## 解题思路是:利用case when 判断 root/inner/leaf 三种类型,其中需要用到关联子查询以及谓词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;

结果:


image.png
image.png

利用谓词和窗口函数

## 创建employee_2表
CREATE TABLE `employee_2` (
  `Id` INTEGER,
  `Name` varchar(10),
  `Department` varchar(10),
  `ManagerId` INTEGER,
  PRIMARY KEY (`Id`)
);
# 插入数据
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个下属的主管。利用谓词和窗口函数
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);

结果:


image.png
image.png

考察的是rank()

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

结果:


image.png
image.png
## 创建question表
CREATE TABLE `question` (
  `uid` INTEGER,
  `action` varchar(10),
  `question_id` INTEGER,
  `answer_id` INTEGER,
  `q_num` integer,
  `timestamp` integer
);
# 插入数据
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
## 正常计算即可
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

结果:


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

结果:


image.png

拓展:若要选出,前n个,就最后一行的rank <= n即可。

image.png
## 创建point_2d表
CREATE TABLE `point_2d` (
  `x` INTEGER,
  `y` INTEGER
);
# 插入数据
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,当序号不同时,把列取出来。它自然就是笛卡尔积。

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 205,386评论 6 479
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,939评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,851评论 0 341
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,953评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,971评论 5 369
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,784评论 1 283
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,126评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,765评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,148评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,744评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,858评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,479评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,080评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,053评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,278评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,245评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,590评论 2 343

推荐阅读更多精彩内容