#进阶3:排序查询
/*
语法:
select 查询列表
from 表名
[where 筛选条件]
order by 排序列表 [asc|desc]
特点:
1、asc升序,desc降序, 如果都不写,默认升序asc
2、排序列表,可以是单个字段,多个字段,可以是表达式,可以是函数,也可以是出现过的别名
3、order by一般是放在查询语句的最后面,本质是最后将查询结果进行排序
4、只有limit子句是放在order by子句后
*/
#案例1: 查询员工信息,要求工资从高到低排序
SELECT * FROM employees ORDER BY salary DESC;
SELECT * FROM employees ORDER BY salary ASC;
#默认升序排列
SELECT * FROM employees ORDER BY salary;
#案例2:查询部门编号>=90的员工信息,要求按照入职先后顺序进行排序
SELECT * FROM employees WHERE department_id >= 90 ORDER BY hiredate;
#案例3:按年薪的高低显示员工信息和年薪【按表达式排序】
SELECT *,salary*12*(1+IFNULL(commission_pct,0)) AS "年薪"
FROM employees
ORDER BY (salary*12*(1+IFNULL(commission_pct,0))) DESC;
#案例4:按年薪的高低显示员工信息和年薪【按别名排序】
SELECT *,salary*12*(1+IFNULL(commission_pct,0)) AS "年薪"
FROM employees
ORDER BY 年薪 DESC;
#案例5:按姓名的长度,排序员工姓名和工资【按函数排序】
SELECT last_name,salary FROM employees
ORDER BY LENGTH(last_name);
#案例6:查询员工信息,要求先按工资排序,再按员工编号排序【按多个字段排序】
SELECT * FROM employees
ORDER BY salary ASC,employee_id DESC;