- About
- What is sql
- Sql Syntax
- Sql Data types
- Sql Operators
- Sql Create Database
- Sql Drop Database
- Sql Create Table
- Sql Drop Table
- Sql Insert Query
- Sql Select Query
- Sql Where Clause
- Sql Update Query
- Sql Delete Query
- Sql Like Clause
- Sql Order By Clause
- Sql Group By Clause
- Sql Distinct Keyword
- Sql Constraints
Sql Order By Clause
ORDER BY clause is used in a SELECT statement to sort results of SELECT in either ascending or descending order. Oracle by default sorts query results in ascending order.
We can use more than one column in the ORDER BY clause.
Syntax
SELECT column-list
FROM tableName
[WHERE condition]
[ORDER BY column1, column2, .. columnN] [ASC | DESC];
FROM tableName
[WHERE condition]
[ORDER BY column1, column2, .. columnN] [ASC | DESC];
Consider the table Employee having following records:
FirstName | LastName | DOB | Phone | Salary | |
---|---|---|---|---|---|
Jagdish | Jain | jagdish@yahoo.com | 6/12/1988 | 452-345-236 | 15000 |
Ramesh | Patel | ramesh@gmail.com | 7/12/1988 | 147-852-258 | 18000 |
Rahul | Tanvar | rahul@gmail.com | 5/24/1988 | 233-234-234 | 20000 |
Jai | Soni | js@gmail.co.uk | 20/12/1990 | 789-525-258 | 25000 |
Demo
SELECT * FROM EMPLOYEE
ORDER BY FirstName, LastName;
Output
FirstName | LastName | DOB | Phone | Salary | |
---|---|---|---|---|---|
Jagdish | Jain | jagdish@yahoo.com | 6/12/1988 | 452-345-236 | 15000 |
Jai | Soni | js@gmail.co.uk | 20/12/1990 | 789-525-258 | 25000 |
Ramesh | Patel | ramesh@gmail.com | 7/12/1988 | 147-852-258 | 18000 |
Rahul | Tanvar | rahul@gmail.com | 5/24/1988 | 233-234-234 | 20000 |
Demo
SELECT * FROM EMPLOYEE
ORDER BY FirstName DESC;
Output
FirstName | LastName | DOB | Phone | Salary | |
---|---|---|---|---|---|
Ramesh | Patel | ramesh@gmail.com | 7/12/1988 | 147-852-258 | 18000 |
Rahul | Tanvar | rahul@gmail.com | 5/24/1988 | 233-234-234 | 20000 |
Jagdish | Jain | jagdish@yahoo.com | 6/12/1988 | 452-345-236 | 15000 |
Jai | Soni | js@gmail.co.uk | 20/12/1990 | 789-525-258 | 25000 |