SQL ORDER BY キーワード

❮ 前章へ 次章へ ❯

ORDER BY キーワードは、結果セットをソートするのに使用します。


SQL ORDER BY キーワード

ORDER BY キーワードは、1 つ以上の列で結果セットをソートするのに使用します。

ORDER BY キーワードは、デフォルトで、レコードを昇順にソートします。 レコードを降順にソートするには、DESC キーワードを使用します。

SQL ORDER BY 構文

SELECT column_name, column_name
FROM table_name
ORDER BY column_name ASC|DESC, column_name ASC|DESC;

デモ・データベース

このチュートリアルでは、よく知られた Northwind サンプルデータベースを使用します。

下は、"Customers" テーブルから抜粋したものです:

CustomerID CustomerName ContactName Address City PostalCode Country
1

Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021 Mexico
3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
4

Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
5 Berglunds snabbköp Christina Berglund Berguvsvägen 8 Luleå S-958 22 Sweden

ORDER BY の例

次の SQL 文は、"Customers" テーブルから全ての customer を選択し、"Country" 列でソートします:

SELECT * FROM Customers
ORDER BY Country;
Try it Yourself ❯

ORDER BY DESC の例

次の SQL 文は、"Customers" テーブルから全ての customer を選択し、"Country" 列で降順にソートします:

SELECT * FROM Customers
ORDER BY Country DESC;
Try it Yourself ❯

ORDER BY Several Columns の例

following SQL statement selects all customers from the "Customers" table, sorted by the "Country" and the "CustomerName" column:

SELECT * FROM Customers
ORDER BY Country, CustomerName;
Try it Yourself ❯

ORDER BY 複数列の例

次の SQL 文は、"Customers" テーブルから全ての customer を選択し、"Country" の昇順、且つ "CustomerName" の降順にソートします:

SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;
Try it Yourself ❯

❮ 前章へ 次章へ ❯