From 2221abbcae03953bec0b65715edf4ba7c7d76e75 Mon Sep 17 00:00:00 2001 From: Mars Liu Date: Thu, 4 Nov 2021 20:48:49 +0800 Subject: [PATCH] add max salary and lost id --- .../config.json" | 6 +- .../lost_id.json" | 5 ++ .../lost_id.md" | 61 +++++++++++++++++++ .../max_salary.json" | 5 ++ .../max_salary.md" | 51 ++++++++++++++++ 5 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 "data/3.OceanBase\351\253\230\351\230\266/3.\347\274\226\347\250\213\345\222\214\346\237\245\350\257\242/3.\346\225\260\346\215\256\345\210\206\346\236\220/lost_id.json" create mode 100644 "data/3.OceanBase\351\253\230\351\230\266/3.\347\274\226\347\250\213\345\222\214\346\237\245\350\257\242/3.\346\225\260\346\215\256\345\210\206\346\236\220/lost_id.md" create mode 100644 "data/3.OceanBase\351\253\230\351\230\266/3.\347\274\226\347\250\213\345\222\214\346\237\245\350\257\242/3.\346\225\260\346\215\256\345\210\206\346\236\220/max_salary.json" create mode 100644 "data/3.OceanBase\351\253\230\351\230\266/3.\347\274\226\347\250\213\345\222\214\346\237\245\350\257\242/3.\346\225\260\346\215\256\345\210\206\346\236\220/max_salary.md" diff --git "a/data/3.OceanBase\351\253\230\351\230\266/3.\347\274\226\347\250\213\345\222\214\346\237\245\350\257\242/3.\346\225\260\346\215\256\345\210\206\346\236\220/config.json" "b/data/3.OceanBase\351\253\230\351\230\266/3.\347\274\226\347\250\213\345\222\214\346\237\245\350\257\242/3.\346\225\260\346\215\256\345\210\206\346\236\220/config.json" index 65884a3..4a5bdfa 100644 --- "a/data/3.OceanBase\351\253\230\351\230\266/3.\347\274\226\347\250\213\345\222\214\346\237\245\350\257\242/3.\346\225\260\346\215\256\345\210\206\346\236\220/config.json" +++ "b/data/3.OceanBase\351\253\230\351\230\266/3.\347\274\226\347\250\213\345\222\214\346\237\245\350\257\242/3.\346\225\260\346\215\256\345\210\206\346\236\220/config.json" @@ -1,5 +1,9 @@ { "keywords": [], "node_id": "oceanbase-4977bf7616784bfbac4bf88c8a064375", - "title": "数据分析" + "title": "数据分析", + "export": [ + "max_salary.json", + "lost_id.json" + ] } \ No newline at end of file diff --git "a/data/3.OceanBase\351\253\230\351\230\266/3.\347\274\226\347\250\213\345\222\214\346\237\245\350\257\242/3.\346\225\260\346\215\256\345\210\206\346\236\220/lost_id.json" "b/data/3.OceanBase\351\253\230\351\230\266/3.\347\274\226\347\250\213\345\222\214\346\237\245\350\257\242/3.\346\225\260\346\215\256\345\210\206\346\236\220/lost_id.json" new file mode 100644 index 0000000..8df36f0 --- /dev/null +++ "b/data/3.OceanBase\351\253\230\351\230\266/3.\347\274\226\347\250\213\345\222\214\346\237\245\350\257\242/3.\346\225\260\346\215\256\345\210\206\346\236\220/lost_id.json" @@ -0,0 +1,5 @@ +{ + "type": "code_options", + "author": "刘鑫", + "source": "lost_id.md" +} \ No newline at end of file diff --git "a/data/3.OceanBase\351\253\230\351\230\266/3.\347\274\226\347\250\213\345\222\214\346\237\245\350\257\242/3.\346\225\260\346\215\256\345\210\206\346\236\220/lost_id.md" "b/data/3.OceanBase\351\253\230\351\230\266/3.\347\274\226\347\250\213\345\222\214\346\237\245\350\257\242/3.\346\225\260\346\215\256\345\210\206\346\236\220/lost_id.md" new file mode 100644 index 0000000..9357802 --- /dev/null +++ "b/data/3.OceanBase\351\253\230\351\230\266/3.\347\274\226\347\250\213\345\222\214\346\237\245\350\257\242/3.\346\225\260\346\215\256\345\210\206\346\236\220/lost_id.md" @@ -0,0 +1,61 @@ +# 查找缺失id + +一个关系型数据库中,有一个表 list,主键是自增 id。形如: + +```sql +$ select * from list +id, ... +---------- +1 +2 +3 +... + +``` + +其中某一行被删除了(不在行首或行尾),哪一条查询能把被删除的哪一行的id找到? + +## 答案 + +```sql +select l.id +from (select id - 1 as id from list) as l + left join list as r + on l.id = r.id +where r.id is null + and l.id > 0; +``` + +## 选项 + +### 没有处理边界 + +```sql +select l.id +from (select id - 1 as id from list) as l + left join list as r + on l.id = r.id +where r.id is null; +``` + +### 没有取外连接 + +```sql +select l.id +from (select id - 1 as id from list) as l + join list as r + on l.id = r.id +where r.id is null + and l.id > 0; +``` + +### 没有取外连接 + +```sql +select l.id +from (select id - 1 as id from list) as l + join list as r + on l.id = r.id +where r.id is null + and l.id > 0; +``` \ No newline at end of file diff --git "a/data/3.OceanBase\351\253\230\351\230\266/3.\347\274\226\347\250\213\345\222\214\346\237\245\350\257\242/3.\346\225\260\346\215\256\345\210\206\346\236\220/max_salary.json" "b/data/3.OceanBase\351\253\230\351\230\266/3.\347\274\226\347\250\213\345\222\214\346\237\245\350\257\242/3.\346\225\260\346\215\256\345\210\206\346\236\220/max_salary.json" new file mode 100644 index 0000000..a704b02 --- /dev/null +++ "b/data/3.OceanBase\351\253\230\351\230\266/3.\347\274\226\347\250\213\345\222\214\346\237\245\350\257\242/3.\346\225\260\346\215\256\345\210\206\346\236\220/max_salary.json" @@ -0,0 +1,5 @@ +{ + "type": "code_options", + "author": "刘鑫", + "source": "max_salary.md" +} \ No newline at end of file diff --git "a/data/3.OceanBase\351\253\230\351\230\266/3.\347\274\226\347\250\213\345\222\214\346\237\245\350\257\242/3.\346\225\260\346\215\256\345\210\206\346\236\220/max_salary.md" "b/data/3.OceanBase\351\253\230\351\230\266/3.\347\274\226\347\250\213\345\222\214\346\237\245\350\257\242/3.\346\225\260\346\215\256\345\210\206\346\236\220/max_salary.md" new file mode 100644 index 0000000..de9aedf --- /dev/null +++ "b/data/3.OceanBase\351\253\230\351\230\266/3.\347\274\226\347\250\213\345\222\214\346\237\245\350\257\242/3.\346\225\260\346\215\256\345\210\206\346\236\220/max_salary.md" @@ -0,0 +1,51 @@ +# 寻找工资最高的人 + +有这样一个表: + +```sql +create table employee +( + id integer auto_increment primary key, + name varchar(256), + department varchar(256), + salary integer +); +``` + +下列哪个选项可以找出每个部门工资最高的人,并给出这个人的id,姓名,部门和工资信息? + +## 答案 + +```sql +select l.id, l.name, l.department, l.salary +from employee as l + join (select department, max(salary) as salary from employee group by department) as r + on l.department = r.department and l.salary = r.salary +``` + +## 选项 + +### 分组错误 + +```sql +select id, name, department, salary +from employee +group by department +having salary = max(salary); +``` + +### 结构错误 + +```sql +select id, name, department, max(salary) +from employee +group by department; +``` + +### 结构错误 + +```sql +select id, name, department, max(salary) +from employee +where salary = max(salary); +``` \ No newline at end of file -- GitLab