oracle
  1. oracle-order-by

ORDER BY - (Oracle Clauses)

The ORDER BY clause is used in Oracle SQL to sort the results of a query in either ascending or descending order based on one or more columns. This clause is used to organize the results in a meaningful way for better interpretation and readability.

Syntax

The syntax for using the ORDER BY in Oracle SQL is as follows:

SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC | DESC], column2 [ASC | DESC], ...;

Here, SELECT is used to select the columns from the table, FROM is used to specify the table to select the data from, and ORDER BY is used to sort the result set with the specified column(s) in ascending (ASC) or descending (DESC) order.

Example

For example, if we have a table customers with columns id, name, age, and address, we can use the following syntax to sort the results in ascending order based on the name column:

SELECT id, name, age, address
FROM customers
ORDER BY name ASC;

Output

The output of the above SQL query would be as follows:

1   Amy     20  123 Main St.
3   John    35  456 Elm St.
2   Susan   25  789 Oak St.

Explanation

In the above example, we have selected the columns id, name, age, and address from the customers table and sorted the result set in ascending order based on the name column.

Use

The ORDER BY clause is useful when we need to organize the results of our query in a meaningful way. This is especially important when we are working with large datasets and need to quickly find or analyze data.

Important Points

  • The ORDER BY clause is used to sort the result set of a query based on one or more columns.
  • The ORDER BY clause can sort in ascending or descending order.
  • The ORDER BY clause is useful when organizing large datasets.

Summary

In summary, the ORDER BY clause in Oracle SQL is used to sort the result set of a query based on one or more columns. This clause can sort in ascending or descending order and is useful when dealing with large datasets.

Published on: