SELECT employee_id,last_name, salary
FROM employees
ORDER BY salary DESC; # 降序排列
# ORDER BY salary ASC; # 升序排列,结果差不多,就不展示了,默认升序排列
运行结果如下所示:
还可以使用列的别名来排序,具体如下所示:
SELECT employee_id,last_name, salary * 12 AS annual_sal
FROM employees
ORDER BY annual_sal; # 利用别名来排序
运行结果如下所示:
注:只能在ORDER BY中使用,不能在WHERE中使用,而且WHERE语句一定要在ORDER BY之前。
原因:先执行SELECT语句以及ORDER BY语句之间的语句,再执行SELECT语句,最后执行ORDER BY语句
SELECT employee_id,last_name, salary
FROM employees
WHERE department_id IN (10,20) # 部门为10,20的人,工资按升序排列
ORDER BY salary;
运行结果如下所示:
SELECT employee_id,last_name, salary
FROM employees
ORDER BY salary DESC, employee_id; # salary降序,employee_id 升序
运行结果如下所示: