Order By Clause
Advertisements
Order By Clause
Order By Clause are used to arrange the data either in ascending or descending order. Bydefault order by clause arrange or short the data in ascending order. To sort the records in a descending order, you can use the DESC keyword.
Note: You can apply order by clause on more than one column in the same table
Syntax
SELECT column_name, column_name FROM table_name ORDER BY column_name ASC|DESC, column_name ASC|DESC;
Example Order by
Employee table
emp_id | name | salary |
---|---|---|
101 | Amit | 24000 |
102 | Rahul | 34000 |
103 | Sultan | 54000 |
104 | Gaurav | 26000 |
105 | Hitesh | 35000 |
After apply join on these two table, result show like below;
Example
SELECT * FROM Employee ORDER BY name;
Result after apply order by on Employee table.
emp_id | name | salary |
---|---|---|
101 | Amit | 24000 |
104 | Gaurav | 26000 |
105 | Hitesh | 35000 |
102 | Rahul | 34000 |
103 | Sultan | 54000 |
Order by in descending order
This is an example to sort the result in descending order by age.
Example
SELECT * FROM Employee ORDER BY salary DESC;
Result after apply order by on Employee table.
emp_id | name | salary |
---|---|---|
103 | Sultan | 54000 |
105 | Hitesh | 35000 |
102 | Rahul | 34000 |
104 | Gaurav | 26000 |
101 | Amit | 24000 |
Google Advertisment