MySQL ORDER BY
The ORDER BY statement is used to sort the fetched data in ascending and descensing order.But sorts the records in ascending order by default, use the DESC keyword to sort descensing order.
Syntax
SELECT column1,column2... FROM table_name order by column name ASC | DESC ;
Demo Database:
- Table Name : products
product_id | product_name | product_code | price | unit | category_id |
---|---|---|---|---|---|
1 | The Psychology | 7550 | 250.00 | Piece | 1 |
2 | Stories for Children | 3098 | 350.00 | Piece | 1 |
3 | Harry Potter | 8472 | 275.00 | Piece | 1 |
4 | Tecno Spark | 9468 | 8000.00 | Nos | 2 |
5 | Samsung Galaxy | 7188 | 10000.00 | Nos | 2 |
6 | Panasonic Eluga | 3433 | 7000.00 | Nos | 2 |
7 | Lenovo IdeaPad | 6708 | 45000.00 | Nos | 3 |
8 | ASUS Celeron Dual Core | 3583 | 43000.00 | Nos | 3 |
Order By Ascending
The ASC keyword used in sort the fetched records in ascending order.
Example
SELECT * FROM products Order by product_name ASC;Try it Yourself
Order By Descending
The DESC keyword is used to sort the fetched data in descending order.
Example
SELECT * FROM products Order by product_name DESC;Try it Yourself
Sorting multiple columns
This clause is used to sort the retrieved rows based on multiple columns
Example
SELECT * FROM products Order by price ASC ,unit DESC ;Try it Yourself