order_by.md 1.1 KB
Newer Older
M
having  
Mars Liu 已提交
1 2 3 4
# 排序

Joe 需要根据员工表

fix bug  
张志晨 已提交
5
```sql
M
having  
Mars Liu 已提交
6 7 8 9 10 11 12 13 14 15 16
create table employee
(
    id     serial primary key,
    name   varchar(256),
    dept   varchar(256),
    salary decimal(12, 4)
);
```

生成一份报表,首先按部门名称的字典序排序,部门内部按员工工资从高到低排列,那么这个查询应该是:

M
Mars Liu 已提交
17 18
<hr/>

F
feilong 已提交
19
点击进入[MySQL实战练习环境](https://mydev.csdn.net/product/pod/new?image=cimg-centos7-skilltreemysql&connect=auto&create=auto&utm_source=skill){target="_blank"}。
F
feilong 已提交
20

F
feilong 已提交
21 22
* `show databases;` 列出所有数据库
* `show tables;` 列出所有表
M
Mars Liu 已提交
23

M
having  
Mars Liu 已提交
24 25
## 答案

fix bug  
张志晨 已提交
26
```sql
27 28 29
select id, name, dept, salary 
from employee 
order by dept, salary desc;
M
having  
Mars Liu 已提交
30 31 32 33 34 35
```

## 选项

### A

fix bug  
张志晨 已提交
36
```sql
37 38 39
select id, name, dept, salary 
from employee 
order by dept, salary;
M
having  
Mars Liu 已提交
40 41 42 43
```

### B

fix bug  
张志晨 已提交
44
```sql
45 46 47
select id, name, dept, salary 
from employee 
order by dept desc, salary desc;
M
having  
Mars Liu 已提交
48 49 50
```

### C
fix bug  
张志晨 已提交
51
```sql
52 53 54
select id, name, dept, salary 
from employee 
order by dept and salary desc;
M
having  
Mars Liu 已提交
55 56 57 58
```

### D

fix bug  
张志晨 已提交
59
```sql
60 61 62
select id, name, dept, salary 
from employee 
order by dept, salary, id, name;
M
having  
Mars Liu 已提交
63 64 65
```

### E
fix bug  
张志晨 已提交
66
```sql
67 68 69
select id, name, dept, salary 
from employee 
order by dept, salary desc;
M
having  
Mars Liu 已提交
70 71
```