SQL ORDER BY Clause
- SELECT returns records in no particular order.
- To ensure a specific order use the ORDER BY clause.
- ORDER BY allows sorting by one or more columns.
- Records can be returned in ascending or descending order.
The SQL ORDER BY syntax
The general syntax is:
- SELECT column-names FROM table-name WHERE condition ORDER BY column-names
- SELECT column-names1, column-name2, ......... FROM table-name WHERE condition ORDER BY column-names
SQL ORDER BY Examples
1) for single column
mysql> select name from person where age>20 order by name;
+-----------------+
| name |
+-----------------+
| chayan das |
| monojit |
| sajal |
| sajal |
| shantanu biswas |
| sourav biswas |
| subham ball |
| sudip saha |
+-----------------+
8 rows in set (0.00 sec)
+-----------------+
| name |
+-----------------+
| chayan das |
| monojit |
| sajal |
| sajal |
| shantanu biswas |
| sourav biswas |
| subham ball |
| sudip saha |
+-----------------+
8 rows in set (0.00 sec)
2) for multiple Column
mysql> select name,age from person where age>20 order by age;
+-----------------+------+
| name | age |
+-----------------+------+
| sajal | 21 |
| sajal | 21 |
| monojit | 22 |
| sourav biswas | 23 |
| chayan das | 24 |
| subham ball | 25 |
| shantanu biswas | 26 |
| sudip saha | 26 |
+-----------------+------+
8 rows in set (0.00 sec)
Comments
Post a Comment