SQL PHP HTML ASP JavaScript articles and free scripts to download
SQL

SQL ORDER BY Descending and ascending Command

The results we get from a table we may have to display in an order. The result may be from highest to lowest or lowest to highest  in a numeric field or from A to Z or Z to A in a text or varchar field.  We may require a combination of this also. We can use ORDER BY sql command to manage the display of records. We will apply this sql command to our student table in MySQL database to control or manage our order of display.

Highest to lowest order

We can apply this to our numeric field mark to display the list in order of lowest mark to highest mark  by using the ASC command ( ascending command ) . Please note that by default all order by commands are in ascending order only.  Here is the command to display the records in decending order ( from highest to lowest ) based on the mark field.

SELECT * FROM `student` ORDER BY mark desc

Here the total mark will be displayed in the order of highest to lowest and class will not be considered. To display the records in order of mark in a class, we have to use to field names in the order by clause. Here is the code to display the records in the order of class and then in the order of marks. This will give a highly use full way by displaying all the records in order of class and within  the class in order of marks.

SELECT * FROM `student` ORDER BY class desc, mark desc

idnameclass mark
2Max RuinThree85
3ArnoldThree55
1John DeoFour75
4Krish StarFour60
5John MikeFour60
6Alex JohnFour55

Read how Order by command is used to display highest 3 records of student table

order by on a varchar field

Here is the command to display the records in the order of class
SELECT * FROM `student` ORDER BY class
id name class   mark
1John DeoFour75
4Krish StarFour60
5John MikeFour60
6Alex John Four55
2Max RuinThree85
3ArnoldThree55

Here the records are returned in the order by class from A to Z . We can reverse the order by specifying in our sql command to change the order to Descending that is from Z to A. We have to add desc to the ORDER BY clause. Here is the command to do that
SELECT * FROM `student` ORDER BY class desc
How to give the option to the user to change the record display order ?

Number of User Comments : 3


Google+

jigger11-06-2013
just want to asked guys, hope you help me this is the scenario i have at least 3 data in my database from ID 1, 2, 3, and i want to display this file or post, data display is OK but i want to display like this 3, 2, 1 how can make it that way.
annu10-10-2014
Suppose if we are using 1st, 2nd and 3rd in class instead of two three and four how will this query will work in that case
Steve Highley27-10-2014
The desc qualifier (which stands for descending, i.e. high to low) changes the sequence from the default of low to high.

Data is ordered depending on the data type. Text is ordered according to collating sequence, numbers from low to high (e.g. -100 is before 5), and dates are ordered from earliest to latest.

So 'Three' is greater than 'Four' because T is after F in the collating sequence.

But 3 is less than 4 whether stored as numbers or text.

I hope that helps.

Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked




HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer