Documentation

SELECT statement

Use the SELECT statement to query data from one or more measurements. The SELECT statement requires a SELECT clause and a FROM clause.

Syntax

SELECT field_expression[, ..., field_expression_n[, tag_expression[, ..., tag_expression_n]]] FROM measurement_expression[, ..., measurement_expression_n]

SELECT clause

The SELECT clause supports several formats for identifying data to query. It requires one or more field expressions and optional tag expressions.

  • field_expression: Expression to identify one or more fields to return in query results. Can be a field key, constant, regular expression, wildcard (*), or function expression and any combination of arithmetic operators.
  • tag_expression: Expression to identify one or more tags to return in query results. Can be a tag key or constant.

Select clause behaviors

  • SELECT field_key - Returns a specific field.
  • SELECT field_key1, field_key2 - Returns two specific fields.
  • SELECT field_key, tag_key - Returns a specific field and tag.
  • SELECT * - Returns all fields and tags. See Wildcard expressions.
  • SELECT /^[t]/ - Returns all fields and tags with keys that match the regular expression. At least one field key must match the regular expression. If no field keys match the regular expression, no results are returned.

FROM clause

The FROM clause specifies the measurement or subquery to query. It requires one or more comma-delimited measurement expressions or subqueries.

measurement_expression

A measurement expression identifies a measurement to query. It can be a measurement name, fully-qualified measurement, constant, or a regular expression.

  • Measurement name: When using just the measurement name, InfluxQL assumes the default retention policy of the database specified in the query request.

    FROM measurement
    
  • Fully-qualified measurement: A fully qualified measurement includes a database name, retention policy name, and measurement name, each separated by a period (.). If the retention policy is not specified, InfluxQL uses the default retention policy for the specified database.

FROM database.retention_policy.measurement

-- Fully-qualified measurement with default retention policy
FROM database..measurement

InfluxQL retention policies

In InfluxDB Cloud Serverless, retention policies are not part of the data model like they are in InfluxDB 1.x. Each InfluxDB Cloud Serverless database has a retention period which defines the maximum age of data to retain in the database. To use fully-qualified measurements in InfluxQL queries, use the following naming convention when creating a database:

database_name/retention_policy

Subquery

An InfluxQL subquery is a query nested in the FROM clause of an InfluxQL query. The outer query queries results returned by the inner query (subquery).

For more information, see InfluxQL subqueries.

Notable SELECT statement behaviors

Must query at least one field

A query requires at least one field key in the SELECT clause to return data. If the SELECT clause includes only tag keys, the query returns an empty result. When using regular expressions in the SELECT clause, if regular expression matches only tag keys and no field keys, the query returns an empty result.

To return data associated with tag keys, include at least one field key in the SELECT clause.

Wildcard expressions

When using a wildcard expression (*) in the SELECT clause, the query returns all tags and fields. If a function is applied to a wildcard expression, the query returns all fields with the function applied, but does not return tags unless they are included in the SELECT clause.

Cannot include both aggregate and non-aggregate field expressions

The SELECT statement cannot include an aggregate field expression (one that uses an aggregate or selector function) and a non-aggregate field expression. For example, in the following query, an aggregate function is applied to one field, but not the other:

SELECT mean(temp), hum FROM home

This query returns an error. For more information, see error about mixing aggregate and non-aggregate queries.

Data types and casting operations

The SELECT clause supports specifying a field’s type and basic casting operations with the :: syntax.

SELECT field_expression::type FROM measurement_expression

The :: syntax allows users to perform basic cast operations in queries. Currently, InfluxQL supports casting numeric field values to other numeric types. Casting to an identifier type acts as a filter on results and returns only columns of that specific identifier type along with the time column.

Numeric types
  • float
  • integer
  • unsigned
Non-numeric types
  • string
  • boolean
Identifier types
  • field
  • tag

InfluxQL returns no data if the query attempts to cast a numeric value to a non-numeric type and vice versa.

When casting a float value to an integer or unsigned integer, the float value is truncated at the decimal point. No rounding is performed.

SELECT statement examples

The examples below use the following sample data sets:

Select all fields and tags from a measurement

Select specific tags and fields from a measurement

Select all fields from a measurement

Select a field from a measurement and perform basic arithmetic

Select all data from more than one measurement

Select all data from a fully-qualified measurement (with default retention policy)

Type-casting examples

Cast an integer field to a float

Cast a float field to an integer

Cast a float field to an unsigned integer


Was this page helpful?

Thank you for your feedback!


The future of Flux

Flux is going into maintenance mode. You can continue using it as you currently are without any changes to your code.

Read more

InfluxDB v3 enhancements and InfluxDB Clustered is now generally available

New capabilities, including faster query performance and management tooling advance the InfluxDB v3 product line. InfluxDB Clustered is now generally available.

InfluxDB v3 performance and features

The InfluxDB v3 product line has seen significant enhancements in query performance and has made new management tooling available. These enhancements include an operational dashboard to monitor the health of your InfluxDB cluster, single sign-on (SSO) support in InfluxDB Cloud Dedicated, and new management APIs for tokens and databases.

Learn about the new v3 enhancements


InfluxDB Clustered general availability

InfluxDB Clustered is now generally available and gives you the power of InfluxDB v3 in your self-managed stack.

Talk to us about InfluxDB Clustered

InfluxDB Cloud Serverless