|
| 1 | +<!-- |
| 2 | +
|
| 3 | + Licensed to the Apache Software Foundation (ASF) under one |
| 4 | + or more contributor license agreements. See the NOTICE file |
| 5 | + distributed with this work for additional information |
| 6 | + regarding copyright ownership. The ASF licenses this file |
| 7 | + to you under the Apache License, Version 2.0 (the |
| 8 | + "License"); you may not use this file except in compliance |
| 9 | + with the License. You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | + Unless required by applicable law or agreed to in writing, |
| 14 | + software distributed under the License is distributed on an |
| 15 | + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | + KIND, either express or implied. See the License for the |
| 17 | + specific language governing permissions and limitations |
| 18 | + under the License. |
| 19 | +
|
| 20 | +--> |
| 21 | + |
| 22 | +# Tableless Query |
| 23 | + |
| 24 | +## 1. Overview |
| 25 | + |
| 26 | +Tableless query allows you to execute SQL statements without a `FROM` clause, without relying on any actual database table. It retrieves computed results, constant values, or system information, and is mainly used in scenarios such as expression evaluation, system function calls, and constant queries. |
| 27 | + |
| 28 | +> Note: This feature is supported since V2.0.11. |
| 29 | +
|
| 30 | +## 2. Syntax |
| 31 | + |
| 32 | +```sql |
| 33 | +SELECT expression [ [ AS ] column_alias ] |
| 34 | + [, expression [ [ AS ] column_alias ] ]*; |
| 35 | +``` |
| 36 | + |
| 37 | +Where: |
| 38 | + |
| 39 | +- `expression`: the expression to be evaluated. It can be a constant, a mathematical function, a string function, a date-time function, or an aggregate function. Currently, only functions that take **no arguments or constant arguments only** are supported. |
| 40 | + |
| 41 | +- `column_alias`: an optional alias for the result column, which improves the readability of query results. If no alias is specified, the system automatically generates column names such as `_col0`, `_col1`, etc. |
| 42 | + |
| 43 | +- `SELECT *` is not supported. |
| 44 | + |
| 45 | +- When executing `SELECT COUNT(*);`, the result is always `1`. |
| 46 | + |
| 47 | +## 3. Examples |
| 48 | + |
| 49 | +### 3.1 Constants and Mathematical Calculations |
| 50 | + |
| 51 | +Query statement: |
| 52 | + |
| 53 | +```sql |
| 54 | +SELECT 100 + 50 AS sum_result, |
| 55 | + 10 * 5 AS product, |
| 56 | + SQRT(144) AS square_root, |
| 57 | + ABS(-25) AS absolute_value, |
| 58 | + SIN(1) AS sin_value; |
| 59 | +``` |
| 60 | + |
| 61 | +Result: |
| 62 | + |
| 63 | +```text |
| 64 | ++----------+-------+-----------+--------------+------------------+ |
| 65 | +|sum_result|product|square_root|absolute_value| sin_value| |
| 66 | ++----------+-------+-----------+--------------+------------------+ |
| 67 | +| 150| 50| 12.0| 25|0.8414709848078965| |
| 68 | ++----------+-------+-----------+--------------+------------------+ |
| 69 | +``` |
| 70 | + |
| 71 | +### 3.2 String Functions |
| 72 | + |
| 73 | +Query statement: |
| 74 | + |
| 75 | +```sql |
| 76 | +SELECT |
| 77 | + CONCAT('Hello', ' ', 'World') AS greeting, |
| 78 | + UPPER('iotdb') AS uppercase, |
| 79 | + LOWER('IOTDB') AS lowercase, |
| 80 | + LENGTH('database') AS str_length; |
| 81 | +``` |
| 82 | + |
| 83 | +Result: |
| 84 | + |
| 85 | +```text |
| 86 | ++-----------+---------+---------+----------+ |
| 87 | +| greeting|uppercase|lowercase|str_length| |
| 88 | ++-----------+---------+---------+----------+ |
| 89 | +|Hello World| IOTDB| iotdb| 8| |
| 90 | ++-----------+---------+---------+----------+ |
| 91 | +``` |
| 92 | + |
| 93 | +### 3.3 Date and Time Functions |
| 94 | + |
| 95 | +Query statement: |
| 96 | + |
| 97 | +```sql |
| 98 | +SELECT NOW() AS now_time; |
| 99 | +``` |
| 100 | + |
| 101 | +Result: |
| 102 | + |
| 103 | +```text |
| 104 | ++-------------+ |
| 105 | +| now_time| |
| 106 | ++-------------+ |
| 107 | +|1784776724555| |
| 108 | ++-------------+ |
| 109 | +``` |
| 110 | + |
| 111 | +### 3.4 Aggregate Functions |
| 112 | + |
| 113 | +Query statement: |
| 114 | + |
| 115 | +```sql |
| 116 | +SELECT |
| 117 | + AVG(10), |
| 118 | + SUM(10), |
| 119 | + COUNT(10), |
| 120 | + COUNT(*); |
| 121 | +``` |
| 122 | + |
| 123 | +Result: |
| 124 | + |
| 125 | +```text |
| 126 | ++-----+-----+-----+-----+ |
| 127 | +|_col0|_col1|_col2|_col3| |
| 128 | ++-----+-----+-----+-----+ |
| 129 | +| 10.0| 10.0| 1| 1| |
| 130 | ++-----+-----+-----+-----+ |
| 131 | +``` |
0 commit comments