提交 49348e55 编写于 作者: M Mars Liu

format

上级 9d53b62d
......@@ -30,25 +30,34 @@ from employee as l
### select 与 group by 不匹配
```postgresql
select id, name, dept, max(salary) from employee group by dept;
select id, name, dept, max(salary)
from employee
group by dept;
```
### group by 不对
```postgresql
select id, name, dept, max(salary) from employee group by dept, id, name;
select id, name, dept, max(salary)
from employee
group by dept, id, name;
```
### group by 不对
```postgresql
select id, name, dept, max(salary) from employee group by dept, id, name having salary = max(salary);
select id, name, dept, max(salary)
from employee
group by dept, id, name
having salary = max(salary);
```
### 结构错误
```postgresql
select id, name, dept, max(salary) from employee where salary=max(salary) group by dept;
select id, name, dept, max(salary)
from employee
where salary = max(salary)
group by dept;
```
......@@ -3,10 +3,11 @@
现有员工表
```postgresql
create table employee(
id serial primary key ,
name text,
dept text,
create table employee
(
id serial primary key,
name text,
dept text,
salary money
)
```
......@@ -16,9 +17,11 @@ create table employee(
## 答案
```postgresql
select id, name, dept, salary
select id, name, dept, salary
from employee
where salary > (select max(salary) from employee where dept='sale')
where salary > (select max(salary)
from employee
where dept = 'sale')
```
## 选项
......@@ -26,27 +29,29 @@ where salary > (select max(salary) from employee where dept='sale')
### A
```postgresql
select id, name, dept, salary
select id, name, dept, salary
from employee
where dept = 'sale'
group by dept having salary > max(salary)
group by dept
having salary > max(salary)
```
### B
```postgresql
select l.id, l.name, l.dept, l.salary
from employee as l join employee as r on l.salary > max(r.salary)
where r.dept = 'sale'
select l.id, l.name, l.dept, l.salary
from employee as l
join employee as r on l.salary > max(r.salary)
where r.dept = 'sale'
group by r.dept
```
### C
```postgresql
select id, name, dept, salary
select id, name, dept, salary
from employee
having salary > (select max(salary) from employee where dept='sale')
having salary > (select max(salary) from employee where dept = 'sale')
```
......
......@@ -18,26 +18,36 @@ create table orders
## 答案
```postgresql
select id, product_id, order_date, quantity, customer_id from orders where date = $1 offset $2 limit 100;
select id, product_id, order_date, quantity, customer_id
from orders
where date = $1
offset $2 limit 100;
```
## 选项
### 缺少 limit
### 缺少 limit
```postgresql
select id, product_id, order_date, quantity, customer_id from orders where date = $1 offset $2;
select id, product_id, order_date, quantity, customer_id
from orders
where date = $1
offset $2;
```
### 缺少 offset
### 缺少 offset
```postgresql
select id, product_id, order_date, quantity, customer_id from orders where date = $1;
select id, product_id, order_date, quantity, customer_id
from orders
where date = $1;
```
### 结构不对
```postgresql
select id, product_id, order_date, quantity, customer_id from orders where date = $1 and offset $2 and limit 100 ;
select id, product_id, order_date, quantity, customer_id
from orders
where date = $1 and
offset $2 and limit 100;
```
......@@ -18,13 +18,14 @@ create table node
## 答案
```postgresql
with recursive t(id, pid, content) as (select id, pid, content
from node
where id = $1
union all
select node.id, node.pid, node.level
from node
join t on node.id = t.pid)
with recursive t(id, pid, content) as (
select id, pid, content
from node
where id = $1
union all
select node.id, node.pid, node.level
from node
join t on node.id = t.pid)
select node.id, node.pid, content
from node
join t on node.id = t.id;
......@@ -35,13 +36,14 @@ from node
### 没有递归定义
```postgresql
with t as (select id, pid, content
from node
where id = $1
union all
select node.id, node.pid, node.level
from node
join t on node.id = t.pid)
with t as (
select id, pid, content
from node
where id = $1
union all
select node.id, node.pid, node.level
from node
join t on node.id = t.pid)
select node.id, node.pid, content
from node
join t on node.id = t.id;
......@@ -53,7 +55,7 @@ from node
select node.id, node.pid, node.content
from node
join node as p on node.pid = p.id
where id=$1;
where id = $1;
```
### 子查询无法处理递归问题
......@@ -61,5 +63,5 @@ where id=$1;
```postgresql
select node.id, node.pid, node content
from node as t
where t.pid = (select id from t where id=t.pid)
where t.pid = (select id from t where id = t.pid)
```
\ No newline at end of file
......@@ -5,11 +5,14 @@
## 答案
```postgresql
create function gen(start integer, stop integer) returns setof integer as $$
begin
for idx in start .. stop loop
return next idx;
end loop;
create function gen(start integer, stop integer)
returns setof integer as
$$
begin
for idx in start .. stop
loop
return next idx;
end loop;
end;
$$ language plpgsql;
```
......@@ -19,11 +22,13 @@ $$ language plpgsql;
### A
```postgresql
create function gen(start integer, stop integer) returns integer as $$
begin
for idx in start .. stop loop
return idx;
end loop;
create function gen(start integer, stop integer) returns integer as
$$
begin
for idx in start .. stop
loop
return idx;
end loop;
end;
$$ language plpgsql;
```
......@@ -31,11 +36,14 @@ $$ language plpgsql;
### B
```postgresql
create function gen(start integer, stop integer) returns integer as $$
begin
for idx in start .. stop loop
yield idx;
end loop;
create function gen(start integer, stop integer)
returns integer as
$$
begin
for idx in start .. stop
loop
yield idx;
end loop;
end;
$$ language plpgsql;
```
......@@ -43,12 +51,15 @@ $$ language plpgsql;
### C
```postgresql
create function gen(start integer, stop integer) returns setof integer as $$
begin
for idx in start .. stop loop
return idx;
idx += 1;
end loop;
create function gen(start integer, stop integer)
returns setof integer as
$$
begin
for idx in start .. stop
loop
return idx;
idx += 1;
end loop;
end;
$$ language plpgsql;
```
......@@ -56,11 +67,14 @@ $$ language plpgsql;
### D
```postgresql
create function gen(start integer, stop integer) returns setof integer as $$
begin
for idx in start .. stop loop
select idx;
end loop ;
create function gen(start integer, stop integer)
returns setof integer as
$$
begin
for idx in start .. stop
loop
select idx;
end loop;
end;
$$ language plpgsql;
```
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册