Back to Influxdb

Logical

content/shared/sql-reference/operators/logical.md

latest11.2 KB
Original Source

Logical operators combine or manipulate conditions in a SQL query.

OperatorMeaning
ANDReturns true if both operands are true. Otherwise, returns false.{{< icon "link" >}}
BETWEENReturns true if the left operand is within the range of the right operand.{{< icon "link" >}}
EXISTSReturns true if the results of a subquery are not empty.{{< icon "link" >}}
INReturns true if the left operand is in the right operand list.{{< icon "link" >}}
LIKEReturns true if the left operand matches the right operand pattern string.{{< icon "link" >}}
NOTNegates the subsequent expression.{{< icon "link" >}}
ORReturns true if any operand is true. Otherwise, returns false.{{< icon "link" >}}

[!Note]

Sample data

Query examples on this page use the following sample data sets:

AND {.monospace}

The AND operand returns true if both operands are true. Otherwise, it returns false. This operator is typically used in the WHERE clause to combine multiple conditions.

{{< flex >}} {{% flex-content "two-thirds operator-example" %}}

sql
SELECT true AND false AS "AND condition"

{{% /flex-content %}} {{% flex-content "third operator-example" %}}

AND condition
false

{{% /flex-content %}} {{< /flex >}}

Examples

{{< expand-wrapper >}} {{% expand "AND operator in the WHERE clause" %}}

sql
SELECT *
FROM home
WHERE
  co > 10
  AND room = 'Kitchen'

{{% influxdb/custom-timestamps %}}

cohumroomtemptime
1836.9Kitchen23.32022-01-01T18:00:00Z
2236.6Kitchen23.12022-01-01T19:00:00Z
2636.5Kitchen22.72022-01-01T20:00:00Z

{{% /influxdb/custom-timestamps %}} {{% /expand %}} {{< /expand-wrapper >}}

BETWEEN {.monospace}

The BETWEEN operator returns true if the left numeric operand is within the range specified in the right operand. Otherwise, it returns false

{{< flex >}} {{% flex-content "two-thirds operator-example" %}}

sql
SELECT 6 BETWEEN 5 AND 8 AS "BETWEEN condition"

{{% /flex-content %}} {{% flex-content "third operator-example" %}}

BETWEEN condition
true

{{% /flex-content %}} {{< /flex >}}

Examples

{{< expand-wrapper >}} {{% expand "BETWEEN operator in the WHERE clause" %}}

sql
SELECT *
FROM home
WHERE
  co BETWEEN 5 AND 10

{{% influxdb/custom-timestamps %}}

cohumroomtemptime
736Kitchen22.42022-01-01T16:00:00Z
936Kitchen22.72022-01-01T17:00:00Z
535.9Living Room22.62022-01-01T17:00:00Z
936.2Living Room22.82022-01-01T18:00:00Z

{{% /influxdb/custom-timestamps %}} {{% /expand %}} {{< /expand-wrapper >}}

EXISTS {.monospace}

The EXISTS operator returns true if result of a correlated subquery is not empty. Otherwise it returns false.

See SQL subquery operators.

Examples

{{< expand-wrapper >}} {{% expand "EXISTS operator with a subquery in the WHERE clause" %}}

sql
SELECT *
FROM
  home home_actions
WHERE EXISTS (
  SELECT *
  FROM home
  WHERE
    home.co = home_actions.co - 1
)
ORDER BY time

{{% influxdb/custom-timestamps %}}

cohumroomtemptime
136.5Kitchen22.82022-01-01T13:00:00Z
136.3Kitchen22.82022-01-01T14:00:00Z
136.1Living Room22.32022-01-01T15:00:00Z
436Living Room22.42022-01-01T16:00:00Z
535.9Living Room22.62022-01-01T17:00:00Z
1836.9Kitchen23.32022-01-01T18:00:00Z

{{% /influxdb/custom-timestamps %}} {{% /expand %}} {{< /expand-wrapper >}}

IN {.monospace}

The IN operator returns true if the left operand is in the right operand list or subquery result. Otherwise, it returns false.

{{< flex >}} {{% flex-content "two-thirds operator-example" %}}

sql
SELECT 'John' IN ('Jane', 'John') AS "IN condition"

{{% /flex-content %}} {{% flex-content "third operator-example" %}}

IN condition
true

{{% /flex-content %}} {{< /flex >}}

See SQL subquery operators.

Examples

{{< expand-wrapper >}} {{% expand "IN operator with a list in the WHERE clause" %}}

sql
SELECT *
FROM home
WHERE
  room IN ('Bathroom', 'Bedroom', 'Kitchen')
LIMIT 4

{{% influxdb/custom-timestamps %}}

cohumroomtemptime
035.9Kitchen212022-01-01T08:00:00Z
036.2Kitchen232022-01-01T09:00:00Z
036.1Kitchen22.72022-01-01T10:00:00Z
036Kitchen22.42022-01-01T11:00:00Z

{{% /influxdb/custom-timestamps %}}

{{% /expand %}} {{% expand "IN operator with a subquery in the WHERE clause" %}}

sql
SELECT *
FROM home
WHERE
  room IN (
    SELECT DISTINCT room
    FROM home_actions
  )
ORDER BY time
LIMIT 4

{{% influxdb/custom-timestamps %}}

cohumroomtemptime
035.9Living Room21.12022-01-01T08:00:00Z
035.9Kitchen212022-01-01T08:00:00Z
035.9Living Room21.42022-01-01T09:00:00Z
036.2Kitchen232022-01-01T09:00:00Z

{{% /influxdb/custom-timestamps %}} {{% /expand %}} {{< /expand-wrapper >}}

LIKE {.monospace}

The LIKE operator returns true if the left operand matches the string pattern specified in the right operand. LIKE expressions support SQL wildcard characters.

{{< flex >}} {{% flex-content "two-thirds operator-example" %}}

sql
SELECT 'John' LIKE 'J_%n' AS "LIKE condition"

{{% /flex-content %}} {{% flex-content "third operator-example" %}}

LIKE condition
true

{{% /flex-content %}} {{< /flex >}}

{{< expand-wrapper >}} {{% expand "LIKE operator in the WHERE clause" %}}

sql
SELECT *
FROM home
WHERE
  room LIKE '%Room'
LIMIT 4

{{% influxdb/custom-timestamps %}}

cohumroomtemptime
035.9Living Room21.12022-01-01T08:00:00Z
035.9Living Room21.42022-01-01T09:00:00Z
036Living Room21.82022-01-01T10:00:00Z
036Living Room22.22022-01-01T11:00:00Z

{{% /influxdb/custom-timestamps %}} {{% /expand %}} {{< /expand-wrapper >}}

SQL wildcard characters

The InfluxDB SQL implementation supports the following wildcard characters when using the LIKE operator to match strings to a pattern.

CharacterDescription
%Represents zero or more characters
_Represents any single character

NOT {.monospace}

The NOT operator negates the subsequent expression.

{{< flex >}} {{% flex-content "two-thirds operator-example" %}}

sql
SELECT NOT true AS "NOT condition"

{{% /flex-content %}} {{% flex-content "third operator-example" %}}

NOT condition
false

{{% /flex-content %}} {{< /flex >}}

Examples

{{< expand-wrapper >}} {{% expand "NOT IN" %}}

sql
SELECT *
FROM home
WHERE
  room NOT IN ('Kitchen', 'Bathroom')
LIMIT 4

{{% influxdb/custom-timestamps %}}

cohumroomtemptime
035.9Living Room21.12022-01-01T08:00:00Z
035.9Living Room21.42022-01-01T09:00:00Z
036Living Room21.82022-01-01T10:00:00Z
036Living Room22.22022-01-01T11:00:00Z

{{% /influxdb/custom-timestamps %}} {{% /expand %}}

{{% expand "NOT EXISTS" %}}

sql
SELECT *
FROM
  home home_actions
WHERE NOT EXISTS (
  SELECT *
  FROM home
  WHERE
    home.co = home_actions.co + 4
)
ORDER BY time

{{% influxdb/custom-timestamps %}}

cohumroomtemptime
736Kitchen22.42022-01-01T16:00:00Z
436Living Room22.42022-01-01T16:00:00Z
936Kitchen22.72022-01-01T17:00:00Z
936.2Living Room22.82022-01-01T18:00:00Z
1736.4Living Room22.22022-01-01T20:00:00Z
2636.5Kitchen22.72022-01-01T20:00:00Z

{{% /influxdb/custom-timestamps %}} {{% /expand %}}

{{% expand "NOT BETWEEN" %}}

sql
SELECT *
FROM home
WHERE
  co NOT BETWEEN 1 AND 22
  AND room = 'Kitchen'

{{% influxdb/custom-timestamps %}}

cohumroomtemptime
035.9Kitchen212022-01-01T08:00:00Z
036.2Kitchen232022-01-01T09:00:00Z
036.1Kitchen22.72022-01-01T10:00:00Z
036Kitchen22.42022-01-01T11:00:00Z
036Kitchen22.52022-01-01T12:00:00Z
2636.5Kitchen22.72022-01-01T20:00:00Z

{{% /influxdb/custom-timestamps %}} {{% /expand %}} {{< /expand-wrapper >}}

OR {.monospace}

The OR operator returns true if any operand is true. Otherwise, it returns false. This operator is typically used in the WHERE clause to combine multiple conditions.

{{< flex >}} {{% flex-content "two-thirds operator-example" %}}

sql
SELECT true OR false AS "OR condition"

{{% /flex-content %}} {{% flex-content "third operator-example" %}}

OR condition
true

{{% /flex-content %}} {{< /flex >}}

Examples

{{< expand-wrapper >}} {{% expand "OR in the WHERE clause" %}}

sql
SELECT *
FROM home
WHERE
  co > 20
  OR temp > 23

{{% influxdb/custom-timestamps %}}

cohumroomtemptime
1836.9Kitchen23.32022-01-01T18:00:00Z
2236.6Kitchen23.12022-01-01T19:00:00Z
2636.5Kitchen22.72022-01-01T20:00:00Z

{{% /influxdb/custom-timestamps %}} {{% /expand %}} {{< /expand-wrapper >}}