MYSQL sort
In this chapter you will learn how to use the SELECT Statements ORDER BY clause to sort retrieved data as needed.
For example
Select student_name form student
In this example all the data is displayed in a mere random order. if you want to display name alphabetical order ,or ascending order or descending order,the order by clause is used
Order by takes the name of one or more columns by which to sort the output. Look at ate following example
SELECT student_name from students ORDER BY student_name
Sorting multiple columns
It is often necessary to sort date by more than none column. For example if you are displaying an students list, you might be display it sorted by last name and first name
To sort by multiple columns, simply specify the column names separated by commas.
The following code retrieves three columns and sorts the results by two of them
Select student_name,student_age,student_marks
From students
ORDER BY student_name,student_marks;
Sorting ascending or descending order
Data specify is not limited to ascending sort orders(form A TO Z) although this is the default sort order, the order by clause can also be used to sort in descending order. To sort by descending order, the keyword DESC must be specified.
Te following example sorts the student in descending order(most recent student joined school)
Select student_name,student_age,student_marks
From students
Order by id DESC,student_name;
Similarly you can use ASC for ascending order
|