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 65884a3a853152360a31e69d2b110e6cdad1de5a..4a5bdfa4019d070a7604220541c17d5842d28f92 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 0000000000000000000000000000000000000000..8df36f02baf224ec2b1d8ad4e7093a008f894377 --- /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 0000000000000000000000000000000000000000..93578028eee79430929f0e741205d614fa7838f1 --- /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 0000000000000000000000000000000000000000..a704b0218d1559c4fe1d4b1b959133915af6b448 --- /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 0000000000000000000000000000000000000000..de9aedf0863125a48ce987f59f02c712f60cb2df --- /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