The SQL ORDER BY Keyword
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.
ORDER BY Syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
MariaDB [vlrinst]> select * from emp;
+----+---------+-----------+------+-------+
| id | name | loc | age | sal |
+----+---------+-----------+------+-------+
| 10 | venkat | kphb | 35 | 30000 |
| 11 | praveen | pune | 28 | 35000 |
| 12 | mounika | hyderabad | 22 | 39000 |
| 13 | revathi | jntu | 26 | 20000 |
| 14 | anji | gutta | 28 | 20000 |
| 15 | harika | lb nagar | 26 | 30100 |
| 16 | praveen | jntu | 33 | 100 |
| 17 | pandu | munipeda | 35 | 30300 |
| 18 | pandu | pune | 23 | 39300 |
+----+---------+-----------+------+-------+
9 rows in set (0.001 sec)
MariaDB [vlrinst]> select * from emp order by name;
+----+---------+-----------+------+-------+
| id | name | loc | age | sal |
+----+---------+-----------+------+-------+
| 14 | anji | gutta | 28 | 20000 |
| 15 | harika | lb nagar | 26 | 30100 |
| 12 | mounika | hyderabad | 22 | 39000 |
| 17 | pandu | munipeda | 35 | 30300 |
| 18 | pandu | pune | 23 | 39300 |
| 11 | praveen | pune | 28 | 35000 |
| 16 | praveen | jntu | 33 | 100 |
| 13 | revathi | jntu | 26 | 20000 |
| 10 | venkat | kphb | 35 | 30000 |
+----+---------+-----------+------+-------+
9 rows in set (0.038 sec)
MariaDB [vlrinst]> select * from emp order by sal;
+----+---------+-----------+------+-------+
| id | name | loc | age | sal |
+----+---------+-----------+------+-------+
| 16 | praveen | jntu | 33 | 100 |
| 13 | revathi | jntu | 26 | 20000 |
| 14 | anji | gutta | 28 | 20000 |
| 10 | venkat | kphb | 35 | 30000 |
| 15 | harika | lb nagar | 26 | 30100 |
| 17 | pandu | munipeda | 35 | 30300 |
| 11 | praveen | pune | 28 | 35000 |
| 12 | mounika | hyderabad | 22 | 39000 |
| 18 | pandu | pune | 23 | 39300 |
+----+---------+-----------+------+-------+
9 rows in set (0.001 sec)
MariaDB [vlrinst]> select * from emp order by desc;
ERROR 1064 (42000): You have an error in your SQL synt
MariaDB [vlrinst]> select * from emp order by sal desc
+----+---------+-----------+------+-------+
| id | name | loc | age | sal |
+----+---------+-----------+------+-------+
| 18 | pandu | pune | 23 | 39300 |
| 12 | mounika | hyderabad | 22 | 39000 |
| 11 | praveen | pune | 28 | 35000 |
| 17 | pandu | munipeda | 35 | 30300 |
| 15 | harika | lb nagar | 26 | 30100 |
| 10 | venkat | kphb | 35 | 30000 |
| 13 | revathi | jntu | 26 | 20000 |
| 14 | anji | gutta | 28 | 20000 |
| 16 | praveen | jntu | 33 | 100 |
+----+---------+-----------+------+-------+
9 rows in set (0.000 sec)
MariaDB [vlrinst]> select name,loc from emp order by
ERROR 1064 (42000): You have an error in your SQL synt
MariaDB [vlrinst]> select name,loc from emp order by
-> name,loc;
+---------+-----------+
| name | loc |
+---------+-----------+
| anji | gutta |
| harika | lb nagar |
| mounika | hyderabad |
| pandu | munipeda |
| pandu | pune |
| praveen | jntu |
| praveen | pune |
| revathi | jntu |
| venkat | kphb |
+---------+-----------+
9 rows in set (0.001 sec)
MariaDB [vlrinst]> select name,age,loc from emp
-> order by 2;
+---------+------+-----------+
| name | age | loc |
+---------+------+-----------+
| mounika | 22 | hyderabad |
| pandu | 23 | pune |
| revathi | 26 | jntu |
| harika | 26 | lb nagar |
| praveen | 28 | pune |
| anji | 28 | gutta |
| praveen | 33 | jntu |
| venkat | 35 | kphb |
| pandu | 35 | munipeda |
+---------+------+-----------+
9 rows in set (0.024 sec)
MariaDB [vlrinst]> select name,age,loc from emp
-> order by 2,1;
+---------+------+-----------+
| name | age | loc |
+---------+------+-----------+
| mounika | 22 | hyderabad |
| pandu | 23 | pune |
| harika | 26 | lb nagar |
| revathi | 26 | jntu |
| anji | 28 | gutta |
| praveen | 28 | pune |
| praveen | 33 | jntu |
| pandu | 35 | munipeda |
| venkat | 35 | kphb |
+---------+------+-----------+
9 rows in set (0.001 sec)
MariaDB [vlrinst]> select * from emp order by 2;
+----+---------+-----------+------+-------+
| id | name | loc | age | sal |
+----+---------+-----------+------+-------+
| 14 | anji | gutta | 28 | 20000 |
| 15 | harika | lb nagar | 26 | 30100 |
| 12 | mounika | hyderabad | 22 | 39000 |
| 17 | pandu | munipeda | 35 | 30300 |
| 18 | pandu | pune | 23 | 39300 |
| 11 | praveen | pune | 28 | 35000 |
| 16 | praveen | jntu | 33 | 100 |
| 13 | revathi | jntu | 26 | 20000 |
| 10 | venkat | kphb | 35 | 30000 |
+----+---------+-----------+------+-------+
9 rows in set (0.000 sec)
MariaDB [vlrinst]> select * from emp order by 5;
+----+---------+-----------+------+-------+
| id | name | loc | age | sal |
+----+---------+-----------+------+-------+
| 16 | praveen | jntu | 33 | 100 |
| 13 | revathi | jntu | 26 | 20000 |
| 14 | anji | gutta | 28 | 20000 |
| 10 | venkat | kphb | 35 | 30000 |
| 15 | harika | lb nagar | 26 | 30100 |
| 17 | pandu | munipeda | 35 | 30300 |
| 11 | praveen | pune | 28 | 35000 |
| 12 | mounika | hyderabad | 22 | 39000 |
| 18 | pandu | pune | 23 | 39300 |
+----+---------+-----------+------+-------+
9 rows in set (0.001 sec)
MariaDB [vlrinst]>
No comments:
Post a Comment
Note: only a member of this blog may post a comment.