引入:
select * from employees;
语法:
select 查询列表 from 表【where 筛选条件】 order by 排序列表 【asc|desc】
特点:
1.asc升序,desc降序
如果不写,默认是升序
2.order by 子句中可以支持单个字段、多个字段、表达式、函数、别名
3.order by 子句一般是放在查询语句的最后面,limit子句除外
案例1:查询员工信息,要求工资从高到低排序
SELECT * FROM employees ORDER BY salary DESC;
从低到高,asc可以省略
SELECT * FROM employees ORDER BY salary ASC;
案例2:查询部门编号>=90的员工信息,按入职时间的先后进行排序
SELECT * FROM employees WHERE department_id >= 90 ORDER BY hiredate ASC ;
案例3:【按表达式排序】按年薪的高低显示员工的信息和年薪
SELECT *, salary * 12 * (1+ IFNULL(commission_pct, 0)) FROM employees ORDER BY salary * 12 * (1+ IFNULL(commission_pct, 0)) DESC ;
案例4:【按别名排序】按年薪的高低显示员工的信息和年薪
SELECT *, salary * 12 * (1+ IFNULL(commission_pct, 0)) 年薪 FROM employees ORDER BY 年薪 DESC ;
案例5:【按函数排序】按姓名的长度显示员工的姓名和工资
SELECT LENGTH(last_name) 字节长度, last_name, salary FROM employees ORDER BY LENGTH(last_name) DESC;
案例6:【按多个字段排序】查询员工信息,要求先按工资升序,再按员工编号降序
SELECT * FROM employees ORDER BY salary ASC, employee_id DESC ;