---
title: ORDER BY clause
description: Use the ORDER BY clause to sort data in ascending or descending order.
url: https://docs.influxdata.com/influxdb/v2/query-data/influxql/explore-data/order-by/
estimated_tokens: 2245
product: InfluxDB OSS v2
version: v2
---

# ORDER BY clause

This page documents an earlier version of InfluxDB OSS. [InfluxDB 3 Core](/influxdb3/core/) is the latest stable version.

#### API token hashing is enabled by default in InfluxDB OSS 2.9.0

Stronger token security: tokens are stored as hashes on disk, so a copy of the database file doesn’t expose usable tokens. Existing tokens are hashed on first startup and the original strings can’t be recovered afterward — **capture any plaintext tokens you still need before you upgrade**.

For more information, see [Token hashing](/influxdb/v2/admin/tokens/#token-hashing).

Use the `ORDER BY` clause to sort data.

-   [Syntax](#syntax)
-   [Examples](#examples)

## ORDER BY time DESC

By default, InfluxDB returns results in ascending time order; the first [point](/influxdb/v2/reference/glossary/#point) returned has the oldest [timestamp](/influxdb/v2/reference/glossary/#timestamp) and the last point returned has the most recent timestamp. `ORDER BY time DESC` reverses that order such that InfluxDB returns the points with the most recent timestamps first.

### Syntax

```sql
SELECT_clause FROM_clause [WHERE_clause] [GROUP_BY_clause] ORDER BY time DESC
```

If the query includes a `GROUP BY` clause, `ORDER by time DESC` must appear **after** the `GROUP BY` clause. If the query includes a `WHERE` clause and no `GROUP BY` clause, `ORDER by time DESC` must appear **after** the `WHERE` clause.

### Examples

[](#return-the-newest-points-first)

Return the newest points first

```sql
SELECT "water_level" FROM "h2o_feet" WHERE "location" = 'santa_monica' ORDER BY time DESC
```

Output:

Name: h2o\_feet

| time | water_level |
| --- | --- |
| 2019-09-17T21:42:00Z | 4.9380000000 |
| 2019-09-17T21:36:00Z | 5.0660000000 |
| 2019-09-17T21:30:00Z | 5.0100000000 |
| 2019-09-17T21:24:00Z | 5.0130000000 |
| 2019-09-17T21:18:00Z | 5.0720000000 |

The query returns the points with the most recent timestamps from the `h2o_feet` [measurement](/influxdb/v2/reference/glossary/#measurement) first. Without `ORDER by time DESC`, the query would return the following output:

Output:

Name: h2o\_feet

| time | water_level |
| --- | --- |
| 2019-08-17T00:00:00Z | 2.0640000000 |
| 2019-08-17T00:06:00Z | 2.1160000000 |
| 2019-08-17T00:12:00Z | 2.0280000000 |
| 2019-08-17T00:18:00Z | 2.1260000000 |

[](#return-the-newest-points-first-and-include-a-group-by-time-clause)

Return the newest points first and include a `GROUP BY time()` clause

```sql
SELECT MEAN("water_level") FROM "h2o_feet" WHERE time >= '2019-08-18T00:00:00Z' AND time <= '2019-08-18T00:42:00Z' GROUP BY time(12m) ORDER BY time DESC
```

Output:

Name: h2o\_feet

| time | mean |
| --- | --- |
| 2019-08-18T00:36:00Z | 4.9712860355 |
| 2019-08-18T00:24:00Z | 5.1682500000 |
| 2019-08-18T00:12:00Z | 5.3042500000 |
| 2019-08-18T00:00:00Z | 5.4135000000 |

The query uses the InfluxQL [MEAN() function](/influxdb/v2/query-data/influxql/functions/aggregates/#mean) and a time interval in the [GROUP BY clause](/influxdb/v2/query-data/influxql/explore-data/group-by/) to calculate the average `water_level` for each 12-minute interval in the queried time range. [`ORDER BY time DESC`](/influxdb/v2/query-data/influxql/explore-data/order-by/#order-by-time-desc) returns the most recent 12-minute time intervals first.

Without `ORDER BY time DESC`, the query would return the following output:

Output:

Name: h2o\_feet

| time | mean |
| --- | --- |
| 2019-08-18T00:00:00Z | 5.4135000000 |
| 2019-08-18T00:12:00Z | 5.3042500000 |
| 2019-08-18T00:24:00Z | 5.1682500000 |
| 2019-08-18T00:36:00Z | 4.9712860355 |
