SELECT 查询列表
FROM 表名
【WHERE 筛选条件】
ORDER BY 排序列表 【ASC}DESC】;#支持多个排序条件,以逗号分隔
1、ASC :升序,如果不写默认升序
DESC:降序
2、排序列表 支持 单个字段、多个字段、函数、表达式、别名;
3、ORDER BY 的位置一般放在查询语句的最后(除LIMIT语句之外);
SELECT * FROM employees;
SELECT * FROM employees ORDER BY salary DESC;
案例:查询部门编号>=90的员工信息,并按员工编号降序
SELECT *
FROM employees
WHERE department_id>=90
ORDER BY employee_id DESC;
案例:查询员工信息 按年薪降序
SELECT *,salary*12*(1+IFNULL(commission_pct,0))
FROM employees
ORDER BY salary*12*(1+IFNULL(commission_pct,0)) DESC;
案例:查询员工信息 按年薪升序
SELECT *,salary*12*(1+IFNULL(commission_pct,0)) 年薪
FROM employees
ORDER BY 年薪 ASC;
案例:查询员工名,并且按名字的长度降序
SELECT LENGTH(last_name),last_name
FROM employees
ORDER BY LENGTH(last_name) DESC;
案例:查询员工信息,要求先按工资降序,再按employee_id升序
SELECT *
FROM employees
ORDER BY salary DESC,employee_id ASC;
1、查询员工的姓名和部门号和年薪,按年薪升序、按姓名升序
SELECT last_name,department_id,salary*12*(1+IFNULL(commission_pct,0)) AS 年薪
FROM employees
ORDER BY 年薪 ASC,last_name ASC;
2.选择工资不在8000到17000的员工的姓名和工资,按工资降序
SELECT last_name,salary,salary*12*(1+IFNULL(commission_pct,0)) 年薪
FROM employees
WHERE salary NOT BETWEEN 8000 AND 17000
ORDER BY 年薪 ASC,salary DESC;
3.查询邮箱中包含e的员工信息,并先按邮箱的字节数降序,再按部门号升序
SELECT *
FROM employees
WHERE email LIKE '%e%'
ORDER BY LENGTH(email) DESC,department_id;