The ORDER BY clause is used to sort the query results in either ascending (ASC) or descending (DESC) order.
The ASC keyword is used to sort the result set in ascending order, which is the default behavior if you don't specify any order.
The DESC keyword, on the other hand, is used to sort the result set in descending order.
Content of our table:
mysql> SELECT * FROM friends;
+----+-----------+----------+-----------+
| id | Name | Lastname | Telephone |
+----+-----------+----------+-----------+
| 1 | John | Smith | 111333 |
| 2 | Anastasia | TheGreat | 555777 |
| 3 | Samantha | Fox | 456456 |
| 10 | Larry | TheFirst | 555888 |
| 11 | Bill | Gates | 444888 |
| 12 | Bill | Gates | 444888 |
| 13 | John | Smith | 111333 |
| 14 | Anastasia | Some | 555444 |
+----+-----------+----------+-----------+
8 rows in set (0.00 sec)
mysql>
Usage of ORDER BY targeting column id:
mysql> SELECT * FROM friends ORDER BY id DESC;
+----+-----------+----------+-----------+
| id | Name | Lastname | Telephone |
+----+-----------+----------+-----------+
| 14 | Anastasia | Some | 555444 |
| 13 | John | Smith | 111333 |
| 12 | Bill | Gates | 444888 |
| 11 | Bill | Gates | 444888 |
| 10 | Larry | TheFirst | 555888 |
| 3 | Samantha | Fox | 456456 |
| 2 | Anastasia | TheGreat | 555777 |
| 1 | John | Smith | 111333 |
+----+-----------+----------+-----------+
8 rows in set (0.02 sec)
mysql>
Targeting column Name ascending:
mysql> SELECT * FROM friends ORDER BY Name ASC;
+----+-----------+----------+-----------+
| id | Name | Lastname | Telephone |
+----+-----------+----------+-----------+
| 2 | Anastasia | TheGreat | 555777 |
| 14 | Anastasia | Some | 555444 |
| 11 | Bill | Gates | 444888 |
| 12 | Bill | Gates | 444888 |
| 1 | John | Smith | 111333 |
| 13 | John | Smith | 111333 |
| 10 | Larry | TheFirst | 555888 |
| 3 | Samantha | Fox | 456456 |
+----+-----------+----------+-----------+
8 rows in set (0.01 sec)
mysql>
mysql> SELECT * FROM friends ORDER BY Name DESC;
+----+-----------+----------+-----------+
| id | Name | Lastname | Telephone |
+----+-----------+----------+-----------+
| 3 | Samantha | Fox | 456456 |
| 10 | Larry | TheFirst | 555888 |
| 1 | John | Smith | 111333 |
| 13 | John | Smith | 111333 |
| 11 | Bill | Gates | 444888 |
| 12 | Bill | Gates | 444888 |
| 2 | Anastasia | TheGreat | 555777 |
| 14 | Anastasia | Some | 555444 |
+----+-----------+----------+-----------+
8 rows in set (0.00 sec)
mysql>
This MySQL query selects all columns from the "friends" table and orders the results in descending order by the "Name" column. The resulting table will contain all rows from the "friends" table, sorted in reverse alphabetical order by the values in the "Name" column.
The DESC
keyword is used to specify that the order should be descending.
If you used ASC
instead, the results would be sorted in ascending order.
mysql> SELECt * FROM friends ORDER BY Lastname ASC;
+----+-----------+----------+-----------+
| id | Name | Lastname | Telephone |
+----+-----------+----------+-----------+
| 3 | Samantha | Fox | 456456 |
| 11 | Bill | Gates | 444888 |
| 12 | Bill | Gates | 444888 |
| 1 | John | Smith | 111333 |
| 13 | John | Smith | 111333 |
| 14 | Anastasia | Some | 555444 |
| 10 | Larry | TheFirst | 555888 |
| 2 | Anastasia | TheGreat | 555777 |
+----+-----------+----------+-----------+
8 rows in set (0.00 sec)
mysql>
More experiments:
mysql> SELECT id, Name, Telephone FROM friends ORDER BY Lastname ASC;
+----+-----------+-----------+
| id | Name | Telephone |
+----+-----------+-----------+
| 3 | Samantha | 456456 |
| 11 | Bill | 444888 |
| 12 | Bill | 444888 |
| 1 | John | 111333 |
| 13 | John | 111333 |
| 14 | Anastasia | 555444 |
| 10 | Larry | 555888 |
| 2 | Anastasia | 555777 |
+----+-----------+-----------+
8 rows in set (0.00 sec)
mysql>
mysql> SELECT Lastname, id, Name, Telephone FROM friends ORDER BY Lastname ASC;
+----------+----+-----------+-----------+
| Lastname | id | Name | Telephone |
+----------+----+-----------+-----------+
| Fox | 3 | Samantha | 456456 |
| Gates | 11 | Bill | 444888 |
| Gates | 12 | Bill | 444888 |
| Smith | 1 | John | 111333 |
| Smith | 13 | John | 111333 |
| Some | 14 | Anastasia | 555444 |
| TheFirst | 10 | Larry | 555888 |
| TheGreat | 2 | Anastasia | 555777 |
+----------+----+-----------+-----------+
8 rows in set (0.00 sec)
mysql>
mysql> SELECT Lastname, id, Name, Telephone FROM friends ORDER BY Lastname DESC;
+----------+----+-----------+-----------+
| Lastname | id | Name | Telephone |
+----------+----+-----------+-----------+
| TheGreat | 2 | Anastasia | 555777 |
| TheFirst | 10 | Larry | 555888 |
| Some | 14 | Anastasia | 555444 |
| Smith | 1 | John | 111333 |
| Smith | 13 | John | 111333 |
| Gates | 11 | Bill | 444888 |
| Gates | 12 | Bill | 444888 |
| Fox | 3 | Samantha | 456456 |
+----------+----+-----------+-----------+
8 rows in set (0.00 sec)
mysql>
No comments:
Post a Comment