ORDER BY keyword is used to sort records in the form of ascending or descending.
For the ORDER BY command, we will use this given table as an example.
id | username | age | country |
1 | Aman | 20 | India |
2 | John | 20 | USA |
3 | Ahmad | 20 | Pakistan |
ORDER BY (ASC)
This command will fetch all the records from a table by sorting specified field values in ascending order.
SELECT * FROM users ORDER BY country ASC;
id | username | age | country |
1 | Aman | 20 | India |
3 | Ahmad | 20 | Pakistan |
2 | John | 20 | USA |
ORDER BY (DESC)
This command will fetch all the records from a table by sorting specified field values in descending order.
SELECT * FROM users ORDER BY country DESC;
id | username | age | country |
2 | John | 20 | USA |
3 | Ahmad | 20 | Pakistan |
1 | Aman | 20 | India |