排序查询SQL语句笔记如下:
- #查询员工信息,要求工资从高到低(从低到高)排序。
- SELECT * FROM `employees` ORDER BY salary DESC;
- SELECT * FROM `employees` ORDER BY salary ASC;#不写ASC默认升序
- #查询部门编号>=90的员工信息,按入职时间的先后进行排序
- SELECT * FROM `employees` WHERE department_id>=90 ORDER BY `hiredate` ASC;
- #按年薪的高低显示员工的信息和年薪【按表达式排序】
- SELECT *,salary*12*(1+IFNULL(`commission_pct`,0)) AS 年薪 FROM `employees` ORDER BY 年薪 DESC;
- #按姓名的长度显示员工的姓名和工资【按函数排序】
- SELECT LENGTH(last_name) AS 姓名长度, `last_name`,`salary` FROM `employees` ORDER BY LENGTH(`last_name`) ASC;
- #查询员工信息,要求先按工资排序,再按员工编号排序【按多个字段排序】
- SELECT * FROM `employees` ORDER BY salary ASC,`employee_id` ASC;
- #排序查询测试题
- SELECT `last_name`,`department_id`,salary*12*(1+IFNULL(`commission_pct`,0)) AS 年薪 FROM `employees` ORDER BY 年薪 DESC,LENGTH(`last_name`) ASC;
- SELECT `last_name`,`salary` FROM `employees` WHERE `salary`<8000 OR `salary`>17000 ORDER BY `salary` DESC;
- SELECT * FROM `employees` WHERE `email` LIKE '%e%' ORDER BY LENGTH(`email`) DESC,`department_id` ASC;