MySQL - SPLessons

MySQL Sort

Chapter 27

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

MySQL Sort

MySQL Sort

shape Description

MySQL Sort is similar to Order By Clause that arrange data of a table either in ascending order or descending order based on single column i.e., Select * from the table name will show all the rows from the table. And this sorting clause is used to arrange the specific column in ascending or descending order.

shape Syntax

The syntax for Sort clause is as follows:
Select * from <table_name> ORDER BY <column_name> DESC;

table_name=>Any accurate table.

column_name =>The condition that one can perform on a column by using Order by clause.

shape Examples

By viewing the below example, the concept of sorting clause can be easily understood.

mysql> select * from employee;
+--------+-------+-------+
| emp_id | ename | sal   |
+--------+-------+-------+
|   1001 | mike  | 12000 |
|   1002 | rambo | 13000 |
+--------+-------+-------+
2 rows in set (0.00 sec)

mysql> select * from employee order by sal desc;
+--------+-------+-------+
| emp_id | ename | sal   |
+--------+-------+-------+
|   1002 | rambo | 13000 |
|   1001 | mike  | 12000 |
+--------+-------+-------+
2 rows in set (0.06 sec)

mysql> select * from employee order by sal;
+--------+-------+-------+
| emp_id | ename | sal   |
+--------+-------+-------+
|   1001 | mike  | 12000 |
|   1002 | rambo | 13000 |
+--------+-------+-------+
2 rows in set (0.06 sec)

The above example tells that, when sort clause operation is performed on a column name like salary, then it automatically displays the entire details of employees in prescribed order format.

Summary

shape Key Points

  • MySQL Sort – Used to arrange the data in a increasing or decreasing order.