Sorting when digits & alphabets in data (#22878)

Introduced sorting done during digits & alphabets combines and updated the example to reflect the same.
This commit is contained in:
Shad Ahmad Zaidi
2018-11-24 05:49:21 +05:30
committed by Randell Dawson
parent 08f7e2a87e
commit b5df808ecf

View File

@ -4,7 +4,7 @@ title: SQL Order By Keyword
## SQL Order By Keyword
### Order By (ASC, DESC)
ORDER BY gives us a way to SORT the result set by one or more of the items in the SELECT section. Here is an SQL sorting the students by FullName in descending order. The default sort order is ascending (ASC) but to sort in the opposite order (descending) you use DESC.
ORDER BY gives us a way to SORT the result set by one or more of the items in the SELECT section. It sorts the result set in lexicographical order to cater data which have both digits & alphabets together. Here is an SQL sorting the students by FullName in descending order. The default sort order is ascending (ASC) but to sort in the opposite order (descending) you use DESC.
```sql
SELECT studentID, FullName, sat_score
@ -25,9 +25,12 @@ ORDER BY FullName DESC;
| 7 | Edgar Frank "Ted" Codd | 2400 |
| 8 | Donald D. Chamberlin | 2400 |
| 5 | Alvin Greene | 1200 |
| 10 | 2monk Zeph | 700 |
+-----------+------------------------+-----------+
9 rows in set (0.00 sec)
10 rows in set (0.00 sec)
```
We see that the name starting with digits goes to the top since digits are prefered over alphabets in lexicographical sorting.
*Here is the UN-ORDERED, current, full student list to compare to the above.*
@ -39,6 +42,7 @@ SELECT studentID, FullName, sat_score, rcd_updated FROM student;
+-----------+------------------------+-----------+---------------------+
| studentID | FullName | sat_score | rcd_updated |
+-----------+------------------------+-----------+---------------------+
| 10 | 2monk Zeph | 700 | 2017-08-19 19:56:13 |
| 1 | Monique Davis | 400 | 2017-08-16 15:34:50 |
| 2 | Teri Gutierrez | 800 | 2017-08-16 15:34:50 |
| 3 | Spencer Pautier | 1000 | 2017-08-16 15:34:50 |
@ -49,9 +53,8 @@ SELECT studentID, FullName, sat_score, rcd_updated FROM student;
| 8 | Donald D. Chamberlin | 2400 | 2017-08-16 15:35:33 |
| 9 | Raymond F. Boyce | 2400 | 2017-08-16 15:35:33 |
+-----------+------------------------+-----------+---------------------+
9 rows in set (0.00 sec)
10 rows in set (0.00 sec)
```
As with all of these SQL things there is MUCH MORE to them than what's in this introductory guide.
I hope this at least gives you enough to get started.