一、语法 执行顺序
select 查询列表--------------------------------③
from 表-------------------------------------------①
where 筛选条件------------------------------- ②
order by 排序列表 ASC/DESC;-----------④
二、特点:① ASC代表升序,DESC代表降序,如果不写默认升序;
② order by 子句中可以支持单个字段,多个字段,表达式,函数,别名;
③order by 子句一般是放在查询语句的最后边,limit子句除外;
例:1. 查询员工信息,要求工资从高到低排序
select * from employees order by salary desc;
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 年薪 DESC;
4. 查询员工的姓名和工资,按姓名的长度显示(按函数排序)
select last_name,salary from employees order by length(last_name) DESC;
5. 查询员工信息,要求先按工资升序,再按员工标号降序(按多个字段排序)
select * from employees order by salary ASC, employee_id DESC;