ORDER BY子句語法:

SELECT column1, SUM(column2)
FROM "list-of-tables"
ORDER BY "column-list" [ASC | DESC];

[ ] =選項

ORDER BY是一個選項子句,當你以排序方式來顯示資料(正向由小到大、反向由大到小)以指定的欄位資料做排序。

ASC = 正向- 預設值
DESC = 反向排序

For example:

SELECT employee_id, dept, name, age, salary
FROM employee_info
WHERE dept = 'Sales'
ORDER BY salary;

以上列出公司代號、公司名、銷售員人名、年紀及薪資,而條件是公司名為 Sales ,顯示順序以薪資多少排序(由小到大,為預設值)

如果要以多個欄位做排序,要以逗號做分隔,如下:

SELECT employee_id, dept, name, age, salary
FROM employee_info
WHERE dept = 'Sales'
ORDER BY salary, age DESC;

Use these tables for the exercises
items_ordered
customers

Review Exercises

1) 列出 customers 表中,所有銷售員的姓lastname、名firstname、城市city資料。以姓(lastname)做正向排序。

2) 同第一題,但以反向排序

3)列出 items_ordered表中每一項物品 item、售價 price,條件為售價要大於 10.00. 顯示以售價正向排序。

解答