提交 2221abbc 编写于 作者: M Mars Liu

add max salary and lost id

上级 239a6a42
{
"keywords": [],
"node_id": "oceanbase-4977bf7616784bfbac4bf88c8a064375",
"title": "数据分析"
"title": "数据分析",
"export": [
"max_salary.json",
"lost_id.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "lost_id.md"
}
\ No newline at end of file
# 查找缺失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
{
"type": "code_options",
"author": "刘鑫",
"source": "max_salary.md"
}
\ No newline at end of file
# 寻找工资最高的人
有这样一个表:
```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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册