The SQL ORDER BY clause sorts query results. Ascending order is the default, so these two queries are equivalent:
SELECT id, name, mark
FROM student
ORDER BY mark;
SELECT id, name, mark
FROM student
ORDER BY mark ASC;
To sort from highest to lowest, use DESC:
SELECT id, name, mark
FROM student
ORDER BY mark DESC;
ORDER BY, SQL does not guarantee the order in which rows are returned.SELECT column1, column2
FROM table_name
ORDER BY column1 ASC;
ASC means ascending order and is the default. DESC means descending order.
For a numeric column, ascending order moves from lower values to higher values:
SELECT id, name, mark
FROM student
ORDER BY mark ASC;
| id | name | mark |
|---|---|---|
| 19 | Tinny | 18 |
| 17 | Tumyu | 54 |
| 3 | Arnold | 55 |
| 6 | Alex John | 55 |
| 4 | Krish Star | 60 |
Use DESC to return larger values first:
SELECT id, name, mark
FROM student
ORDER BY mark DESC;
This is useful for highest scores, newest dates, largest prices and similar rankings.
For top records, combine ORDER BY with LIMIT.
For text columns, ascending order follows the column's collation rules:
SELECT id, name, class
FROM student
ORDER BY class ASC;
Descending order reverses that sort:
SELECT id, name, class
FROM student
ORDER BY class DESC;
SQL applies the sort columns from left to right. This query sorts by class first, then by mark within each class:
SELECT id, name, class, mark
FROM student
ORDER BY class ASC, mark DESC;
Each sort column can use its own direction.
For example, marks can be descending while names are ascending for equal marks:
SELECT id, name, mark
FROM student
ORDER BY mark DESC, name ASC;
If several rows have the same value in the first sort column, their relative order is not guaranteed unless another sort expression resolves the tie.
Instead of:
SELECT id, name, mark
FROM student
ORDER BY mark DESC;
use a unique or otherwise deterministic secondary column when stable output matters:
SELECT id, name, mark
FROM student
ORDER BY mark DESC, id ASC;
This is especially important for pagination and top-N queries.
The WHERE clause filters rows first; ORDER BY sorts the rows that remain.
SELECT id, name, class, mark
FROM student
WHERE class = 'Four'
ORDER BY mark DESC, id ASC;
To return the three highest marks:
SELECT id, name, mark
FROM student
ORDER BY mark DESC, id ASC
LIMIT 3;
Without ORDER BY, LIMIT 3 means only "return three rows"; it does not mean the highest three rows.
See also highest and second-highest record examples.
MySQL can sort using an expression or an alias defined in the SELECT list.
SELECT id,
name,
100 - mark AS difference
FROM student
ORDER BY difference DESC;
This avoids creating or updating an extra column merely for display-time sorting.
If numeric-looking values are stored in a text column, normal text sorting can produce an unexpected sequence such as 100, 18, 40, 6, 82.
For an existing VARCHAR column named diff, convert the value for sorting:
SELECT id, name, diff
FROM student
ORDER BY CAST(diff AS UNSIGNED) DESC;
See the MySQL CAST and type-conversion tutorial.
In MySQL, NULL values normally sort before non-NULL values in ascending order and after non-NULL values in descending order.
SELECT id, name, class
FROM student3
ORDER BY class ASC;
If you need a different placement, sort first by a NULL expression and then by the column:
SELECT id, name, class
FROM student3
ORDER BY (class IS NULL) ASC,
class ASC;
This places non-NULL class values before NULL values. See SQL NULL values.
PDO placeholders bind data values, not SQL identifiers or keywords. This does not work:
<?php
$stmt=$dbo->prepare(
"SELECT id,name,mark
FROM student
ORDER BY :sort :direction"
);
If users can choose the sort column or direction, select them from strict application-controlled allowlists:
<?php
$allowed_columns=[
'name' => 'name',
'class' => 'class',
'mark' => 'mark'
];
$allowed_directions=[
'asc' => 'ASC',
'desc' => 'DESC'
];
$sort_key=$_GET['sort'] ?? 'name';
$direction_key=strtolower(
$_GET['direction'] ?? 'asc'
);
$sort=$allowed_columns[$sort_key] ?? 'name';
$direction=$allowed_directions[$direction_key] ?? 'ASC';
$sql="SELECT id,name,class,mark
FROM student
ORDER BY $sort $direction, id ASC";
$stmt=$dbo->query($sql);
See changing record display order from user input for the practical interface idea.
When combining rows from two sources with UNION ALL, the final ORDER BY sorts the combined result.
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
) AS t
ORDER BY dt DESC
LIMIT 10;
This returns the ten most recent rows across both sources, assuming the date-time values are comparable.
CAST(column AS UNSIGNED) can make index-based ordering less straightforward.EXPLAIN when a production query is slow rather than assuming every ORDER BY requires a new index.Without ORDER BY, the database is free to return rows in any order allowed by the execution plan.
If many rows share the same sort value, add another column such as a unique ID when deterministic ordering matters.
Each ORDER BY expression has its own direction. Write the required ASC or DESC explicitly when mixing directions.
Text values sort lexically. Use the correct numeric datatype whenever possible or CAST legacy text values when required.
LIMIT restricts row count. ORDER BY defines which rows are highest, lowest, newest or otherwise first.
Prepared-statement placeholders are for data values, not identifiers or ASC/DESC keywords. Use an allowlist for user-selected sorting.
Ascending order is the default. ORDER BY column and ORDER BY column ASC are equivalent.
Use DESC after the sort expression, for example ORDER BY mark DESC.
Yes. List the columns from highest sorting priority to lowest, and give each column its own ASC or DESC direction if required.
Rows tied on all specified ORDER BY expressions do not have a guaranteed relative order. Add a deterministic tie-breaker such as a unique ID when stable output matters.
Sort the relevant value in descending order and then apply LIMIT 3, preferably with a secondary tie-breaker.
No. PDO placeholders bind data values, not SQL identifiers or ASC/DESC keywords. Use fixed SQL or select the allowed identifier from a strict application allowlist.
MySQL normally sorts NULL before non-NULL values in ascending order and after non-NULL values in descending order. An additional expression can be used when another placement is required.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| 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 | |