ORACLE | ||||||||
| ||||||||
| ||||||||
| ||||||||
| ||||||||
| ||||||||
| ||||||||
| ||||||||
|
Oracle - Order By
In Oracle, ORDER BY Clause is used to sort or re-arrange the records in the result set. The ORDER BY clause is only used with SELECT statement.
SELECT expressions
FROM tables
WHERE conditions
ORDER BY expression [ ASC | DESC ];
Let's take a table "supplier"
CREATE TABLE "SUPPLIER"
( "SUPPLIER_ID" NUMBER,
"FIRST_NAME" VARCHAR2(4000),
"LAST_NAME" VARCHAR2(4000)
)
/
SUPPLIER_ID | FIRST_NAME | LAST_NAME |
---|---|---|
1 | Ranveer | Desai |
2 | Noel | Sean |
3 | Renu | Kapoor |
SELECT *
FROM supplier
ORDER BY last_name;
SUPPLIER_ID | FIRST_NAME | LAST_NAME |
---|---|---|
1 | Ranveer | Desai |
3 | Renu | Kapoor |
2 | Noel | Sean |
The above example returns the first_name ordered by last_name in ascending order.
If you want to sort your result in descending order, you should use the DESC attribute in your ORDER BY clause:
SELECT *
FROM supplier
ORDER BY last_name DESC;
SUPPLIER_ID | FIRST_NAME | LAST_NAME |
---|---|---|
2 | Noel | Sean |
3 | Renu | Kapoor |
1 | Ranveer | Desai |
The above example returns the first_name ordered by last_name in descending order.