From 2c1375d9cbdbc14dd7544e5b8350caffb3694a74 Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Mon, 13 Nov 2023 09:12:50 -0700 Subject: [PATCH] SAL operators (#5225) * WIP SQL operators * add more descriptions for sql operators * Apply suggestions from code review Co-authored-by: Jason Stirnaman * port sql operator docs to serverless and clustered --------- Co-authored-by: Jason Stirnaman --- assets/styles/layouts/_article.scss | 1 + assets/styles/layouts/article/_flex.scss | 15 + .../cloud-dedicated/reference/sql/_index.md | 16 +- .../reference/sql/operators/_index.md | 18 + .../reference/sql/operators/arithmetic.md | 156 ++++++ .../reference/sql/operators/bitwise.md | 152 ++++++ .../reference/sql/operators/comparison.md | 289 +++++++++++ .../reference/sql/operators/logical.md | 463 ++++++++++++++++++ .../reference/sql/operators/other.md | 42 ++ .../reference/sql/subqueries.md | 2 +- .../cloud-serverless/reference/sql/_index.md | 16 +- .../reference/sql/operators/_index.md | 18 + .../reference/sql/operators/arithmetic.md | 156 ++++++ .../reference/sql/operators/bitwise.md | 152 ++++++ .../reference/sql/operators/comparison.md | 289 +++++++++++ .../reference/sql/operators/logical.md | 463 ++++++++++++++++++ .../reference/sql/operators/other.md | 42 ++ .../reference/sql/subqueries.md | 2 +- .../clustered/reference/sql/_index.md | 16 +- .../reference/sql/operators/_index.md | 18 + .../reference/sql/operators/arithmetic.md | 156 ++++++ .../reference/sql/operators/bitwise.md | 152 ++++++ .../reference/sql/operators/comparison.md | 289 +++++++++++ .../reference/sql/operators/logical.md | 463 ++++++++++++++++++ .../reference/sql/operators/other.md | 42 ++ layouts/shortcodes/flex-content.html | 4 +- layouts/shortcodes/icon.html | 2 + 27 files changed, 3412 insertions(+), 22 deletions(-) create mode 100644 content/influxdb/cloud-dedicated/reference/sql/operators/_index.md create mode 100644 content/influxdb/cloud-dedicated/reference/sql/operators/arithmetic.md create mode 100644 content/influxdb/cloud-dedicated/reference/sql/operators/bitwise.md create mode 100644 content/influxdb/cloud-dedicated/reference/sql/operators/comparison.md create mode 100644 content/influxdb/cloud-dedicated/reference/sql/operators/logical.md create mode 100644 content/influxdb/cloud-dedicated/reference/sql/operators/other.md create mode 100644 content/influxdb/cloud-serverless/reference/sql/operators/_index.md create mode 100644 content/influxdb/cloud-serverless/reference/sql/operators/arithmetic.md create mode 100644 content/influxdb/cloud-serverless/reference/sql/operators/bitwise.md create mode 100644 content/influxdb/cloud-serverless/reference/sql/operators/comparison.md create mode 100644 content/influxdb/cloud-serverless/reference/sql/operators/logical.md create mode 100644 content/influxdb/cloud-serverless/reference/sql/operators/other.md create mode 100644 content/influxdb/clustered/reference/sql/operators/_index.md create mode 100644 content/influxdb/clustered/reference/sql/operators/arithmetic.md create mode 100644 content/influxdb/clustered/reference/sql/operators/bitwise.md create mode 100644 content/influxdb/clustered/reference/sql/operators/comparison.md create mode 100644 content/influxdb/clustered/reference/sql/operators/logical.md create mode 100644 content/influxdb/clustered/reference/sql/operators/other.md diff --git a/assets/styles/layouts/_article.scss b/assets/styles/layouts/_article.scss index 130d44d22..e720313ab 100644 --- a/assets/styles/layouts/_article.scss +++ b/assets/styles/layouts/_article.scss @@ -23,6 +23,7 @@ & + .highlight pre { margin-top: .5rem } & + pre { margin-top: .5rem } & + .code-tabs-wrapper { margin-top: 0; } + &.monospace { font-family: $code; } &.green { color: $gr-rainforest; } &.orange { color: $r-dreamsicle; } &[metadata]::after { diff --git a/assets/styles/layouts/article/_flex.scss b/assets/styles/layouts/article/_flex.scss index bc0c314c0..c9e997f67 100644 --- a/assets/styles/layouts/article/_flex.scss +++ b/assets/styles/layouts/article/_flex.scss @@ -21,6 +21,21 @@ img { margin-bottom: 0;} table { display: table; } p:last-child {margin-bottom: 0.5rem;} + + // Addition flex content container classes + &.operator-example { + .highlight:first-child .codeblock pre { + margin-top: 0; + margin-bottom: 1.5rem; + } + table:first-child { + margin-top: 0; + margin-bottom: 1.5rem; + + th {font-size: 1.05rem;} + th,td {padding: .65rem 1.15rem;} + } + } } //////////////////////////////////////////////////////////////////////////////// diff --git a/content/influxdb/cloud-dedicated/reference/sql/_index.md b/content/influxdb/cloud-dedicated/reference/sql/_index.md index a6b0f92a7..cbf9e4c97 100644 --- a/content/influxdb/cloud-dedicated/reference/sql/_index.md +++ b/content/influxdb/cloud-dedicated/reference/sql/_index.md @@ -171,11 +171,15 @@ Comparison operators evaluate the relationship between the left and right operan ### Logical operators -| Operator | Meaning | -| :------: | :---------------------------------------------------------------- | -| `AND` | Returns true if both operands are true. Otherwise, returns false. | -| `OR` | Returns true if any operand is true. Otherwise, returns false. | -| `NOT` | Negates the subsequent expression | +| Operator | Meaning | +| :-------: | :------------------------------------------------------------------------- | +| `AND` | Returns true if both operands are true. Otherwise, returns false. | +| `BETWEEN` | Returns true if the left operand is within the range of the right operand. | +| `EXISTS` | Returns true if the operand is not null. | +| `IN` | Returns true if the left operand is in the right operand list. | +| `LIKE` | Returns true if the left operand matches the right operand pattern string. | +| `NOT` | Negates the subsequent expression. | +| `OR` | Returns true if any operand is true. Otherwise, returns false. | ### Bitwise operators @@ -185,7 +189,6 @@ Bitwise operators perform bitwise operations on bit patterns or binary numerals. | :------: | :------------------ | :------- | -----: | | `&` | Bitwise and | `5 & 3` | `1` | | `\|` | Bitwise or | `5 \| 3` | `7` | -| `#` | Bitwise xor | `5 # 3` | `6` | | `^` | Bitwise xor | `5 ^ 3` | `6` | | `>>` | Bitwise shift right | `5 >> 3` | `0` | | `<<` | Bitwise shift left | `5 << 3` | `40` | @@ -220,6 +223,7 @@ IN INNER JOIN JOIN LEFT JOIN +LIKE LIMIT NOT EXISTS diff --git a/content/influxdb/cloud-dedicated/reference/sql/operators/_index.md b/content/influxdb/cloud-dedicated/reference/sql/operators/_index.md new file mode 100644 index 000000000..18d745431 --- /dev/null +++ b/content/influxdb/cloud-dedicated/reference/sql/operators/_index.md @@ -0,0 +1,18 @@ +--- +title: SQL operators +description: > + SQL operators are reserved words or characters which perform certain operations, + including comparisons and arithmetic. +menu: + influxdb_cloud_dedicated: + name: Operators + parent: SQL reference +weight: 211 +--- + +SQL operators are reserved words or characters which perform certain operations, +including comparisons and arithmetic. + +{{< children type="anchored-list" >}} + +{{< children hlevel="h2" >}} diff --git a/content/influxdb/cloud-dedicated/reference/sql/operators/arithmetic.md b/content/influxdb/cloud-dedicated/reference/sql/operators/arithmetic.md new file mode 100644 index 000000000..d1ea4e129 --- /dev/null +++ b/content/influxdb/cloud-dedicated/reference/sql/operators/arithmetic.md @@ -0,0 +1,156 @@ +--- +title: SQL arithmetic operators +list_title: Arithmetic operators +description: > + Arithmetic operators take two numeric values (either literals or variables) + and perform a calculation that returns a single numeric value. +menu: + influxdb_cloud_dedicated: + name: Arithmetic operators + parent: Operators +weight: 301 +list_code_example: | + | Operator | Description | Example | Result | + | :------: | :------------- | ------- | -----: | + | `+` | Addition | `2 + 2` | `4` | + | `-` | Subtraction | `4 - 2` | `2` | + | `*` | Multiplication | `2 * 3` | `6` | + | `/` | Division | `6 / 3` | `2` | + | `%` | Modulo | `7 % 2` | `1` | +--- + +Arithmetic operators take two numeric values (either literals or variables) +and perform a calculation that returns a single numeric value. + +| Operator | Description | | +| :------: | :------------- | :------------------------------------- | +| `+` | Addition | [{{< icon "link" >}}](#addition) | +| `-` | Subtraction | [{{< icon "link" >}}](#subtraction) | +| `*` | Multiplication | [{{< icon "link" >}}](#multiplication) | +| `/` | Division | [{{< icon "link" >}}](#division) | +| `%` | Modulo | [{{< icon "link" >}}](#modulo) | + +## + {#addition .monospace} + +The `+` operator adds two operands together and returns the sum. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 1 + 2 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| int64(1) + int64(2) | +| ------------------: | +| 3 | + +{{% /flex-content %}} +{{< /flex >}} + +## - {#subtraction .monospace} + +The `-` operator subtracts the right operand from the left operand and returns +the difference. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 4 - 2 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| int64(4) - int64(2) | +| ------------------: | +| 2 | + +{{% /flex-content %}} +{{< /flex >}} + +## * {#multiplication .monospace} + +The `*` operator multiplies two operands together and returns the product. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 2 * 3 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| int64(2) * int64(3) | +| ------------------: | +| 6 | + +{{% /flex-content %}} +{{< /flex >}} + +## / {#division .monospace} + +The `/` operator divides the left operand by the right operand and returns the quotient. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 6 / 3 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| int64(6) / int64(3) | +| ------------------: | +| 2 | + +{{% /flex-content %}} +{{< /flex >}} + +## % {#modulo .monospace} + +The `%` (modulo) operator divides the left operand by the right operand and returns the +remainder. If the left operand is not divisible by the right operand, it returns +the left operand. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 8 % 3 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(8) % Int64(3) | +| ------------------: | +| 2 | + +{{% /flex-content %}} +{{< /flex >}} + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 3 % 8 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(3) % Int64(8) | +| ------------------: | +| 3 | + +{{% /flex-content %}} +{{< /flex >}} diff --git a/content/influxdb/cloud-dedicated/reference/sql/operators/bitwise.md b/content/influxdb/cloud-dedicated/reference/sql/operators/bitwise.md new file mode 100644 index 000000000..7e18b6562 --- /dev/null +++ b/content/influxdb/cloud-dedicated/reference/sql/operators/bitwise.md @@ -0,0 +1,152 @@ +--- +title: SQL bitwise operators +list_title: Bitwise operators +description: > + Bitwise operators perform bitwise operations on bit patterns or binary numerals. +menu: + influxdb_cloud_dedicated: + name: Bitwise operators + parent: Operators +weight: 304 +list_code_example: | + | Operator | Meaning | Example | Result | + | :------: | :------------------ | :------- | -----: | + | `&` | Bitwise and | `5 & 3` | `1` | + | `\|` | Bitwise or | `5 \| 3` | `7` | + | `^` | Bitwise xor | `5 ^ 3` | `6` | + | `>>` | Bitwise shift right | `5 >> 3` | `0` | + | `<<` | Bitwise shift left | `5 << 3` | `40` | +--- + +Bitwise operators perform bitwise operations on bit patterns or binary numerals. + +| Operator | Meaning | | +| :------: | :------------------ | :------------------------------------------ | +| `&` | Bitwise and | [{{< icon "link" >}}](#bitwise-and) | +| `\|` | Bitwise or | [{{< icon "link" >}}](#bitwise-or) | +| `^` | Bitwise xor | [{{< icon "link" >}}](#bitwise-xor) | +| `>>` | Bitwise shift right | [{{< icon "link" >}}](#bitwise-shift-right) | +| `<<` | Bitwise shift left | [{{< icon "link" >}}](#bitwise-shift-left) | + +## & {#bitwise-and .monospace} + +The `&` (bitwise AND) operator compares each bit of the left operand to the +corresponding bit of the right operand. +If both bits are 1, the corresponding result bit is set to 1. +Otherwise, the corresponding result bit is set to 0. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 5 & 3 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(5) & Int64(3) | +| ------------------: | +| 1 | + +{{% /flex-content %}} +{{< /flex >}} + +## \| {#bitwise-or .monospace} + +The `|` (bitwise OR or inclusive OR) operator compares each bit of the left +operand to the corresponding bit of the right operand. +If either bit is 1, the corresponding result bit is set to 1. +Otherwise, the corresponding result bit is set to 0. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 5 | 3 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(5) \| Int64(3) | +| -------------------: | +| 7 | + +{{% /flex-content %}} +{{< /flex >}} + +## ^ {#bitwise-xor .monospace} + +The `^` (bitwise XOR or exclusive OR) operator compares each bit of the left +operand to the corresponding bit of the right operand. +If the bit in one of the operands is 0 and the bit in the other operand is 1, +the corresponding result bit is set to 1. +Otherwise, the corresponding result bit is set to 0. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 5 ^ 3 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(5) BIT_XOR Int64(3) | +| ------------------------: | +| 6 | + +{{% /flex-content %}} +{{< /flex >}} + +## \>\> {#bitwise-shift-right .monospace} + +The `>>` (bitwise shift right) operator shifts the bits in the left operand to +the right by the number of positions specified in the right operand. +For unsigned numbers, bit positions vacated by the shift operation are filled with 0. +For signed numbers, the sign bit is used to fill the vacated bit positions. +If the number is positive, the bit position is filled with 0. +If the number is negative, the bit position is filled with 1. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 5 >> 3 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(5) \>\> Int64(3) | +| ---------------------: | +| 0 | + +{{% /flex-content %}} +{{< /flex >}} + +## \<\< {#bitwise-shift-left .monospace} + +The `<<` (bitwise shift left) operator shifts the bits in the left operand to +the left by the number of positions specified in the right operand. +Bit positions vacated by the shift operation are filled with 0. +Bits that shift off the end are discarded, including the sign bit. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 5 << 3 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(5) \<\< Int64(3) | +| ---------------------: | +| 40 | + +{{% /flex-content %}} +{{< /flex >}} diff --git a/content/influxdb/cloud-dedicated/reference/sql/operators/comparison.md b/content/influxdb/cloud-dedicated/reference/sql/operators/comparison.md new file mode 100644 index 000000000..c875d4b2a --- /dev/null +++ b/content/influxdb/cloud-dedicated/reference/sql/operators/comparison.md @@ -0,0 +1,289 @@ +--- +title: SQL comparison operators +list_title: Comparison operators +description: > + Comparison operators evaluate the relationship between the left and right + operands and returns `true` or `false`. +menu: + influxdb_cloud_dedicated: + name: Comparison operators + parent: Operators +weight: 302 +list_code_example: | + | Operator | Meaning | Example | + | :------: | :------------------------------------------------------- | :---------------- | + | `=` | Equal to | `123 = 123` | + | `<>` | Not equal to | `123 <> 456` | + | `!=` | Not equal to | `123 != 456` | + | `>` | Greater than | `3 > 2` | + | `>=` | Greater than or equal to | `3 >= 2` | + | `<` | Less than | `1 < 2` | + | `<=` | Less than or equal to | `1 <= 2` | + | `~` | Matches a regular expression | `'abc' ~ 'a.*'` | + | `~*` | Matches a regular expression _(case-insensitive)_ | `'Abc' ~* 'A.*'` | + | `!~` | Does not match a regular expression | `'abc' !~ 'd.*'` | + | `!~*` | Does not match a regular expression _(case-insensitive)_ | `'Abc' !~* 'a.*'` | +--- + +Comparison operators evaluate the relationship between the left and right +operands and returns `true` or `false`. + + +| Operator | Meaning | | +| :------: | :------------------------------------------------------- | :------------------------------------------------------ | +| `=` | Equal to | [{{< icon "link" >}}](#equal-to) | +| `<>` | Not equal to | [{{< icon "link" >}}](#not-equal-to) | +| `!=` | Not equal to | [{{< icon "link" >}}](#not-equal-to) | +| `>` | Greater than | [{{< icon "link" >}}](#greater-than) | +| `>=` | Greater than or equal to | [{{< icon "link" >}}](#greater-than-or-equal) | +| `<` | Less than | [{{< icon "link" >}}](#less-than) | +| `<=` | Less than or equal to | [{{< icon "link" >}}](#less-than-or-equal) | +| `~` | Matches a regular expression | [{{< icon "link" >}}](#regexp-match) | +| `~*` | Matches a regular expression _(case-insensitive)_ | [{{< icon "link" >}}](#regexp-match-case-insensitive) | +| `!~` | Does not match a regular expression | [{{< icon "link" >}}](#regexp-nomatch) | +| `!~*` | Does not match a regular expression _(case-insensitive)_ | [{{< icon "link" >}}](#regexp-nomatch-case-insensitive) | + +## = {#equal-to .monospace} + +The `=` operator compares the left and right operands and, if equal, returns `true`. +Otherwise returns `false`. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 123 = 123 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(123) = Int64(123) | +| :---------------------- | +| true | + +{{% /flex-content %}} +{{< /flex >}} + +## !=, <> {#not-equal-to .monospace} + +The `!=` and `<>` operators compare the left and right operands and, if not equal, +returns `true`. Otherwise returns `false`. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 123 != 456 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(123) != Int64(456) | +| :----------------------- | +| true | + +{{% /flex-content %}} +{{< /flex >}} + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 123 <> 456 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(123) != Int64(456) | +| :----------------------- | +| true | + +{{% /flex-content %}} +{{< /flex >}} + +## > {#greater-than .monospace} + +The `>` operator compares the left and right operands and, if the left operand +is greater than the right operand, returns `true`. +Otherwise returns `false`. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 3 > 2 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(3) > Int64(2) | +| :------------------ | +| true | + +{{% /flex-content %}} +{{< /flex >}} + +## >= {#greater-than-or-equal .monospace} + +The `>=` operator compares the left and right operands and, if the left operand +is greater than or equal to the right operand, returns `true`. +Otherwise returns `false`. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 3 >= 2 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(3) >= Int64(2) | +| :------------------- | +| true | + +{{% /flex-content %}} +{{< /flex >}} + +## < {#less-than .monospace} + +The `<` operator compares the left and right operands and, if the left operand +is less than the right operand, returns `true`. +Otherwise returns `false`. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 1 < 2 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int641(1) < Int64(2) | +| :------------------- | +| true | + +{{% /flex-content %}} +{{< /flex >}} + +## <= {#less-than-or-equal .monospace} + +The `<=` operator compares the left and right operands and, if the left operand +is less than or equal to the right operand, returns `true`. +Otherwise returns `false`. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 1 <= 2 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int641(1) <= Int64(2) | +| :-------------------- | +| true | + +{{% /flex-content %}} +{{< /flex >}} + +## ~ {#regexp-match .monospace} + +The `~` operator compares the left string operand to the right regular expression +operand and, if it matches (case-sensitive), returns `true`. +Otherwise returns `false`. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 'abc' ~ 'a.*' +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Utf8("abc") ~ Utf8("a.*") | +| :------------------------ | +| true | + +{{% /flex-content %}} +{{< /flex >}} + +## ~* {#regexp-match-case-insensitive .monospace} + +The `~*` operator compares the left string operand to the right regular expression +operand and, if it matches (case-insensitive), returns `true`. +Otherwise returns `false`. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 'Abc' ~* 'A.*' +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Utf8("Abc") ~* Utf8("A.*") | +| :------------------------- | +| true | + +{{% /flex-content %}} +{{< /flex >}} + +## !~ {#regexp-nomatch .monospace} + +The `!~` operator compares the left string operand to the right regular expression +operand and, if it does not match (case-sensitive), returns `true`. +Otherwise returns `false`. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 'abc' !~ 'd.*' +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Utf8("abc") !~ Utf8("d.*") | +| :------------------------- | +| true | + +{{% /flex-content %}} +{{< /flex >}} + +## !~* {#regexp-nomatch-case-insensitive .monospace} + +The `!~*` operator compares the left string operand to the right regular expression +operand and, if it does not match (case-insensitive), returns `true`. +Otherwise returns `false`. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 'Abc' !~* 'a.*' +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Utf8("Abc") !~* Utf8("a.*") | +| :-------------------------- | +| false | + +{{% /flex-content %}} +{{< /flex >}} diff --git a/content/influxdb/cloud-dedicated/reference/sql/operators/logical.md b/content/influxdb/cloud-dedicated/reference/sql/operators/logical.md new file mode 100644 index 000000000..92f75a27e --- /dev/null +++ b/content/influxdb/cloud-dedicated/reference/sql/operators/logical.md @@ -0,0 +1,463 @@ +--- +title: SQL logical operators +list_title: Logical operators +description: > + Logical operators combine or manipulate conditions in a SQL query. +menu: + influxdb_cloud_dedicated: + name: Logical operators + parent: Operators +weight: 303 +related: + - /influxdb/cloud-dedicated/reference/sql/where/ + - /influxdb/cloud-dedicated/reference/sql/subqueries/#subquery-operators, Subquery operators +list_code_example: | + | Operator | Meaning | + | :-------: | :------------------------------------------------------------------------- | + | `AND` | Returns true if both operands are true. Otherwise, returns false. | + | `BETWEEN` | Returns true if the left operand is within the range of the right operand. | + | `EXISTS` | Returns true if the results of a subquery are not empty. | + | `IN` | Returns true if the left operand is in the right operand list. | + | `LIKE` | Returns true if the left operand matches the right operand pattern string. | + | `NOT` | Negates the subsequent expression. | + | `OR` | Returns true if any operand is true. Otherwise, returns false. | +--- + +Logical operators combine or manipulate conditions in a SQL query. + +| Operator | Meaning | | +| :-------: | :------------------------------------------------------------------------- | :------------------------------ | +| `AND` | Returns true if both operands are true. Otherwise, returns false. | [{{< icon "link" >}}](#and) | +| `BETWEEN` | Returns true if the left operand is within the range of the right operand. | [{{< icon "link" >}}](#between) | +| `EXISTS` | Returns true if the results of a subquery are not empty. | [{{< icon "link" >}}](#exists) | +| `IN` | Returns true if the left operand is in the right operand list. | [{{< icon "link" >}}](#in) | +| `LIKE` | Returns true if the left operand matches the right operand pattern string. | [{{< icon "link" >}}](#like) | +| `NOT` | Negates the subsequent expression. | [{{< icon "link" >}}](#not) | +| `OR` | Returns true if any operand is true. Otherwise, returns false. | [{{< icon "link" >}}](#or) | + +{{% note %}} +#### Sample data + +Query examples on this page use the following sample data sets: + +- [Get started home sensor sample data](/influxdb/cloud-dedicated/reference/sample-data/#get-started-home-sensor-data) +- [Home sensor actions sample data](/influxdb/cloud-dedicated/reference/sample-data/#home-sensor-actions-data) +{{% /note %}} + +## 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](/influxdb/cloud-dedicated/reference/sql/where/) +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 %}} + +| co | hum | room | temp | time | +| --: | ---: | :------ | ---: | :------------------- | +| 18 | 36.9 | Kitchen | 23.3 | 2022-01-01T18:00:00Z | +| 22 | 36.6 | Kitchen | 23.1 | 2022-01-01T19:00:00Z | +| 26 | 36.5 | Kitchen | 22.7 | 2022-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 %}} + +| co | hum | room | temp | time | +| --: | ---: | :---------- | ---: | :------------------- | +| 7 | 36 | Kitchen | 22.4 | 2022-01-01T16:00:00Z | +| 9 | 36 | Kitchen | 22.7 | 2022-01-01T17:00:00Z | +| 5 | 35.9 | Living Room | 22.6 | 2022-01-01T17:00:00Z | +| 9 | 36.2 | Living Room | 22.8 | 2022-01-01T18:00:00Z | + +{{% /influxdb/custom-timestamps %}} +{{% /expand %}} +{{< /expand-wrapper >}} + +## EXISTS {.monospace} + +The `EXISTS` operator returns `true` if result of a +[correlated subquery](/influxdb/cloud-dedicated/reference/sql/subqueries/#correlated-subqueries) +is not empty. Otherwise it returns `false`. + +_See [SQL subquery operators](/influxdb/cloud-dedicated/reference/sql/subqueries/#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 %}} + +| co | hum | room | temp | time | +| --: | ---: | :---------- | ---: | :------------------- | +| 1 | 36.5 | Kitchen | 22.8 | 2022-01-01T13:00:00Z | +| 1 | 36.3 | Kitchen | 22.8 | 2022-01-01T14:00:00Z | +| 1 | 36.1 | Living Room | 22.3 | 2022-01-01T15:00:00Z | +| 4 | 36 | Living Room | 22.4 | 2022-01-01T16:00:00Z | +| 5 | 35.9 | Living Room | 22.6 | 2022-01-01T17:00:00Z | +| 18 | 36.9 | Kitchen | 23.3 | 2022-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](/influxdb/cloud-dedicated/reference/sql/subqueries/#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 %}} + +| co | hum | room | temp | time | +| --: | ---: | :------ | ---: | :------------------- | +| 0 | 35.9 | Kitchen | 21 | 2022-01-01T08:00:00Z | +| 0 | 36.2 | Kitchen | 23 | 2022-01-01T09:00:00Z | +| 0 | 36.1 | Kitchen | 22.7 | 2022-01-01T10:00:00Z | +| 0 | 36 | Kitchen | 22.4 | 2022-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 %}} + +| co | hum | room | temp | time | +| --: | ---: | :---------- | ---: | :------------------- | +| 0 | 35.9 | Living Room | 21.1 | 2022-01-01T08:00:00Z | +| 0 | 35.9 | Kitchen | 21 | 2022-01-01T08:00:00Z | +| 0 | 35.9 | Living Room | 21.4 | 2022-01-01T09:00:00Z | +| 0 | 36.2 | Kitchen | 23 | 2022-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](#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 %}} + +| co | hum | room | temp | time | +| --: | ---: | :---------- | ---: | :------------------- | +| 0 | 35.9 | Living Room | 21.1 | 2022-01-01T08:00:00Z | +| 0 | 35.9 | Living Room | 21.4 | 2022-01-01T09:00:00Z | +| 0 | 36 | Living Room | 21.8 | 2022-01-01T10:00:00Z | +| 0 | 36 | Living Room | 22.2 | 2022-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. + +| Character | Description | +| :-------: | :--------------------------------- | +| `%` | 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 %}} + +| co | hum | room | temp | time | +| --: | ---: | :---------- | ---: | :------------------- | +| 0 | 35.9 | Living Room | 21.1 | 2022-01-01T08:00:00Z | +| 0 | 35.9 | Living Room | 21.4 | 2022-01-01T09:00:00Z | +| 0 | 36 | Living Room | 21.8 | 2022-01-01T10:00:00Z | +| 0 | 36 | Living Room | 22.2 | 2022-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 %}} + +| co | hum | room | temp | time | +| --: | ---: | :---------- | ---: | :------------------- | +| 7 | 36 | Kitchen | 22.4 | 2022-01-01T16:00:00Z | +| 4 | 36 | Living Room | 22.4 | 2022-01-01T16:00:00Z | +| 9 | 36 | Kitchen | 22.7 | 2022-01-01T17:00:00Z | +| 9 | 36.2 | Living Room | 22.8 | 2022-01-01T18:00:00Z | +| 17 | 36.4 | Living Room | 22.2 | 2022-01-01T20:00:00Z | +| 26 | 36.5 | Kitchen | 22.7 | 2022-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 %}} + +| co | hum | room | temp | time | +| --: | ---: | :------ | ---: | :------------------- | +| 0 | 35.9 | Kitchen | 21 | 2022-01-01T08:00:00Z | +| 0 | 36.2 | Kitchen | 23 | 2022-01-01T09:00:00Z | +| 0 | 36.1 | Kitchen | 22.7 | 2022-01-01T10:00:00Z | +| 0 | 36 | Kitchen | 22.4 | 2022-01-01T11:00:00Z | +| 0 | 36 | Kitchen | 22.5 | 2022-01-01T12:00:00Z | +| 26 | 36.5 | Kitchen | 22.7 | 2022-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](/influxdb/cloud-dedicated/reference/sql/where/) +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 %}} + +| co | hum | room | temp | time | +| --: | ---: | :------ | ---: | :------------------- | +| 18 | 36.9 | Kitchen | 23.3 | 2022-01-01T18:00:00Z | +| 22 | 36.6 | Kitchen | 23.1 | 2022-01-01T19:00:00Z | +| 26 | 36.5 | Kitchen | 22.7 | 2022-01-01T20:00:00Z | + +{{% /influxdb/custom-timestamps %}} +{{% /expand %}} +{{< /expand-wrapper >}} diff --git a/content/influxdb/cloud-dedicated/reference/sql/operators/other.md b/content/influxdb/cloud-dedicated/reference/sql/operators/other.md new file mode 100644 index 000000000..2b4af90f2 --- /dev/null +++ b/content/influxdb/cloud-dedicated/reference/sql/operators/other.md @@ -0,0 +1,42 @@ +--- +title: Other SQL operators +list_title: Other operators +description: > + SQL supports other miscellaneous operators that perform various operations. +menu: + influxdb_cloud_dedicated: + name: Other operators + parent: Operators +weight: 305 +list_code_example: | + | Operator | Meaning | Example | Result | + | :------: | :------------------- | :---------------------- | :------------ | + | `\|\|` | Concatenate strings | `'Hello' \|\| ' world'` | `Hello world` | +--- + +SQL supports miscellaneous operators that perform various operations. + +| Operator | Meaning | | +| :------: | :------------------ | :------------------------------------------ | +| `\|\|` | Concatenate strings | [{{< icon "link" >}}](#concatenate-strings) | + +## || {#concatenate-strings} + +The `||` operator concatenates two string operands into a single string. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 'Hello' || ' world' AS "Concatenated" +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Concatenated | +| :----------- | +| Hello world | + +{{% /flex-content %}} +{{< /flex >}} diff --git a/content/influxdb/cloud-dedicated/reference/sql/subqueries.md b/content/influxdb/cloud-dedicated/reference/sql/subqueries.md index 03997238b..4a31860d2 100644 --- a/content/influxdb/cloud-dedicated/reference/sql/subqueries.md +++ b/content/influxdb/cloud-dedicated/reference/sql/subqueries.md @@ -51,7 +51,7 @@ Query examples on this page use the following sample data sets: ### [ NOT ] EXISTS The `EXISTS` operator returns all rows where a -_[correlated subquery](#correlated-subquery)_ produces one or more matches for +_[correlated subquery](#correlated-subqueries)_ produces one or more matches for that row. `NOT EXISTS` returns all rows where a _correlated subquery_ produces zero matches for that row. Only _correlated subqueries_ are supported. diff --git a/content/influxdb/cloud-serverless/reference/sql/_index.md b/content/influxdb/cloud-serverless/reference/sql/_index.md index 3576d881c..ff686924e 100644 --- a/content/influxdb/cloud-serverless/reference/sql/_index.md +++ b/content/influxdb/cloud-serverless/reference/sql/_index.md @@ -171,11 +171,15 @@ Comparison operators evaluate the relationship between the left and right operan ### Logical operators -| Operator | Meaning | -| :------: | :---------------------------------------------------------------- | -| `AND` | Returns true if both operands are true. Otherwise, returns false. | -| `OR` | Returns true if any operand is true. Otherwise, returns false. | -| `NOT` | Negates the subsequent expression | +| Operator | Meaning | +| :-------: | :------------------------------------------------------------------------- | +| `AND` | Returns true if both operands are true. Otherwise, returns false. | +| `BETWEEN` | Returns true if the left operand is within the range of the right operand. | +| `EXISTS` | Returns true if the operand is not null. | +| `IN` | Returns true if the left operand is in the right operand list. | +| `LIKE` | Returns true if the left operand matches the right operand pattern string. | +| `NOT` | Negates the subsequent expression. | +| `OR` | Returns true if any operand is true. Otherwise, returns false. | ### Bitwise operators @@ -185,7 +189,6 @@ Bitwise operators perform bitwise operations on bit patterns or binary numerals. | :------: | :------------------ | :------- | -----: | | `&` | Bitwise and | `5 & 3` | `1` | | `\|` | Bitwise or | `5 \| 3` | `7` | -| `#` | Bitwise xor | `5 # 3` | `6` | | `^` | Bitwise xor | `5 ^ 3` | `6` | | `>>` | Bitwise shift right | `5 >> 3` | `0` | | `<<` | Bitwise shift left | `5 << 3` | `40` | @@ -220,6 +223,7 @@ IN INNER JOIN JOIN LEFT JOIN +LIKE LIMIT NOT EXISTS diff --git a/content/influxdb/cloud-serverless/reference/sql/operators/_index.md b/content/influxdb/cloud-serverless/reference/sql/operators/_index.md new file mode 100644 index 000000000..306f327a4 --- /dev/null +++ b/content/influxdb/cloud-serverless/reference/sql/operators/_index.md @@ -0,0 +1,18 @@ +--- +title: SQL operators +description: > + SQL operators are reserved words or characters which perform certain operations, + including comparisons and arithmetic. +menu: + influxdb_cloud_serverless: + name: Operators + parent: SQL reference +weight: 211 +--- + +SQL operators are reserved words or characters which perform certain operations, +including comparisons and arithmetic. + +{{< children type="anchored-list" >}} + +{{< children hlevel="h2" >}} diff --git a/content/influxdb/cloud-serverless/reference/sql/operators/arithmetic.md b/content/influxdb/cloud-serverless/reference/sql/operators/arithmetic.md new file mode 100644 index 000000000..50f36d099 --- /dev/null +++ b/content/influxdb/cloud-serverless/reference/sql/operators/arithmetic.md @@ -0,0 +1,156 @@ +--- +title: SQL arithmetic operators +list_title: Arithmetic operators +description: > + Arithmetic operators take two numeric values (either literals or variables) + and perform a calculation that returns a single numeric value. +menu: + influxdb_cloud_serverless: + name: Arithmetic operators + parent: Operators +weight: 301 +list_code_example: | + | Operator | Description | Example | Result | + | :------: | :------------- | ------- | -----: | + | `+` | Addition | `2 + 2` | `4` | + | `-` | Subtraction | `4 - 2` | `2` | + | `*` | Multiplication | `2 * 3` | `6` | + | `/` | Division | `6 / 3` | `2` | + | `%` | Modulo | `7 % 2` | `1` | +--- + +Arithmetic operators take two numeric values (either literals or variables) +and perform a calculation that returns a single numeric value. + +| Operator | Description | | +| :------: | :------------- | :------------------------------------- | +| `+` | Addition | [{{< icon "link" >}}](#addition) | +| `-` | Subtraction | [{{< icon "link" >}}](#subtraction) | +| `*` | Multiplication | [{{< icon "link" >}}](#multiplication) | +| `/` | Division | [{{< icon "link" >}}](#division) | +| `%` | Modulo | [{{< icon "link" >}}](#modulo) | + +## + {#addition .monospace} + +The `+` operator adds two operands together and returns the sum. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 1 + 2 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| int64(1) + int64(2) | +| ------------------: | +| 3 | + +{{% /flex-content %}} +{{< /flex >}} + +## - {#subtraction .monospace} + +The `-` operator subtracts the right operand from the left operand and returns +the difference. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 4 - 2 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| int64(4) - int64(2) | +| ------------------: | +| 2 | + +{{% /flex-content %}} +{{< /flex >}} + +## * {#multiplication .monospace} + +The `*` operator multiplies two operands together and returns the product. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 2 * 3 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| int64(2) * int64(3) | +| ------------------: | +| 6 | + +{{% /flex-content %}} +{{< /flex >}} + +## / {#division .monospace} + +The `/` operator divides the left operand by the right operand and returns the quotient. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 6 / 3 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| int64(6) / int64(3) | +| ------------------: | +| 2 | + +{{% /flex-content %}} +{{< /flex >}} + +## % {#modulo .monospace} + +The `%` (modulo) operator divides the left operand by the right operand and returns the +remainder. If the left operand is not divisible by the right operand, it returns +the left operand. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 8 % 3 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(8) % Int64(3) | +| ------------------: | +| 2 | + +{{% /flex-content %}} +{{< /flex >}} + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 3 % 8 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(3) % Int64(8) | +| ------------------: | +| 3 | + +{{% /flex-content %}} +{{< /flex >}} diff --git a/content/influxdb/cloud-serverless/reference/sql/operators/bitwise.md b/content/influxdb/cloud-serverless/reference/sql/operators/bitwise.md new file mode 100644 index 000000000..af276723e --- /dev/null +++ b/content/influxdb/cloud-serverless/reference/sql/operators/bitwise.md @@ -0,0 +1,152 @@ +--- +title: SQL bitwise operators +list_title: Bitwise operators +description: > + Bitwise operators perform bitwise operations on bit patterns or binary numerals. +menu: + influxdb_cloud_serverless: + name: Bitwise operators + parent: Operators +weight: 304 +list_code_example: | + | Operator | Meaning | Example | Result | + | :------: | :------------------ | :------- | -----: | + | `&` | Bitwise and | `5 & 3` | `1` | + | `\|` | Bitwise or | `5 \| 3` | `7` | + | `^` | Bitwise xor | `5 ^ 3` | `6` | + | `>>` | Bitwise shift right | `5 >> 3` | `0` | + | `<<` | Bitwise shift left | `5 << 3` | `40` | +--- + +Bitwise operators perform bitwise operations on bit patterns or binary numerals. + +| Operator | Meaning | | +| :------: | :------------------ | :------------------------------------------ | +| `&` | Bitwise and | [{{< icon "link" >}}](#bitwise-and) | +| `\|` | Bitwise or | [{{< icon "link" >}}](#bitwise-or) | +| `^` | Bitwise xor | [{{< icon "link" >}}](#bitwise-xor) | +| `>>` | Bitwise shift right | [{{< icon "link" >}}](#bitwise-shift-right) | +| `<<` | Bitwise shift left | [{{< icon "link" >}}](#bitwise-shift-left) | + +## & {#bitwise-and .monospace} + +The `&` (bitwise AND) operator compares each bit of the left operand to the +corresponding bit of the right operand. +If both bits are 1, the corresponding result bit is set to 1. +Otherwise, the corresponding result bit is set to 0. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 5 & 3 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(5) & Int64(3) | +| ------------------: | +| 1 | + +{{% /flex-content %}} +{{< /flex >}} + +## \| {#bitwise-or .monospace} + +The `|` (bitwise OR or inclusive OR) operator compares each bit of the left +operand to the corresponding bit of the right operand. +If either bit is 1, the corresponding result bit is set to 1. +Otherwise, the corresponding result bit is set to 0. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 5 | 3 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(5) \| Int64(3) | +| -------------------: | +| 7 | + +{{% /flex-content %}} +{{< /flex >}} + +## ^ {#bitwise-xor .monospace} + +The `^` (bitwise XOR or exclusive OR) operator compares each bit of the left +operand to the corresponding bit of the right operand. +If the bit in one of the operands is 0 and the bit in the other operand is 1, +the corresponding result bit is set to 1. +Otherwise, the corresponding result bit is set to 0. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 5 ^ 3 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(5) BIT_XOR Int64(3) | +| ------------------------: | +| 6 | + +{{% /flex-content %}} +{{< /flex >}} + +## \>\> {#bitwise-shift-right .monospace} + +The `>>` (bitwise shift right) operator shifts the bits in the left operand to +the right by the number of positions specified in the right operand. +For unsigned numbers, bit positions vacated by the shift operation are filled with 0. +For signed numbers, the sign bit is used to fill the vacated bit positions. +If the number is positive, the bit position is filled with 0. +If the number is negative, the bit position is filled with 1. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 5 >> 3 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(5) \>\> Int64(3) | +| ---------------------: | +| 0 | + +{{% /flex-content %}} +{{< /flex >}} + +## \<\< {#bitwise-shift-left .monospace} + +The `<<` (bitwise shift left) operator shifts the bits in the left operand to +the left by the number of positions specified in the right operand. +Bit positions vacated by the shift operation are filled with 0. +Bits that shift off the end are discarded, including the sign bit. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 5 << 3 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(5) \<\< Int64(3) | +| ---------------------: | +| 40 | + +{{% /flex-content %}} +{{< /flex >}} diff --git a/content/influxdb/cloud-serverless/reference/sql/operators/comparison.md b/content/influxdb/cloud-serverless/reference/sql/operators/comparison.md new file mode 100644 index 000000000..804912c06 --- /dev/null +++ b/content/influxdb/cloud-serverless/reference/sql/operators/comparison.md @@ -0,0 +1,289 @@ +--- +title: SQL comparison operators +list_title: Comparison operators +description: > + Comparison operators evaluate the relationship between the left and right + operands and returns `true` or `false`. +menu: + influxdb_cloud_serverless: + name: Comparison operators + parent: Operators +weight: 302 +list_code_example: | + | Operator | Meaning | Example | + | :------: | :------------------------------------------------------- | :---------------- | + | `=` | Equal to | `123 = 123` | + | `<>` | Not equal to | `123 <> 456` | + | `!=` | Not equal to | `123 != 456` | + | `>` | Greater than | `3 > 2` | + | `>=` | Greater than or equal to | `3 >= 2` | + | `<` | Less than | `1 < 2` | + | `<=` | Less than or equal to | `1 <= 2` | + | `~` | Matches a regular expression | `'abc' ~ 'a.*'` | + | `~*` | Matches a regular expression _(case-insensitive)_ | `'Abc' ~* 'A.*'` | + | `!~` | Does not match a regular expression | `'abc' !~ 'd.*'` | + | `!~*` | Does not match a regular expression _(case-insensitive)_ | `'Abc' !~* 'a.*'` | +--- + +Comparison operators evaluate the relationship between the left and right +operands and returns `true` or `false`. + + +| Operator | Meaning | | +| :------: | :------------------------------------------------------- | :------------------------------------------------------ | +| `=` | Equal to | [{{< icon "link" >}}](#equal-to) | +| `<>` | Not equal to | [{{< icon "link" >}}](#not-equal-to) | +| `!=` | Not equal to | [{{< icon "link" >}}](#not-equal-to) | +| `>` | Greater than | [{{< icon "link" >}}](#greater-than) | +| `>=` | Greater than or equal to | [{{< icon "link" >}}](#greater-than-or-equal) | +| `<` | Less than | [{{< icon "link" >}}](#less-than) | +| `<=` | Less than or equal to | [{{< icon "link" >}}](#less-than-or-equal) | +| `~` | Matches a regular expression | [{{< icon "link" >}}](#regexp-match) | +| `~*` | Matches a regular expression _(case-insensitive)_ | [{{< icon "link" >}}](#regexp-match-case-insensitive) | +| `!~` | Does not match a regular expression | [{{< icon "link" >}}](#regexp-nomatch) | +| `!~*` | Does not match a regular expression _(case-insensitive)_ | [{{< icon "link" >}}](#regexp-nomatch-case-insensitive) | + +## = {#equal-to .monospace} + +The `=` operator compares the left and right operands and, if equal, returns `true`. +Otherwise returns `false`. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 123 = 123 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(123) = Int64(123) | +| :---------------------- | +| true | + +{{% /flex-content %}} +{{< /flex >}} + +## !=, <> {#not-equal-to .monospace} + +The `!=` and `<>` operators compare the left and right operands and, if not equal, +returns `true`. Otherwise returns `false`. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 123 != 456 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(123) != Int64(456) | +| :----------------------- | +| true | + +{{% /flex-content %}} +{{< /flex >}} + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 123 <> 456 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(123) != Int64(456) | +| :----------------------- | +| true | + +{{% /flex-content %}} +{{< /flex >}} + +## > {#greater-than .monospace} + +The `>` operator compares the left and right operands and, if the left operand +is greater than the right operand, returns `true`. +Otherwise returns `false`. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 3 > 2 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(3) > Int64(2) | +| :------------------ | +| true | + +{{% /flex-content %}} +{{< /flex >}} + +## >= {#greater-than-or-equal .monospace} + +The `>=` operator compares the left and right operands and, if the left operand +is greater than or equal to the right operand, returns `true`. +Otherwise returns `false`. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 3 >= 2 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(3) >= Int64(2) | +| :------------------- | +| true | + +{{% /flex-content %}} +{{< /flex >}} + +## < {#less-than .monospace} + +The `<` operator compares the left and right operands and, if the left operand +is less than the right operand, returns `true`. +Otherwise returns `false`. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 1 < 2 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int641(1) < Int64(2) | +| :------------------- | +| true | + +{{% /flex-content %}} +{{< /flex >}} + +## <= {#less-than-or-equal .monospace} + +The `<=` operator compares the left and right operands and, if the left operand +is less than or equal to the right operand, returns `true`. +Otherwise returns `false`. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 1 <= 2 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int641(1) <= Int64(2) | +| :-------------------- | +| true | + +{{% /flex-content %}} +{{< /flex >}} + +## ~ {#regexp-match .monospace} + +The `~` operator compares the left string operand to the right regular expression +operand and, if it matches (case-sensitive), returns `true`. +Otherwise returns `false`. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 'abc' ~ 'a.*' +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Utf8("abc") ~ Utf8("a.*") | +| :------------------------ | +| true | + +{{% /flex-content %}} +{{< /flex >}} + +## ~* {#regexp-match-case-insensitive .monospace} + +The `~*` operator compares the left string operand to the right regular expression +operand and, if it matches (case-insensitive), returns `true`. +Otherwise returns `false`. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 'Abc' ~* 'A.*' +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Utf8("Abc") ~* Utf8("A.*") | +| :------------------------- | +| true | + +{{% /flex-content %}} +{{< /flex >}} + +## !~ {#regexp-nomatch .monospace} + +The `!~` operator compares the left string operand to the right regular expression +operand and, if it does not match (case-sensitive), returns `true`. +Otherwise returns `false`. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 'abc' !~ 'd.*' +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Utf8("abc") !~ Utf8("d.*") | +| :------------------------- | +| true | + +{{% /flex-content %}} +{{< /flex >}} + +## !~* {#regexp-nomatch-case-insensitive .monospace} + +The `!~*` operator compares the left string operand to the right regular expression +operand and, if it does not match (case-insensitive), returns `true`. +Otherwise returns `false`. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 'Abc' !~* 'a.*' +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Utf8("Abc") !~* Utf8("a.*") | +| :-------------------------- | +| false | + +{{% /flex-content %}} +{{< /flex >}} diff --git a/content/influxdb/cloud-serverless/reference/sql/operators/logical.md b/content/influxdb/cloud-serverless/reference/sql/operators/logical.md new file mode 100644 index 000000000..b3e21eb05 --- /dev/null +++ b/content/influxdb/cloud-serverless/reference/sql/operators/logical.md @@ -0,0 +1,463 @@ +--- +title: SQL logical operators +list_title: Logical operators +description: > + Logical operators combine or manipulate conditions in a SQL query. +menu: + influxdb_cloud_serverless: + name: Logical operators + parent: Operators +weight: 303 +related: + - /influxdb/cloud-serverless/reference/sql/where/ + - /influxdb/cloud-serverless/reference/sql/subqueries/#subquery-operators, Subquery operators +list_code_example: | + | Operator | Meaning | + | :-------: | :------------------------------------------------------------------------- | + | `AND` | Returns true if both operands are true. Otherwise, returns false. | + | `BETWEEN` | Returns true if the left operand is within the range of the right operand. | + | `EXISTS` | Returns true if the results of a subquery are not empty. | + | `IN` | Returns true if the left operand is in the right operand list. | + | `LIKE` | Returns true if the left operand matches the right operand pattern string. | + | `NOT` | Negates the subsequent expression. | + | `OR` | Returns true if any operand is true. Otherwise, returns false. | +--- + +Logical operators combine or manipulate conditions in a SQL query. + +| Operator | Meaning | | +| :-------: | :------------------------------------------------------------------------- | :------------------------------ | +| `AND` | Returns true if both operands are true. Otherwise, returns false. | [{{< icon "link" >}}](#and) | +| `BETWEEN` | Returns true if the left operand is within the range of the right operand. | [{{< icon "link" >}}](#between) | +| `EXISTS` | Returns true if the results of a subquery are not empty. | [{{< icon "link" >}}](#exists) | +| `IN` | Returns true if the left operand is in the right operand list. | [{{< icon "link" >}}](#in) | +| `LIKE` | Returns true if the left operand matches the right operand pattern string. | [{{< icon "link" >}}](#like) | +| `NOT` | Negates the subsequent expression. | [{{< icon "link" >}}](#not) | +| `OR` | Returns true if any operand is true. Otherwise, returns false. | [{{< icon "link" >}}](#or) | + +{{% note %}} +#### Sample data + +Query examples on this page use the following sample data sets: + +- [Get started home sensor sample data](/influxdb/cloud-serverless/reference/sample-data/#get-started-home-sensor-data) +- [Home sensor actions sample data](/influxdb/cloud-serverless/reference/sample-data/#home-sensor-actions-data) +{{% /note %}} + +## 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](/influxdb/cloud-serverless/reference/sql/where/) +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 %}} + +| co | hum | room | temp | time | +| --: | ---: | :------ | ---: | :------------------- | +| 18 | 36.9 | Kitchen | 23.3 | 2022-01-01T18:00:00Z | +| 22 | 36.6 | Kitchen | 23.1 | 2022-01-01T19:00:00Z | +| 26 | 36.5 | Kitchen | 22.7 | 2022-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 %}} + +| co | hum | room | temp | time | +| --: | ---: | :---------- | ---: | :------------------- | +| 7 | 36 | Kitchen | 22.4 | 2022-01-01T16:00:00Z | +| 9 | 36 | Kitchen | 22.7 | 2022-01-01T17:00:00Z | +| 5 | 35.9 | Living Room | 22.6 | 2022-01-01T17:00:00Z | +| 9 | 36.2 | Living Room | 22.8 | 2022-01-01T18:00:00Z | + +{{% /influxdb/custom-timestamps %}} +{{% /expand %}} +{{< /expand-wrapper >}} + +## EXISTS {.monospace} + +The `EXISTS` operator returns `true` if result of a +[correlated subquery](/influxdb/cloud-serverless/reference/sql/subqueries/#correlated-subqueries) +is not empty. Otherwise it returns `false`. + +_See [SQL subquery operators](/influxdb/cloud-serverless/reference/sql/subqueries/#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 %}} + +| co | hum | room | temp | time | +| --: | ---: | :---------- | ---: | :------------------- | +| 1 | 36.5 | Kitchen | 22.8 | 2022-01-01T13:00:00Z | +| 1 | 36.3 | Kitchen | 22.8 | 2022-01-01T14:00:00Z | +| 1 | 36.1 | Living Room | 22.3 | 2022-01-01T15:00:00Z | +| 4 | 36 | Living Room | 22.4 | 2022-01-01T16:00:00Z | +| 5 | 35.9 | Living Room | 22.6 | 2022-01-01T17:00:00Z | +| 18 | 36.9 | Kitchen | 23.3 | 2022-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](/influxdb/cloud-serverless/reference/sql/subqueries/#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 %}} + +| co | hum | room | temp | time | +| --: | ---: | :------ | ---: | :------------------- | +| 0 | 35.9 | Kitchen | 21 | 2022-01-01T08:00:00Z | +| 0 | 36.2 | Kitchen | 23 | 2022-01-01T09:00:00Z | +| 0 | 36.1 | Kitchen | 22.7 | 2022-01-01T10:00:00Z | +| 0 | 36 | Kitchen | 22.4 | 2022-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 %}} + +| co | hum | room | temp | time | +| --: | ---: | :---------- | ---: | :------------------- | +| 0 | 35.9 | Living Room | 21.1 | 2022-01-01T08:00:00Z | +| 0 | 35.9 | Kitchen | 21 | 2022-01-01T08:00:00Z | +| 0 | 35.9 | Living Room | 21.4 | 2022-01-01T09:00:00Z | +| 0 | 36.2 | Kitchen | 23 | 2022-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](#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 %}} + +| co | hum | room | temp | time | +| --: | ---: | :---------- | ---: | :------------------- | +| 0 | 35.9 | Living Room | 21.1 | 2022-01-01T08:00:00Z | +| 0 | 35.9 | Living Room | 21.4 | 2022-01-01T09:00:00Z | +| 0 | 36 | Living Room | 21.8 | 2022-01-01T10:00:00Z | +| 0 | 36 | Living Room | 22.2 | 2022-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. + +| Character | Description | +| :-------: | :--------------------------------- | +| `%` | 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 %}} + +| co | hum | room | temp | time | +| --: | ---: | :---------- | ---: | :------------------- | +| 0 | 35.9 | Living Room | 21.1 | 2022-01-01T08:00:00Z | +| 0 | 35.9 | Living Room | 21.4 | 2022-01-01T09:00:00Z | +| 0 | 36 | Living Room | 21.8 | 2022-01-01T10:00:00Z | +| 0 | 36 | Living Room | 22.2 | 2022-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 %}} + +| co | hum | room | temp | time | +| --: | ---: | :---------- | ---: | :------------------- | +| 7 | 36 | Kitchen | 22.4 | 2022-01-01T16:00:00Z | +| 4 | 36 | Living Room | 22.4 | 2022-01-01T16:00:00Z | +| 9 | 36 | Kitchen | 22.7 | 2022-01-01T17:00:00Z | +| 9 | 36.2 | Living Room | 22.8 | 2022-01-01T18:00:00Z | +| 17 | 36.4 | Living Room | 22.2 | 2022-01-01T20:00:00Z | +| 26 | 36.5 | Kitchen | 22.7 | 2022-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 %}} + +| co | hum | room | temp | time | +| --: | ---: | :------ | ---: | :------------------- | +| 0 | 35.9 | Kitchen | 21 | 2022-01-01T08:00:00Z | +| 0 | 36.2 | Kitchen | 23 | 2022-01-01T09:00:00Z | +| 0 | 36.1 | Kitchen | 22.7 | 2022-01-01T10:00:00Z | +| 0 | 36 | Kitchen | 22.4 | 2022-01-01T11:00:00Z | +| 0 | 36 | Kitchen | 22.5 | 2022-01-01T12:00:00Z | +| 26 | 36.5 | Kitchen | 22.7 | 2022-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](/influxdb/cloud-serverless/reference/sql/where/) +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 %}} + +| co | hum | room | temp | time | +| --: | ---: | :------ | ---: | :------------------- | +| 18 | 36.9 | Kitchen | 23.3 | 2022-01-01T18:00:00Z | +| 22 | 36.6 | Kitchen | 23.1 | 2022-01-01T19:00:00Z | +| 26 | 36.5 | Kitchen | 22.7 | 2022-01-01T20:00:00Z | + +{{% /influxdb/custom-timestamps %}} +{{% /expand %}} +{{< /expand-wrapper >}} diff --git a/content/influxdb/cloud-serverless/reference/sql/operators/other.md b/content/influxdb/cloud-serverless/reference/sql/operators/other.md new file mode 100644 index 000000000..bc2bb3725 --- /dev/null +++ b/content/influxdb/cloud-serverless/reference/sql/operators/other.md @@ -0,0 +1,42 @@ +--- +title: Other SQL operators +list_title: Other operators +description: > + SQL supports other miscellaneous operators that perform various operations. +menu: + influxdb_cloud_serverless: + name: Other operators + parent: Operators +weight: 305 +list_code_example: | + | Operator | Meaning | Example | Result | + | :------: | :------------------- | :---------------------- | :------------ | + | `\|\|` | Concatenate strings | `'Hello' \|\| ' world'` | `Hello world` | +--- + +SQL supports miscellaneous operators that perform various operations. + +| Operator | Meaning | | +| :------: | :------------------ | :------------------------------------------ | +| `\|\|` | Concatenate strings | [{{< icon "link" >}}](#concatenate-strings) | + +## || {#concatenate-strings} + +The `||` operator concatenates two string operands into a single string. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 'Hello' || ' world' AS "Concatenated" +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Concatenated | +| :----------- | +| Hello world | + +{{% /flex-content %}} +{{< /flex >}} diff --git a/content/influxdb/cloud-serverless/reference/sql/subqueries.md b/content/influxdb/cloud-serverless/reference/sql/subqueries.md index ca3c1249b..0bc2bbbf1 100644 --- a/content/influxdb/cloud-serverless/reference/sql/subqueries.md +++ b/content/influxdb/cloud-serverless/reference/sql/subqueries.md @@ -51,7 +51,7 @@ Query examples on this page use the following sample data sets: ### [ NOT ] EXISTS The `EXISTS` operator returns all rows where a -_[correlated subquery](#correlated-subquery)_ produces one or more matches for +_[correlated subquery](#correlated-subqueries)_ produces one or more matches for that row. `NOT EXISTS` returns all rows where a _correlated subquery_ produces zero matches for that row. Only _correlated subqueries_ are supported. diff --git a/content/influxdb/clustered/reference/sql/_index.md b/content/influxdb/clustered/reference/sql/_index.md index 42d242455..e9cf9a917 100644 --- a/content/influxdb/clustered/reference/sql/_index.md +++ b/content/influxdb/clustered/reference/sql/_index.md @@ -171,11 +171,15 @@ Comparison operators evaluate the relationship between the left and right operan ### Logical operators -| Operator | Meaning | -| :------: | :---------------------------------------------------------------- | -| `AND` | Returns true if both operands are true. Otherwise, returns false. | -| `OR` | Returns true if any operand is true. Otherwise, returns false. | -| `NOT` | Negates the subsequent expression | +| Operator | Meaning | +| :-------: | :------------------------------------------------------------------------- | +| `AND` | Returns true if both operands are true. Otherwise, returns false. | +| `BETWEEN` | Returns true if the left operand is within the range of the right operand. | +| `EXISTS` | Returns true if the operand is not null. | +| `IN` | Returns true if the left operand is in the right operand list. | +| `LIKE` | Returns true if the left operand matches the right operand pattern string. | +| `NOT` | Negates the subsequent expression. | +| `OR` | Returns true if any operand is true. Otherwise, returns false. | ### Bitwise operators @@ -185,7 +189,6 @@ Bitwise operators perform bitwise operations on bit patterns or binary numerals. | :------: | :------------------ | :------- | -----: | | `&` | Bitwise and | `5 & 3` | `1` | | `\|` | Bitwise or | `5 \| 3` | `7` | -| `#` | Bitwise xor | `5 # 3` | `6` | | `^` | Bitwise xor | `5 ^ 3` | `6` | | `>>` | Bitwise shift right | `5 >> 3` | `0` | | `<<` | Bitwise shift left | `5 << 3` | `40` | @@ -220,6 +223,7 @@ IN INNER JOIN JOIN LEFT JOIN +LIKE LIMIT NOT EXISTS diff --git a/content/influxdb/clustered/reference/sql/operators/_index.md b/content/influxdb/clustered/reference/sql/operators/_index.md new file mode 100644 index 000000000..e383f2ecc --- /dev/null +++ b/content/influxdb/clustered/reference/sql/operators/_index.md @@ -0,0 +1,18 @@ +--- +title: SQL operators +description: > + SQL operators are reserved words or characters which perform certain operations, + including comparisons and arithmetic. +menu: + influxdb_clustered: + name: Operators + parent: SQL reference +weight: 211 +--- + +SQL operators are reserved words or characters which perform certain operations, +including comparisons and arithmetic. + +{{< children type="anchored-list" >}} + +{{< children hlevel="h2" >}} diff --git a/content/influxdb/clustered/reference/sql/operators/arithmetic.md b/content/influxdb/clustered/reference/sql/operators/arithmetic.md new file mode 100644 index 000000000..976da50c0 --- /dev/null +++ b/content/influxdb/clustered/reference/sql/operators/arithmetic.md @@ -0,0 +1,156 @@ +--- +title: SQL arithmetic operators +list_title: Arithmetic operators +description: > + Arithmetic operators take two numeric values (either literals or variables) + and perform a calculation that returns a single numeric value. +menu: + influxdb_clustered: + name: Arithmetic operators + parent: Operators +weight: 301 +list_code_example: | + | Operator | Description | Example | Result | + | :------: | :------------- | ------- | -----: | + | `+` | Addition | `2 + 2` | `4` | + | `-` | Subtraction | `4 - 2` | `2` | + | `*` | Multiplication | `2 * 3` | `6` | + | `/` | Division | `6 / 3` | `2` | + | `%` | Modulo | `7 % 2` | `1` | +--- + +Arithmetic operators take two numeric values (either literals or variables) +and perform a calculation that returns a single numeric value. + +| Operator | Description | | +| :------: | :------------- | :------------------------------------- | +| `+` | Addition | [{{< icon "link" >}}](#addition) | +| `-` | Subtraction | [{{< icon "link" >}}](#subtraction) | +| `*` | Multiplication | [{{< icon "link" >}}](#multiplication) | +| `/` | Division | [{{< icon "link" >}}](#division) | +| `%` | Modulo | [{{< icon "link" >}}](#modulo) | + +## + {#addition .monospace} + +The `+` operator adds two operands together and returns the sum. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 1 + 2 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| int64(1) + int64(2) | +| ------------------: | +| 3 | + +{{% /flex-content %}} +{{< /flex >}} + +## - {#subtraction .monospace} + +The `-` operator subtracts the right operand from the left operand and returns +the difference. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 4 - 2 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| int64(4) - int64(2) | +| ------------------: | +| 2 | + +{{% /flex-content %}} +{{< /flex >}} + +## * {#multiplication .monospace} + +The `*` operator multiplies two operands together and returns the product. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 2 * 3 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| int64(2) * int64(3) | +| ------------------: | +| 6 | + +{{% /flex-content %}} +{{< /flex >}} + +## / {#division .monospace} + +The `/` operator divides the left operand by the right operand and returns the quotient. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 6 / 3 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| int64(6) / int64(3) | +| ------------------: | +| 2 | + +{{% /flex-content %}} +{{< /flex >}} + +## % {#modulo .monospace} + +The `%` (modulo) operator divides the left operand by the right operand and returns the +remainder. If the left operand is not divisible by the right operand, it returns +the left operand. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 8 % 3 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(8) % Int64(3) | +| ------------------: | +| 2 | + +{{% /flex-content %}} +{{< /flex >}} + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 3 % 8 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(3) % Int64(8) | +| ------------------: | +| 3 | + +{{% /flex-content %}} +{{< /flex >}} diff --git a/content/influxdb/clustered/reference/sql/operators/bitwise.md b/content/influxdb/clustered/reference/sql/operators/bitwise.md new file mode 100644 index 000000000..bf63972c9 --- /dev/null +++ b/content/influxdb/clustered/reference/sql/operators/bitwise.md @@ -0,0 +1,152 @@ +--- +title: SQL bitwise operators +list_title: Bitwise operators +description: > + Bitwise operators perform bitwise operations on bit patterns or binary numerals. +menu: + influxdb_clustered: + name: Bitwise operators + parent: Operators +weight: 304 +list_code_example: | + | Operator | Meaning | Example | Result | + | :------: | :------------------ | :------- | -----: | + | `&` | Bitwise and | `5 & 3` | `1` | + | `\|` | Bitwise or | `5 \| 3` | `7` | + | `^` | Bitwise xor | `5 ^ 3` | `6` | + | `>>` | Bitwise shift right | `5 >> 3` | `0` | + | `<<` | Bitwise shift left | `5 << 3` | `40` | +--- + +Bitwise operators perform bitwise operations on bit patterns or binary numerals. + +| Operator | Meaning | | +| :------: | :------------------ | :------------------------------------------ | +| `&` | Bitwise and | [{{< icon "link" >}}](#bitwise-and) | +| `\|` | Bitwise or | [{{< icon "link" >}}](#bitwise-or) | +| `^` | Bitwise xor | [{{< icon "link" >}}](#bitwise-xor) | +| `>>` | Bitwise shift right | [{{< icon "link" >}}](#bitwise-shift-right) | +| `<<` | Bitwise shift left | [{{< icon "link" >}}](#bitwise-shift-left) | + +## & {#bitwise-and .monospace} + +The `&` (bitwise AND) operator compares each bit of the left operand to the +corresponding bit of the right operand. +If both bits are 1, the corresponding result bit is set to 1. +Otherwise, the corresponding result bit is set to 0. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 5 & 3 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(5) & Int64(3) | +| ------------------: | +| 1 | + +{{% /flex-content %}} +{{< /flex >}} + +## \| {#bitwise-or .monospace} + +The `|` (bitwise OR or inclusive OR) operator compares each bit of the left +operand to the corresponding bit of the right operand. +If either bit is 1, the corresponding result bit is set to 1. +Otherwise, the corresponding result bit is set to 0. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 5 | 3 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(5) \| Int64(3) | +| -------------------: | +| 7 | + +{{% /flex-content %}} +{{< /flex >}} + +## ^ {#bitwise-xor .monospace} + +The `^` (bitwise XOR or exclusive OR) operator compares each bit of the left +operand to the corresponding bit of the right operand. +If the bit in one of the operands is 0 and the bit in the other operand is 1, +the corresponding result bit is set to 1. +Otherwise, the corresponding result bit is set to 0. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 5 ^ 3 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(5) BIT_XOR Int64(3) | +| ------------------------: | +| 6 | + +{{% /flex-content %}} +{{< /flex >}} + +## \>\> {#bitwise-shift-right .monospace} + +The `>>` (bitwise shift right) operator shifts the bits in the left operand to +the right by the number of positions specified in the right operand. +For unsigned numbers, bit positions vacated by the shift operation are filled with 0. +For signed numbers, the sign bit is used to fill the vacated bit positions. +If the number is positive, the bit position is filled with 0. +If the number is negative, the bit position is filled with 1. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 5 >> 3 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(5) \>\> Int64(3) | +| ---------------------: | +| 0 | + +{{% /flex-content %}} +{{< /flex >}} + +## \<\< {#bitwise-shift-left .monospace} + +The `<<` (bitwise shift left) operator shifts the bits in the left operand to +the left by the number of positions specified in the right operand. +Bit positions vacated by the shift operation are filled with 0. +Bits that shift off the end are discarded, including the sign bit. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 5 << 3 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(5) \<\< Int64(3) | +| ---------------------: | +| 40 | + +{{% /flex-content %}} +{{< /flex >}} diff --git a/content/influxdb/clustered/reference/sql/operators/comparison.md b/content/influxdb/clustered/reference/sql/operators/comparison.md new file mode 100644 index 000000000..540ab6bfc --- /dev/null +++ b/content/influxdb/clustered/reference/sql/operators/comparison.md @@ -0,0 +1,289 @@ +--- +title: SQL comparison operators +list_title: Comparison operators +description: > + Comparison operators evaluate the relationship between the left and right + operands and returns `true` or `false`. +menu: + influxdb_clustered: + name: Comparison operators + parent: Operators +weight: 302 +list_code_example: | + | Operator | Meaning | Example | + | :------: | :------------------------------------------------------- | :---------------- | + | `=` | Equal to | `123 = 123` | + | `<>` | Not equal to | `123 <> 456` | + | `!=` | Not equal to | `123 != 456` | + | `>` | Greater than | `3 > 2` | + | `>=` | Greater than or equal to | `3 >= 2` | + | `<` | Less than | `1 < 2` | + | `<=` | Less than or equal to | `1 <= 2` | + | `~` | Matches a regular expression | `'abc' ~ 'a.*'` | + | `~*` | Matches a regular expression _(case-insensitive)_ | `'Abc' ~* 'A.*'` | + | `!~` | Does not match a regular expression | `'abc' !~ 'd.*'` | + | `!~*` | Does not match a regular expression _(case-insensitive)_ | `'Abc' !~* 'a.*'` | +--- + +Comparison operators evaluate the relationship between the left and right +operands and returns `true` or `false`. + + +| Operator | Meaning | | +| :------: | :------------------------------------------------------- | :------------------------------------------------------ | +| `=` | Equal to | [{{< icon "link" >}}](#equal-to) | +| `<>` | Not equal to | [{{< icon "link" >}}](#not-equal-to) | +| `!=` | Not equal to | [{{< icon "link" >}}](#not-equal-to) | +| `>` | Greater than | [{{< icon "link" >}}](#greater-than) | +| `>=` | Greater than or equal to | [{{< icon "link" >}}](#greater-than-or-equal) | +| `<` | Less than | [{{< icon "link" >}}](#less-than) | +| `<=` | Less than or equal to | [{{< icon "link" >}}](#less-than-or-equal) | +| `~` | Matches a regular expression | [{{< icon "link" >}}](#regexp-match) | +| `~*` | Matches a regular expression _(case-insensitive)_ | [{{< icon "link" >}}](#regexp-match-case-insensitive) | +| `!~` | Does not match a regular expression | [{{< icon "link" >}}](#regexp-nomatch) | +| `!~*` | Does not match a regular expression _(case-insensitive)_ | [{{< icon "link" >}}](#regexp-nomatch-case-insensitive) | + +## = {#equal-to .monospace} + +The `=` operator compares the left and right operands and, if equal, returns `true`. +Otherwise returns `false`. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 123 = 123 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(123) = Int64(123) | +| :---------------------- | +| true | + +{{% /flex-content %}} +{{< /flex >}} + +## !=, <> {#not-equal-to .monospace} + +The `!=` and `<>` operators compare the left and right operands and, if not equal, +returns `true`. Otherwise returns `false`. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 123 != 456 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(123) != Int64(456) | +| :----------------------- | +| true | + +{{% /flex-content %}} +{{< /flex >}} + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 123 <> 456 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(123) != Int64(456) | +| :----------------------- | +| true | + +{{% /flex-content %}} +{{< /flex >}} + +## > {#greater-than .monospace} + +The `>` operator compares the left and right operands and, if the left operand +is greater than the right operand, returns `true`. +Otherwise returns `false`. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 3 > 2 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(3) > Int64(2) | +| :------------------ | +| true | + +{{% /flex-content %}} +{{< /flex >}} + +## >= {#greater-than-or-equal .monospace} + +The `>=` operator compares the left and right operands and, if the left operand +is greater than or equal to the right operand, returns `true`. +Otherwise returns `false`. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 3 >= 2 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int64(3) >= Int64(2) | +| :------------------- | +| true | + +{{% /flex-content %}} +{{< /flex >}} + +## < {#less-than .monospace} + +The `<` operator compares the left and right operands and, if the left operand +is less than the right operand, returns `true`. +Otherwise returns `false`. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 1 < 2 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int641(1) < Int64(2) | +| :------------------- | +| true | + +{{% /flex-content %}} +{{< /flex >}} + +## <= {#less-than-or-equal .monospace} + +The `<=` operator compares the left and right operands and, if the left operand +is less than or equal to the right operand, returns `true`. +Otherwise returns `false`. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 1 <= 2 +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Int641(1) <= Int64(2) | +| :-------------------- | +| true | + +{{% /flex-content %}} +{{< /flex >}} + +## ~ {#regexp-match .monospace} + +The `~` operator compares the left string operand to the right regular expression +operand and, if it matches (case-sensitive), returns `true`. +Otherwise returns `false`. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 'abc' ~ 'a.*' +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Utf8("abc") ~ Utf8("a.*") | +| :------------------------ | +| true | + +{{% /flex-content %}} +{{< /flex >}} + +## ~* {#regexp-match-case-insensitive .monospace} + +The `~*` operator compares the left string operand to the right regular expression +operand and, if it matches (case-insensitive), returns `true`. +Otherwise returns `false`. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 'Abc' ~* 'A.*' +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Utf8("Abc") ~* Utf8("A.*") | +| :------------------------- | +| true | + +{{% /flex-content %}} +{{< /flex >}} + +## !~ {#regexp-nomatch .monospace} + +The `!~` operator compares the left string operand to the right regular expression +operand and, if it does not match (case-sensitive), returns `true`. +Otherwise returns `false`. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 'abc' !~ 'd.*' +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Utf8("abc") !~ Utf8("d.*") | +| :------------------------- | +| true | + +{{% /flex-content %}} +{{< /flex >}} + +## !~* {#regexp-nomatch-case-insensitive .monospace} + +The `!~*` operator compares the left string operand to the right regular expression +operand and, if it does not match (case-insensitive), returns `true`. +Otherwise returns `false`. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 'Abc' !~* 'a.*' +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Utf8("Abc") !~* Utf8("a.*") | +| :-------------------------- | +| false | + +{{% /flex-content %}} +{{< /flex >}} diff --git a/content/influxdb/clustered/reference/sql/operators/logical.md b/content/influxdb/clustered/reference/sql/operators/logical.md new file mode 100644 index 000000000..3ffeb1584 --- /dev/null +++ b/content/influxdb/clustered/reference/sql/operators/logical.md @@ -0,0 +1,463 @@ +--- +title: SQL logical operators +list_title: Logical operators +description: > + Logical operators combine or manipulate conditions in a SQL query. +menu: + influxdb_clustered: + name: Logical operators + parent: Operators +weight: 303 +related: + - /influxdb/clustered/reference/sql/where/ + - /influxdb/clustered/reference/sql/subqueries/#subquery-operators, Subquery operators +list_code_example: | + | Operator | Meaning | + | :-------: | :------------------------------------------------------------------------- | + | `AND` | Returns true if both operands are true. Otherwise, returns false. | + | `BETWEEN` | Returns true if the left operand is within the range of the right operand. | + | `EXISTS` | Returns true if the results of a subquery are not empty. | + | `IN` | Returns true if the left operand is in the right operand list. | + | `LIKE` | Returns true if the left operand matches the right operand pattern string. | + | `NOT` | Negates the subsequent expression. | + | `OR` | Returns true if any operand is true. Otherwise, returns false. | +--- + +Logical operators combine or manipulate conditions in a SQL query. + +| Operator | Meaning | | +| :-------: | :------------------------------------------------------------------------- | :------------------------------ | +| `AND` | Returns true if both operands are true. Otherwise, returns false. | [{{< icon "link" >}}](#and) | +| `BETWEEN` | Returns true if the left operand is within the range of the right operand. | [{{< icon "link" >}}](#between) | +| `EXISTS` | Returns true if the results of a subquery are not empty. | [{{< icon "link" >}}](#exists) | +| `IN` | Returns true if the left operand is in the right operand list. | [{{< icon "link" >}}](#in) | +| `LIKE` | Returns true if the left operand matches the right operand pattern string. | [{{< icon "link" >}}](#like) | +| `NOT` | Negates the subsequent expression. | [{{< icon "link" >}}](#not) | +| `OR` | Returns true if any operand is true. Otherwise, returns false. | [{{< icon "link" >}}](#or) | + +{{% note %}} +#### Sample data + +Query examples on this page use the following sample data sets: + +- [Get started home sensor sample data](/influxdb/clustered/reference/sample-data/#get-started-home-sensor-data) +- [Home sensor actions sample data](/influxdb/clustered/reference/sample-data/#home-sensor-actions-data) +{{% /note %}} + +## 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](/influxdb/clustered/reference/sql/where/) +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 %}} + +| co | hum | room | temp | time | +| --: | ---: | :------ | ---: | :------------------- | +| 18 | 36.9 | Kitchen | 23.3 | 2022-01-01T18:00:00Z | +| 22 | 36.6 | Kitchen | 23.1 | 2022-01-01T19:00:00Z | +| 26 | 36.5 | Kitchen | 22.7 | 2022-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 %}} + +| co | hum | room | temp | time | +| --: | ---: | :---------- | ---: | :------------------- | +| 7 | 36 | Kitchen | 22.4 | 2022-01-01T16:00:00Z | +| 9 | 36 | Kitchen | 22.7 | 2022-01-01T17:00:00Z | +| 5 | 35.9 | Living Room | 22.6 | 2022-01-01T17:00:00Z | +| 9 | 36.2 | Living Room | 22.8 | 2022-01-01T18:00:00Z | + +{{% /influxdb/custom-timestamps %}} +{{% /expand %}} +{{< /expand-wrapper >}} + +## EXISTS {.monospace} + +The `EXISTS` operator returns `true` if result of a +[correlated subquery](/influxdb/clustered/reference/sql/subqueries/#correlated-subqueries) +is not empty. Otherwise it returns `false`. + +_See [SQL subquery operators](/influxdb/clustered/reference/sql/subqueries/#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 %}} + +| co | hum | room | temp | time | +| --: | ---: | :---------- | ---: | :------------------- | +| 1 | 36.5 | Kitchen | 22.8 | 2022-01-01T13:00:00Z | +| 1 | 36.3 | Kitchen | 22.8 | 2022-01-01T14:00:00Z | +| 1 | 36.1 | Living Room | 22.3 | 2022-01-01T15:00:00Z | +| 4 | 36 | Living Room | 22.4 | 2022-01-01T16:00:00Z | +| 5 | 35.9 | Living Room | 22.6 | 2022-01-01T17:00:00Z | +| 18 | 36.9 | Kitchen | 23.3 | 2022-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](/influxdb/clustered/reference/sql/subqueries/#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 %}} + +| co | hum | room | temp | time | +| --: | ---: | :------ | ---: | :------------------- | +| 0 | 35.9 | Kitchen | 21 | 2022-01-01T08:00:00Z | +| 0 | 36.2 | Kitchen | 23 | 2022-01-01T09:00:00Z | +| 0 | 36.1 | Kitchen | 22.7 | 2022-01-01T10:00:00Z | +| 0 | 36 | Kitchen | 22.4 | 2022-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 %}} + +| co | hum | room | temp | time | +| --: | ---: | :---------- | ---: | :------------------- | +| 0 | 35.9 | Living Room | 21.1 | 2022-01-01T08:00:00Z | +| 0 | 35.9 | Kitchen | 21 | 2022-01-01T08:00:00Z | +| 0 | 35.9 | Living Room | 21.4 | 2022-01-01T09:00:00Z | +| 0 | 36.2 | Kitchen | 23 | 2022-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](#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 %}} + +| co | hum | room | temp | time | +| --: | ---: | :---------- | ---: | :------------------- | +| 0 | 35.9 | Living Room | 21.1 | 2022-01-01T08:00:00Z | +| 0 | 35.9 | Living Room | 21.4 | 2022-01-01T09:00:00Z | +| 0 | 36 | Living Room | 21.8 | 2022-01-01T10:00:00Z | +| 0 | 36 | Living Room | 22.2 | 2022-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. + +| Character | Description | +| :-------: | :--------------------------------- | +| `%` | 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 %}} + +| co | hum | room | temp | time | +| --: | ---: | :---------- | ---: | :------------------- | +| 0 | 35.9 | Living Room | 21.1 | 2022-01-01T08:00:00Z | +| 0 | 35.9 | Living Room | 21.4 | 2022-01-01T09:00:00Z | +| 0 | 36 | Living Room | 21.8 | 2022-01-01T10:00:00Z | +| 0 | 36 | Living Room | 22.2 | 2022-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 %}} + +| co | hum | room | temp | time | +| --: | ---: | :---------- | ---: | :------------------- | +| 7 | 36 | Kitchen | 22.4 | 2022-01-01T16:00:00Z | +| 4 | 36 | Living Room | 22.4 | 2022-01-01T16:00:00Z | +| 9 | 36 | Kitchen | 22.7 | 2022-01-01T17:00:00Z | +| 9 | 36.2 | Living Room | 22.8 | 2022-01-01T18:00:00Z | +| 17 | 36.4 | Living Room | 22.2 | 2022-01-01T20:00:00Z | +| 26 | 36.5 | Kitchen | 22.7 | 2022-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 %}} + +| co | hum | room | temp | time | +| --: | ---: | :------ | ---: | :------------------- | +| 0 | 35.9 | Kitchen | 21 | 2022-01-01T08:00:00Z | +| 0 | 36.2 | Kitchen | 23 | 2022-01-01T09:00:00Z | +| 0 | 36.1 | Kitchen | 22.7 | 2022-01-01T10:00:00Z | +| 0 | 36 | Kitchen | 22.4 | 2022-01-01T11:00:00Z | +| 0 | 36 | Kitchen | 22.5 | 2022-01-01T12:00:00Z | +| 26 | 36.5 | Kitchen | 22.7 | 2022-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](/influxdb/clustered/reference/sql/where/) +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 %}} + +| co | hum | room | temp | time | +| --: | ---: | :------ | ---: | :------------------- | +| 18 | 36.9 | Kitchen | 23.3 | 2022-01-01T18:00:00Z | +| 22 | 36.6 | Kitchen | 23.1 | 2022-01-01T19:00:00Z | +| 26 | 36.5 | Kitchen | 22.7 | 2022-01-01T20:00:00Z | + +{{% /influxdb/custom-timestamps %}} +{{% /expand %}} +{{< /expand-wrapper >}} diff --git a/content/influxdb/clustered/reference/sql/operators/other.md b/content/influxdb/clustered/reference/sql/operators/other.md new file mode 100644 index 000000000..deccebe0e --- /dev/null +++ b/content/influxdb/clustered/reference/sql/operators/other.md @@ -0,0 +1,42 @@ +--- +title: Other SQL operators +list_title: Other operators +description: > + SQL supports other miscellaneous operators that perform various operations. +menu: + influxdb_clustered: + name: Other operators + parent: Operators +weight: 305 +list_code_example: | + | Operator | Meaning | Example | Result | + | :------: | :------------------- | :---------------------- | :------------ | + | `\|\|` | Concatenate strings | `'Hello' \|\| ' world'` | `Hello world` | +--- + +SQL supports miscellaneous operators that perform various operations. + +| Operator | Meaning | | +| :------: | :------------------ | :------------------------------------------ | +| `\|\|` | Concatenate strings | [{{< icon "link" >}}](#concatenate-strings) | + +## || {#concatenate-strings} + +The `||` operator concatenates two string operands into a single string. + +{{< flex >}} +{{% flex-content "two-thirds operator-example" %}} + +```sql +SELECT 'Hello' || ' world' AS "Concatenated" +``` + +{{% /flex-content %}} +{{% flex-content "third operator-example" %}} + +| Concatenated | +| :----------- | +| Hello world | + +{{% /flex-content %}} +{{< /flex >}} diff --git a/layouts/shortcodes/flex-content.html b/layouts/shortcodes/flex-content.html index 9af32f6aa..3f9fff323 100644 --- a/layouts/shortcodes/flex-content.html +++ b/layouts/shortcodes/flex-content.html @@ -1,5 +1,5 @@ -{{ $width := .Get 0 | default "half" }} {{ $_hugo_config := `{ "version": 1 }` }} -
+{{ $class := .Get 0 | default "" }} +
{{ .Inner }}
diff --git a/layouts/shortcodes/icon.html b/layouts/shortcodes/icon.html index 8f7a81f77..d9cadbc06 100644 --- a/layouts/shortcodes/icon.html +++ b/layouts/shortcodes/icon.html @@ -255,5 +255,7 @@ {{- else if or (eq $icon "flask") (eq $icon "beaker") (eq $icon "experimental") -}} + {{- else if or (eq $icon "link") (eq $icon "chain") -}} + {{- end -}} {{- end -}}