最适合网络开发者的网站

SQL 教程

SQL主页 SQL 简介 SQL 语法 SQL 选择 SQL 选择不同 SQL 语句 SQL 排序依据 SQL 和 SQL 或 SQL不是 SQL 插入 SQL 空值 SQL 更新 SQL 删除 SQL 选择顶部 SQL 最小值和最大值 SQL 计数 SQL 汇总 SQL 平均值 SQL 类似 SQL 通配符 SQL 输入 SQL 之间 SQL 别名 SQL 连接 SQL 内连接 SQL左连接 SQL右连接 SQL全连接 SQL自连接 SQL联盟 SQL 分组依据 SQL 具有 SQL 存在 SQL 任何、全部 SQL 选择 SQL 插入选择 SQL 案例 SQL 空函数 SQL 存储过程 SQL 注释 SQL 运算符

SQL 数据库

SQL创建数据库 SQL删除数据库 SQL 备份数据库 SQL创建表 SQL删除表 SQL 修改表 SQL 约束 SQL 非空 SQL唯一标识符 SQL 主键 SQL 外键 SQL 检查 SQL 默认值 SQL 索引 SQL 自动增量 SQL 日期 SQL 视图 SQL 注入 SQL 托管 SQL 数据类型

SQL 参考

SQL 关键字 MySQL 函数 SQL Server 函数 MS Access 函数 SQL 快速参考

SQL 例子

SQL 示例 SQL 编辑器 SQL 测验 SQL 练习 SQL训练营 SQL 证书

SQL。初学者课程

尿素

SQL 排序依据 Keyword


The SQL ORDER BY Keyword

ORDER BY keyword is used to sort the result-set in ascending or descending order.

ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.

ORDER BY Syntax

SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

Demo Database

Below is a selection from the "Customers" table in the Northwind sample database:

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 Example

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

例子

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

ORDER BY DESC Example

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

例子

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

ORDER BY Several Columns Example

The following SQL statement selects all customers from the "Customers" table, sorted by the "Country" and the "CustomerName" column. This means that it orders by Country, but if some rows have the same Country, it orders them by CustomerName:

例子

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

ORDER BY Several Columns Example 2

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

例子

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

Test Yourself With Exercises

Exercise:

Select all records from the Customers table, sort the result alphabetically by the column City.

SELECT * FROM Customers
;