> For the complete documentation index, see [llms.txt](https://www.pranaypourkar.co.in/the-programmers-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pranaypourkar.co.in/the-programmers-guide/database/sql-databases/resources-and-references/oreilly-sql-cookbook-2nd-edition/sorting-query-results.md).

# 2. Sorting Query Results

## Returning Query Results in a Specified Order

We want to order query result.

```
select ename,job,sal from emp where deptno = 10 order by sal asc;
select ename,job,sal from emp where deptno = 10 order by sal desc;
select ename,job,sal from emp where deptno = 10 order by 3 desc;
```

## Sorting by Multiple Fields

We want to sort the rows from EMP first by DEPTNO ascending, then by SALARY descending.

```
select empno,deptno,sal,ename,job from emp order by deptno, sal desc;
```

## Sorting by Substrings

We want to sort the results of a query by specific parts of a string. For example, we want to return employee names and jobs from table EMP and sort by the last two characters in the JOB field.

```
select ename,job from emp order by substr(job,length(job)-1);
```

## Sorting Mixed Alphanumeric Data

Use the functions REPLACE and TRANSLATE to modify the string for sorting.

{% hint style="info" %}
REPLACE

Replaces all occurrences of a specific substring within a string with another substring.

`REPLACE(string_expression, old_substring, new_substring)`

TRANSLATE

Translates characters within a string based on a translation table defined by another string.

`TRANSLATE(string_expression, from_string, to_string)`
{% endhint %}

```
/* ORDER BY ENAME */
select data
from V
order by replace(
translate(data,'0123456789','##########'),'#','')
```

## Dealing with Nulls When Sorting

We want to sort results from EMP by COMM, but the field is nullable. We need a way to specify whether nulls should come last.

{% hint style="info" %}
Do NULL values come first or last when we use ORDER BY?

The SQL standard does not define the default ordering of NULLs. If we apply the ORDER BY clause to a column with NULLs, the NULL values will be placed either first or last in the result set. The output depends on the database type.

For example, in Oracle, the NULL value records will come first.
{% endhint %}

```
/* NON-NULL COMM SORTED ASCENDING, ALL NULLS LAST */
select ename,sal,comm
from (
    select ename,sal,comm,
    case when comm is null then 0 else 1 end as is_null
    from emp
    ) x
order by is_null desc,comm
```

```
-- Oracle specific solution
/* NON-NULL COMM SORTED ASCENDING, ALL NULLS LAST */
select ename,sal,comm from emp order by comm nulls last;
```

## Sorting based on some specific data

We want to sort based on some conditional logic. For example, if JOB is SALESMAN, we want to sort on COMM; otherwise, we want to sort by SAL. Use a CASE expression in the ORDER BY clause.

```
select ename,sal,job,comm from emp order by 
case when job = 'SALESMAN' then comm else sal end;
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.pranaypourkar.co.in/the-programmers-guide/database/sql-databases/resources-and-references/oreilly-sql-cookbook-2nd-edition/sorting-query-results.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
