SELECT * FROM STUDENT ORDER BY MARK
id | name | class | mark | gender |
---|---|---|---|---|
19 | Tinny | Nine | 18 | male |
17 | Tumyu | Six | 54 | male |
29 | Tess Played | Seven | 55 | male |
3 | Arnold | Three | 55 | male |
6 | Alex John | Four | 55 | male |
22 | Reggid | Seven | 55 | female |
10 | Big John | Four | 55 | female |
4 | Krish Star | Four | 60 | female |
5 | John Mike | Four | 60 | female |
20 | Jackly | Nine | 65 | female |
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
id | name | class | mark |
---|---|---|---|
2 | Max Ruin | Three | 85 |
3 | Arnold | Three | 55 |
1 | John Deo | Four | 75 |
4 | Krish Star | Four | 60 |
5 | John Mike | Four | 60 |
6 | Alex John | Four | 55 |
Read how Order by command is used to display highest 3 records of student table
SELECT * FROM `student` ORDER BY class
id | name | class | mark |
1 | John Deo | Four | 75 |
4 | Krish Star | Four | 60 |
5 | John Mike | Four | 60 |
6 | Alex John | Four | 55 |
2 | Max Ruin | Three | 85 |
3 | Arnold | Three | 55 |
SELECT * FROM `student` ORDER BY class desc
SELECT * FROM student ORDER BY mark , name
This will list on ascending order of mark. When there are more than one student got the same mark ( say 88 ) then the names of them will be listed alphabetically.
SELECT * FROM student ORDER BY mark , name DESC
If you want listing should be from highest mark to lowest mark then query should be
SELECT * FROM student ORDER BY mark DESC, name DESC
UPDATE student set diff = 100-mark
Now use order by query.
SELECT * FROM `student` order by diff desc
You will see the list like this.
id | name | class | mark | gender | diff |
---|---|---|---|---|---|
19 | Tinny | Nine | 18 | male | 82 |
12 | Recky | Six | 94 | female | 6 |
17 | Tumyu | Six | 54 | male | 46 |
22 | Reggid | Seven | 55 | female | 45 |
29 | Tess Played | Seven | 55 | male | 45 |
3 | Arnold | Three | 55 | male | 45 |
6 | Alex John | Four | 55 | male | 45 |
10 | Big John | Four | 55 | female | 45 |
4 | Krish Star | Four | 60 | female | 40 |
5 | John Mike | Four | 60 | female | 40 |
SELECT * FROM `student` ORDER BY CAST(diff as unsigned) DESC
Now you will get list like this.
id | name | class | mark | gender | diff |
---|---|---|---|---|---|
19 | Tinny | Nine | 18 | male | 82 |
17 | Tumyu | Six | 54 | male | 46 |
29 | Tess Played | Seven | 55 | male | 45 |
3 | Arnold | Three | 55 | male | 45 |
6 | Alex John | Four | 55 | male | 45 |
22 | Reggid | Seven | 55 | female | 45 |
10 | Big John | Four | 55 | female | 45 |
4 | Krish Star | Four | 60 | female | 40 |
5 | John Mike | Four | 60 | female | 40 |
20 | Jackly | Nine | 65 | female | 35 |
SELECT dt , topic_id,userid
FROM (
SELECT topic_id, rdtp AS dt, userid
FROM forum_reply
UNION ALL
SELECT topic_id, tdtp AS dt, userid
FROM forum_topics
) t
ORDER BY dt DESC limit 0,10
Here we used date and time field dt ( of both tables ) to display records in order of data and time.
jigger | 11-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. |
annu | 10-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 Highley | 27-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. |
yamsoti | 26-03-2016 |
I want to display 2 highest mark from class 4, it has marks 70, 60, 60, 50. How do I prepare the query. |
smo1234 | 07-04-2016 |
Your can read how to get second highest by using order by and limit |