ORDER BY子句
ORDER BY子句的語法為:
SELECT column1, SUM(column2)
FROM "list-of-tables"
ORDER BY "column-list" [ASC | DESC];
[ ] = optional
ORDER BY是一個可選的子句,它允許你根據指定要order by的列來以上升或者下降的順序來顯示查詢的結果。例如:
ASC = Ascending Order – 這個是缺省的
DESC = Descending Order
下面舉個例子:
SELECT employee_id, dept, name, age, salary
FROM employee_info
WHERE dept = 'Sales'
ORDER BY salary;
這條SQL語句將從employee_info表中列dept等於'Sales'選擇employee_id,、dept、 name、 age和 salary,並且根據他們的salary按升序的順序來列出檢索結果。
如果你想對多列排序的話,那麼在列與列之間要加上逗號,比如 :
SELECT employee_id, dept, name, age, salary
FROM employee_info
WHERE dept = 'Sales'
ORDER BY salary, age DESC;