SQL Order By

SQL Order By

The SQL ORDER BY is used to sort the records of a select query.

Order By syntax

SELECT * FROM table_name ORDER BY column_name ASC | DESC;

Order By example

Coder books table

IDTitlePriceDescription
1Learn SQL20Learn SQL language
2Learn MySQL22Learn MySQL language
3HTML book17Learn HTML
4Learn PHP20Introduction to PHP
5Learn PHP20PHP course
select * from coder_books order by price asc;

Result

IDTitlePriceDescription
3HTML book17Learn HTML
4Learn PHP20Introduction to PHP
5Learn PHP20PHP course
1Learn SQL20Learn SQL language
2Learn MySQL22Learn MySQL language
select * from coder_books where price=20 order by id desc;

Result

IDTitlePriceDescription
5Learn PHP20PHP course
4Learn PHP20Introduction to PHP
1Learn SQL20Learn SQL language