In MySQL, the ORDER by keyword is used to get records from a table in sorted order. The ORDER BY keyword can use one or more columns to sort the result-set.
We can sort the records in both ascending and descending order.By default, ORDER by keyword sorts records in Ascending order. For descending order we need to use DESC keyword with the ORDER by keyword.
ORDER BY Syntax
SELECT column_name, column_name
FROM table_name
ORDER by column_name ASC|DESC, column_name ASC|DESC;
We have a table "employee" as below:
employee
id first_name last_name country
.......................................................
1 John Simp Canada
2 Chris Hely Canada
3 Max Roy Korea
4 Jenny Mill Canada
5 Jack Simpson London
ORDER BY Example
The below statement selects all the employees from the "employee" table sorted by "country column":
SELECT * FROM employee
ORDER by country;
Result
id first_name last_name country
.......................................................
1 John Simp Canada
2 Chris Hely Canada
4 Jenny Mill Canada
3 Max Roy Korea
5 Jack Simpson London
ORDER BY DESC Example
he below statement selects all the employees from the "employee" table sorted in descending order by "country column":
SELECT * FROM employee
ORDER by country DESC;
Result
id first_name last_name country
.......................................................
5 Jack Simpson London
3 Max Roy Korea
1 John Simp Canada
2 Chris Hely Canada
4 Jenny Mill Canada
Hope this will help you :)
0 Comment(s)