MySQL Tutorial
The MySQL ORDER BY clause is used for sorting data in ascending and descending order based on one or more columns
Some databases sort the query results in an ascending order by default.
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
Let's put these statements into real use.
We've a table named Students in our database that contains the following records:
ROLL_NO | NAME | SUBJECT |
---|---|---|
1 | Will | C++ |
2 | SAM | Python |
3 | Sara | HTML | 4 | Rim | Java |
5 | Micheal | MySQL |
6 | Lara | Node.js |
Now we will fetch records from the Students table with Descending order.
SELECT * FROM Students
ORDER BY NAME DESC;
The above SELECT statement selects all In DESCENDING order of NAME from Students table.
ROLL_NO | NAME | SUBJECT |
---|---|---|
1 | Will | C++ |
3 | Sara | HTML |
2 | SAM | Python | 4 | Rim | Java |
5 | Micheal | MySQL |
6 | Lara | Node.js |
Now we will fetch records from the Students table with order.
SELECT * FROM Students
ORDER BY SUBJECT ;
The above SELECT statement selects all In order of SUBJECT from Students table.
ROLL_NO | NAME | SUBJECT |
---|---|---|
1 | Will | C++ |
3 | Sara | HTML | 4 | Rim | Java |
5 | Micheal | MySQL |
6 | Lara | Node.js |
2 | SAM | Python |