排序:ORDER BY
用法:……order by 字段名1 [asc](默认升序),字段名2 [desc](降序);
注意:1.当不使用order by对查询结果进行排序时,默认顺序为添加数据的顺序;
2.列的别名在order by中可以使用,在where中不可用;
分页:LIMIT
用法:……limit [偏移地址](从0开始,0可省略),[每页显示数据条数];
意义:可以减少数据表的网络传输量,提高查询效率。
#第五章 排序与分页
#1.查询员工的姓名和部门号和年薪,按照年薪降序,姓名升序显示
select last_name,department_id,salary*12 annual_salary
from employees
order by last_name,annual_salary desc;
#2.选择工资不在8000到17000的员工的姓名和工资,按照工资降序,显示第21到40位置的数据
select last_name,salary
from employees
where salary not between 8000 and 17000
order by salary desc
LIMIT 20,20;
#3.查询邮箱中包含e的员工信息,并先按照邮箱的字节数降序,再按照部门号升序
select last_name,email
from employees
where email like '%e%'
order by length(email) desc,department_id;