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
ID | Title | Price | Description |
---|
1 | Learn SQL | 20 | Learn SQL language |
2 | Learn MySQL | 22 | Learn MySQL language |
3 | HTML book | 17 | Learn HTML |
4 | Learn PHP | 20 | Introduction to PHP |
5 | Learn PHP | 20 | PHP course |
select * from coder_books order by price asc;
Result
ID | Title | Price | Description |
---|
3 | HTML book | 17 | Learn HTML |
4 | Learn PHP | 20 | Introduction to PHP |
5 | Learn PHP | 20 | PHP course |
1 | Learn SQL | 20 | Learn SQL language |
2 | Learn MySQL | 22 | Learn MySQL language |
select * from coder_books where price=20 order by id desc;
Result
ID | Title | Price | Description |
---|
5 | Learn PHP | 20 | PHP course |
4 | Learn PHP | 20 | Introduction to PHP |
1 | Learn SQL | 20 | Learn SQL language |