In the vast realm of database management, Oracle SQL is a powerful tool that enables users to manipulate and retrieve data effortlessly. One of the most fundamental aspects of data querying is sorting results based on specific criteria, which is achieved using the `ORDER BY` clause in SQL. Let's delve into the world of Oracle SQL and explore various `ORDER BY` examples to help you master this crucial skill.

Before we dive into the specifics, let's briefly recap the basic structure of the `ORDER BY` clause. It is typically used at the end of a SQL SELECT statement and follows this syntax:

`ORDER BY column1 [ASC/DESC], column2 [ASC/DESC], ...`
Here, `ASC` stands for 'Ascending', which sorts the results in ascending order (from A-Z or smallest to largest). Conversely, `DESC` represents 'Descending', sorting the results in descending order (from Z-A or largest to smallest). If no sorting order is specified, SQL defaults to `ASC`. Now, let's dive into the meat of our discussion with two primary topics: ordering by single columns and ordering by multiple columns.
Ordering by Single Columns

Ordering data based on a single column is the most common use of the `ORDER BY` clause. Let's illustrate this with a few examples using a hypothetical `employees` table with columns `id`, `last_name`, `department`, and `salary`.
First, let's sort employees by their last names in ascending order:

```sql SELECT * FROM employees ORDER BY last_name; ```
This query will return all employee records sorted by their last names in alphabetical order. If you want to sort by last names in descending order, you can modify the query like this:
```sql SELECT * FROM employees ORDER BY last_name DESC; ```
Ordering by Numeric Columns
Sorting by numeric columns, such as `salary`, works similarly. Here's how you can sort employees by their salaries in ascending order:

```sql SELECT * FROM employees ORDER BY salary; ```
To sort employees by salary in descending order (from highest to lowest), use the `DESC` keyword:
```sql SELECT * FROM employees ORDER BY salary DESC; ```
Ordering by Date Columns
Oracle SQL also allows you to sort data based on date columns. Let's assume our `employees` table has a `hire_date` column. You can sort employees by their hire dates like this:

```sql SELECT * FROM employees ORDER BY hire_date; ```
To sort by hire dates in descending order (from most recent to oldest), use the `DESC` keyword:
```sql SELECT * FROM employees ORDER BY hire_date DESC; ```
Ordering by Multiple Columns


















![[Oracle DB]Oracle SQL order by 排序資料 ASC及DESC用法教學](https://i.pinimg.com/originals/f0/7e/aa/f07eaa182e92c3cf4dce0fcde14b78b2.png)
Oracle SQL enables you to sort data based on multiple columns, which is particularly useful when you need to prioritize results based on several criteria. Suppose you want to list all employees, sorted first by their departments in ascending order, and then by their last names in ascending order. Here's how you can achieve this:
```sql SELECT * FROM employees ORDER BY department, last_name; ```
In this example, employees within the same department will be sorted by their last names. If you want to sort departments in descending order and last names in ascending order, you can modify the query like this:
```sql SELECT * FROM employees ORDER BY department DESC, last_name; ```
Sorting Using Aliases
When writing complex queries, you may need to order data based on column aliases. Let's create an alias for the `salary` column and sort employees by this alias in descending order:
```sql SELECT id, last_name, department, salary AS 'Annual Salary' FROM employees ORDER BY 'Annual Salary' DESC; ```
It's essential to note that sorting by an alias works only if the alias is created within the same SELECT statement. Sorting by column aliases from another SELECT statement or subquery is not allowed.
Null Values in Ordering
In Oracle SQL, null values are considered 'less than' any other value when sorting in ascending order. Therefore, records with null values will always appear first in the result set. Conversely, null values are considered 'greater than' any other value when sorting in descending order, making null records appear last. Here's an example using the `salary` column and assuming some employees have null salary values:
```sql SELECT * FROM employees ORDER BY salary; SELECT * FROM employees ORDER BY salary DESC; ```
In the first query, employees with null salaries will appear first. In the second query, these employees will be at the end of the result set.
Congratulations! You've just explored various `ORDER BY` examples in Oracle SQL and learned how to sort data based on single and multiple columns. Now that you're equipped with these powerful sorting techniques, you can manipulate and retrieve data more efficiently in your projects. Happy coding, and may your queries always return exactly what you need!