SQL ORDER BY Keyword
ORDER BY keyword is used to sort the result set.
SQL ORDER BY Keyword
ORDER BY keyword is used to sort the result set in accordance with a column or multiple columns.
ORDER BY keywords default to sort records in ascending order. If you need to sort the records in descending order, you can use the DESC keyword.
SQL ORDER BY Syntax
FROM table_name
ORDER BY column_name , column_name ASC|DESC;
The demo database
In this tutorial, we will use w3big sample database.
The following is a selected "Websites" table data:
+----+--------------+---------------------------+-------+---------+ | id | name | url | alexa | country | +----+--------------+---------------------------+-------+---------+ | 1 | Google | https://www.google.cm/ | 1 | USA | | 2 | 淘宝 | https://www.taobao.com/ | 13 | CN | | 3 | 本教程 | http://www.w3big.com/ | 4689 | CN | | 4 | 微博 | http://weibo.com/ | 20 | CN | | 5 | Facebook | https://www.facebook.com/ | 3 | USA | +----+--------------+---------------------------+-------+---------+
ORDER BY instance
The following SQL statement to select all the sites from the "Websites" list and sorted by "alexa" column:
Examples
Execution output:
ORDER BY DESC examples
The following SQL statement to select all the sites from the "Websites" list and follow "alexa" column in descending order:
Examples
Execution output:
ORDER BY multiple columns
The following SQL statement to select all the sites from the "Websites" list and follow the "country" and "alexa" column sort:
Examples
Execution output: