SQL ORDER BY

摘要:本教學將示範如何使用 SQL 的 ORDER BY 子句,根據指定的條件,以遞增或遞減的順序排序結果集。

SQL ORDER BY 子句簡介

ORDER BYSELECT 語句的可選子句。 ORDER BY 子句允許您根據一個或多個排序運算式,以遞增或遞減的順序排序 SELECT 子句返回的行。

以下顯示 ORDER BY 子句的語法

SELECT 
    select_list
FROM
    table_name
ORDER BY 
    sort_expression [ASC | DESC];Code language: SQL (Structured Query Language) (sql)

在這個語法中

  • 首先,將 ORDER BY 子句放在 FROM 子句之後。資料庫將按照以下順序評估帶有 ORDER BY * 子句的 SELECT 語句:FROM > SELECT > ORDER BY
  • 其次,在 ORDER BY 子句之後指定排序運算式。排序運算式指定排序條件。
  • 第三,使用 ASC 選項按遞增順序依排序運算式排序結果集,並使用 DESC 按遞減順序依排序運算式排序結果集。

請注意,如果您沒有使用 ASCDESCORDER BY 子句預設會使用 ASC 選項。

ORDER BY 子句也允許您依多個運算式排序結果集。在這種情況下,您需要使用逗號分隔兩個排序運算式

SELECT 
    select_list
FROM
    table_name
ORDER BY 
    sort_expression_1 [ASC | DESC],
    sort_expression_2 [ASC | DESC];Code language: CSS (css)

在這個語法中,ORDER BY 子句首先依 sort_expression_1 排序結果集,然後依 sort_expression_2 排序已排序的結果集。

請注意,如果您未指定 ORDER BY 子句,SELECT 語句將不會排序結果集。這表示結果集中的行沒有特定的順序。

SQL ORDER BY 子句範例

我們將使用範例資料庫中的 employees 表格進行示範。

employees_table

1) 使用 SQL ORDER BY 子句排序單一欄位的值範例

以下 SELECT 語句返回 employees 表格中 employee id、first name、last name、hire date 和 salary 欄位的資料

SELECT
	employee_id,
	first_name,
	last_name,
	hire_date,
	salary
FROM
	employees;Code language: SQL (Structured Query Language) (sql)

試試看

+-------------+-------------+-------------+------------+----------+
| employee_id | first_name  | last_name   | hire_date  | salary   |
+-------------+-------------+-------------+------------+----------+
|         100 | Steven      | King        | 1987-06-17 | 24000.00 |
|         101 | Neena       | Kochhar     | 1989-09-21 | 17000.00 |
|         102 | Lex         | De Haan     | 1993-01-13 | 17000.00 |
|         103 | Alexander   | Hunold      | 1990-01-03 |  9000.00 |
|         104 | Bruce       | Ernst       | 1991-05-21 |  6000.00 |
|         105 | David       | Austin      | 1997-06-25 |  4800.00 |
|         106 | Valli       | Pataballa   | 1998-02-05 |  4800.00 |
|         107 | Diana       | Lorentz     | 1999-02-07 |  4200.00 |
|         108 | Nancy       | Greenberg   | 1994-08-17 | 12000.00 |
|         109 | Daniel      | Faviet      | 1994-08-16 |  9000.00 |
|         110 | John        | Chen        | 1997-09-28 |  8200.00 |
...

您可以從輸出清楚地看到,這些行沒有任何順序。

以下範例使用 ORDER BY 子句,按名字的字母順序排序員工

SELECT
	employee_id,
	first_name,
	last_name,
	hire_date,
	salary
FROM
	employees
ORDER BY
	first_name;Code language: SQL (Structured Query Language) (sql)

試試看

+-------------+-------------+-------------+------------+----------+
| employee_id | first_name  | last_name   | hire_date  | salary   |
+-------------+-------------+-------------+------------+----------+
|         121 | Adam        | Fripp       | 1997-04-10 |  8200.00 |
|         115 | Alexander   | Khoo        | 1995-05-18 |  3100.00 |
|         103 | Alexander   | Hunold      | 1990-01-03 |  9000.00 |
|         193 | Britney     | Everett     | 1997-03-03 |  3900.00 |
|         104 | Bruce       | Ernst       | 1991-05-21 |  6000.00 |
|         179 | Charles     | Johnson     | 2000-01-04 |  6200.00 |
|         109 | Daniel      | Faviet      | 1994-08-16 |  9000.00 |
|         105 | David       | Austin      | 1997-06-25 |  4800.00 |
|         114 | Den         | Raphaely    | 1994-12-07 | 11000.00 |
|         107 | Diana       | Lorentz     | 1999-02-07 |  4200.00 |
...

ORDER BYfirst_name 欄位中的值排序這些行。

2) 使用 SQL ORDER BY 子句排序多個欄位的值範例

以下範例使用 ORDER BY 子句,先按名字的遞增順序排序員工,然後按姓氏的遞減順序排序

SELECT
	employee_id,
	first_name,
	last_name,
	hire_date,
	salary
FROM
	employees
ORDER BY
	first_name,
	last_name DESC;Code language: SQL (Structured Query Language) (sql)

試試看

+-------------+-------------+-------------+------------+----------+
| employee_id | first_name  | last_name   | hire_date  | salary   |
+-------------+-------------+-------------+------------+----------+
|         121 | Adam        | Fripp       | 1997-04-10 |  8200.00 |
|         115 | Alexander   | Khoo        | 1995-05-18 |  3100.00 |
|         103 | Alexander   | Hunold      | 1990-01-03 |  9000.00 |
|         193 | Britney     | Everett     | 1997-03-03 |  3900.00 |
|         104 | Bruce       | Ernst       | 1991-05-21 |  6000.00 |
|         179 | Charles     | Johnson     | 2000-01-04 |  6200.00 |
|         109 | Daniel      | Faviet      | 1994-08-16 |  9000.00 |
|         105 | David       | Austin      | 1997-06-25 |  4800.00 |
|         114 | Den         | Raphaely    | 1994-12-07 | 11000.00 |
|         107 | Diana       | Lorentz     | 1999-02-07 |  4200.00 |
|         118 | Guy         | Himuro      | 1998-11-15 |  2600.00 |
...

在這個範例中,ORDER BY 子句先依名字的遞增順序排序結果集,然後依姓氏的遞減順序排序已排序的結果集。

請注意結果集中兩名員工的位置變化:Alexander KhooAlexander Hunold

3) 使用 SQL ORDER BY 子句排序數值欄位的值範例

以下範例使用 ORDER BY 子句,依薪資從高到低排序員工

SELECT
	employee_id,
	first_name,
	last_name,
	hire_date,
	salary
FROM
	employees
ORDER BY
	salary DESC;Code language: SQL (Structured Query Language) (sql)

試試看

+-------------+-------------+-------------+------------+----------+
| employee_id | first_name  | last_name   | hire_date  | salary   |
+-------------+-------------+-------------+------------+----------+
|         100 | Steven      | King        | 1987-06-17 | 24000.00 |
|         101 | Neena       | Kochhar     | 1989-09-21 | 17000.00 |
|         102 | Lex         | De Haan     | 1993-01-13 | 17000.00 |
|         145 | John        | Russell     | 1996-10-01 | 14000.00 |
|         146 | Karen       | Partners    | 1997-01-05 | 13500.00 |
|         201 | Michael     | Hartstein   | 1996-02-17 | 13000.00 |
|         205 | Shelley     | Higgins     | 1994-06-07 | 12000.00 |
|         108 | Nancy       | Greenberg   | 1994-08-17 | 12000.00 |
|         114 | Den         | Raphaely    | 1994-12-07 | 11000.00 |
...

4) 使用 SQL ORDER BY 依日期排序範例

除了字元和數值資料外,您還可以使用 ORDER BY 子句依日期排序行。例如,以下語句使用 ORDER BY 子句,依 hire_date 欄位中的值排序員工

SELECT
	employee_id,
	first_name,
	last_name,
	hire_date,
	salary
FROM
	employees
ORDER BY
	hire_date;Code language: SQL (Structured Query Language) (sql)

試試看

+-------------+-------------+-------------+------------+----------+
| employee_id | first_name  | last_name   | hire_date  | salary   |
+-------------+-------------+-------------+------------+----------+
|         100 | Steven      | King        | 1987-06-17 | 24000.00 |
|         200 | Jennifer    | Whalen      | 1987-09-17 |  4400.00 |
|         101 | Neena       | Kochhar     | 1989-09-21 | 17000.00 |
|         103 | Alexander   | Hunold      | 1990-01-03 |  9000.00 |
|         104 | Bruce       | Ernst       | 1991-05-21 |  6000.00 |
|         102 | Lex         | De Haan     | 1993-01-13 | 17000.00 |
|         203 | Susan       | Mavris      | 1994-06-07 |  6500.00 |
|         204 | Hermann     | Baer        | 1994-06-07 | 10000.00 |
...

要查看從最近到最早加入公司的員工,您可以依聘用日期的遞減順序排序員工

SELECT
	employee_id,
	first_name,
	last_name,
	hire_date,
	salary
FROM
	employees
ORDER BY
	hire_date DESC;Code language: SQL (Structured Query Language) (sql)

試試看

+-------------+-------------+-------------+------------+----------+
| employee_id | first_name  | last_name   | hire_date  | salary   |
+-------------+-------------+-------------+------------+----------+
|         179 | Charles     | Johnson     | 2000-01-04 |  6200.00 |
|         113 | Luis        | Popp        | 1999-12-07 |  6900.00 |
|         119 | Karen       | Colmenares  | 1999-08-10 |  2500.00 |
|         178 | Kimberely   | Grant       | 1999-05-24 |  7000.00 |
|         107 | Diana       | Lorentz     | 1999-02-07 |  4200.00 |
|         118 | Guy         | Himuro      | 1998-11-15 |  2600.00 |
|         126 | Irene       | Mikkilineni | 1998-09-28 |  2700.00 |
|         177 | Jack        | Livingston  | 1998-04-23 |  8400.00 |
|         176 | Jonathon    | Taylor      | 1998-03-24 |  8600.00 |
...

摘要

  • 使用 ORDER BY 子句排序 SELECT 子句返回的行。
  • 使用 ASC 選項按遞增順序排序行,並使用 DESC 選項按遞減順序排序行。
本教學對您有幫助嗎?