Syntax
ORDER BY { column-Name | ColumnPosition | Expression }
[ ASC | DESC ]
[ NULLS FIRST | NULLS LAST ]
[ , column-Name | ColumnPosition | Expression
[ ASC | DESC ]
[ NULLS FIRST | NULLS LAST ]
] *
知識點
order by 后面可以接列號(數字)、列名、別名、表達式、函數、分組函數
order by 對空值的處理,DESC空值在前,ASC空值在后;
order by子句中可以不含select中的列;
- 當使用
select distinct或group by時,order by 不能使用select之外的列;
order by 只能放最后,不能放集合操作的中間;
- 集合操作后,不接
order by時按第一列進行升序排序(union all除外);
- 集合操作后的列名為第一個
select的內容,order by 只能選第一個select中的內容進行操作
select job, avg(sal) "Average Salary"
from emp
group by job
order by "Average Salary" DESC;