SELECT ⟨select_list⟩ FROM ⟨tables⟩ [WHERE ⟨condition⟩] [GROUP BY ⟨groups⟩] [HAVING ⟨group_filter⟩] [FILL ⟨fill_methods⟩] [ORDER BY ⟨order_expression⟩] [OFFSET ⟨n⟩] [LIMIT ⟨n⟩];
The IoTDB table model query syntax supports the following clauses:
JOIN clause, or a subquery. Details: FROM & JOIN ClauseFROM clause. Details: WHERE ClauseGROUP BY clause to filter grouped data, similar to WHERE but operates after grouping. Details:HAVING ClauseASC) or descending (DESC) order, with optional handling for null values (NULLS FIRST or NULLS LAST). Details: ORDER BY ClauseOFFSET rows. Often used with the LIMIT clause. Details: LIMIT and OFFSET ClauseOFFSET clause for pagination. Details: LIMIT and OFFSET ClauseThe Example Data pagepage provides SQL statements to construct table schemas and insert data. By downloading and executing these statements in the IoTDB CLI, you can import the data into IoTDB. This data can be used to test and run the example SQL queries included in this documentation, allowing you to reproduce the described results.
Example 1: Filter by Time
IoTDB> SELECT time, temperature, humidity FROM table1 WHERE time >= 2024-11-27 00:00:00 and time <= 2024-11-29 00:00:00;
Result:
+-----------------------------+-----------+--------+ | time|temperature|humidity| +-----------------------------+-----------+--------+ |2024-11-28T08:00:00.000+08:00| 85.0| null| |2024-11-28T09:00:00.000+08:00| null| 40.9| |2024-11-28T10:00:00.000+08:00| 85.0| 35.2| |2024-11-28T11:00:00.000+08:00| 88.0| 45.1| |2024-11-27T16:38:00.000+08:00| null| 35.1| |2024-11-27T16:39:00.000+08:00| 85.0| 35.3| |2024-11-27T16:40:00.000+08:00| 85.0| null| |2024-11-27T16:41:00.000+08:00| 85.0| null| |2024-11-27T16:42:00.000+08:00| null| 35.2| |2024-11-27T16:43:00.000+08:00| null| null| |2024-11-27T16:44:00.000+08:00| null| null| +-----------------------------+-----------+--------+ Total line number = 11 It costs 0.075s
Example 2: Filter by Value
IoTDB> SELECT time, temperature, humidity FROM table1 WHERE temperature > 89.0;
Result:
+-----------------------------+-----------+--------+ | time|temperature|humidity| +-----------------------------+-----------+--------+ |2024-11-29T18:30:00.000+08:00| 90.0| 35.4| |2024-11-26T13:37:00.000+08:00| 90.0| 35.1| |2024-11-26T13:38:00.000+08:00| 90.0| 35.1| |2024-11-30T09:30:00.000+08:00| 90.0| 35.2| |2024-11-30T14:30:00.000+08:00| 90.0| 34.8| +-----------------------------+-----------+--------+ Total line number = 5 It costs 0.156s
Example 3: Filter by Attribute
IoTDB> SELECT time, temperature, humidity FROM table1 WHERE model_id ='B';
Result:
+-----------------------------+-----------+--------+ | time|temperature|humidity| +-----------------------------+-----------+--------+ |2024-11-27T16:38:00.000+08:00| null| 35.1| |2024-11-27T16:39:00.000+08:00| 85.0| 35.3| |2024-11-27T16:40:00.000+08:00| 85.0| null| |2024-11-27T16:41:00.000+08:00| 85.0| null| |2024-11-27T16:42:00.000+08:00| null| 35.2| |2024-11-27T16:43:00.000+08:00| null| null| |2024-11-27T16:44:00.000+08:00| null| null| +-----------------------------+-----------+--------+ Total line number = 7 It costs 0.106s
Example 3:Multi device time aligned query
IoTDB> SELECT date_bin_gapfill(1d, TIME) AS a_time, device_id, AVG(temperature) AS avg_temp FROM table1 WHERE TIME >= 2024-11-26 13:00:00 AND TIME <= 2024-11-27 17:00:00 GROUP BY 1, device_id FILL METHOD PREVIOUS;
Result:
+-----------------------------+---------+--------+ | a_time|device_id|avg_temp| +-----------------------------+---------+--------+ |2024-11-26T08:00:00.000+08:00| 100| 90.0| |2024-11-27T08:00:00.000+08:00| 100| 90.0| |2024-11-26T08:00:00.000+08:00| 101| 90.0| |2024-11-27T08:00:00.000+08:00| 101| 85.0| +-----------------------------+---------+--------+ Total line number = 4 It costs 0.048s
Example: Calculate the average, maximum, and minimum temperature for each device_id within a specific time range.
IoTDB> SELECT device_id, AVG(temperature) as avg_temp, MAX(temperature) as max_temp, MIN(temperature) as min_temp FROM table1 WHERE time >= 2024-11-26 00:00:00 AND time <= 2024-11-29 00:00:00 GROUP BY device_id;
Result:
+---------+--------+--------+--------+ |device_id|avg_temp|max_temp|min_temp| +---------+--------+--------+--------+ | 100| 87.6| 90.0| 85.0| | 101| 85.0| 85.0| 85.0| +---------+--------+--------+--------+ Total line number = 2 It costs 0.278s
Example: Retrieve the latest record for each device_id, including the temperature value and the timestamp of the last record.
IoTDB> SELECT device_id,last(time),last_by(temperature,time) FROM table1 GROUP BY device_id;
Result:
+---------+-----------------------------+-----+ |device_id| _col1|_col2| +---------+-----------------------------+-----+ | 100|2024-11-29T18:30:00.000+08:00| 90.0| | 101|2024-11-30T14:30:00.000+08:00| 90.0| +---------+-----------------------------+-----+ Total line number = 2 It costs 0.090s
Example: Group data by day and calculate the average temperature using date_bin_gapfill function.
IoTDB> SELECT device_id,date_bin(1d ,time) as day_time, AVG(temperature) as avg_temp FROM table1 WHERE time >= 2024-11-26 00:00:00 AND time <= 2024-11-30 00:00:00 GROUP BY device_id,date_bin(1d ,time);
Result:
+---------+-----------------------------+--------+ |device_id| day_time|avg_temp| +---------+-----------------------------+--------+ | 100|2024-11-29T08:00:00.000+08:00| 90.0| | 100|2024-11-28T08:00:00.000+08:00| 86.0| | 100|2024-11-26T08:00:00.000+08:00| 90.0| | 101|2024-11-29T08:00:00.000+08:00| 85.0| | 101|2024-11-27T08:00:00.000+08:00| 85.0| +---------+-----------------------------+--------+ Total line number = 5 It costs 0.066s
Table 1: Sampling Frequency: 1s
| Time | device_id | temperature |
|---|---|---|
| 00:00:00.001 | d1 | 90.0 |
| 00:00:01.002 | d1 | 85.0 |
| 00:00:02.101 | d1 | 85.0 |
| 00:00:03.201 | d1 | null |
| 00:00:04.105 | d1 | 90.0 |
| 00:00:05.023 | d1 | 85.0 |
| 00:00:06.129 | d1 | 90.0 |
Table 2: Sampling Frequency: 1s
| Time | device_id | humidity |
|---|---|---|
| 00:00:00.003 | d1 | 35.1 |
| 00:00:01.012 | d1 | 37.2 |
| 00:00:02.031 | d1 | null |
| 00:00:03.134 | d1 | 35.2 |
| 00:00:04.201 | d1 | 38.2 |
| 00:00:05.091 | d1 | 35.4 |
| 00:00:06.231 | d1 | 35.1 |
Example: Querying the downsampled data of table1:
IoTDB> SELECT date_bin_gapfill(1s, TIME) AS a_time, first(temperature) AS a_value FROM table1 WHERE device_id = 'd1' AND TIME >= 2025-05-13 00:00:00.000 AND TIME <= 2025-05-13 00:00:07.000 GROUP BY 1 FILL METHOD PREVIOUS
Result:
+-----------------------------+-------+ | a_time|a_value| +-----------------------------+-------+ |2025-05-13T00:00:00.000+08:00| 90.0| |2025-05-13T00:00:01.000+08:00| 85.0| |2025-05-13T00:00:02.000+08:00| 85.0| |2025-05-13T00:00:03.000+08:00| 85.0| |2025-05-13T00:00:04.000+08:00| 90.0| |2025-05-13T00:00:05.000+08:00| 85.0| |2025-05-13T00:00:06.000+08:00| 90.0| +-----------------------------+-------+
Example: Querying the downsampled data of table2:
IoTDB> SELECT date_bin_gapfill(1s, TIME) AS b_time, first(humidity) AS b_value FROM table2 WHERE device_id = 'd1' AND TIME >= 2025-05-13 00:00:00.000 AND TIME <= 2025-05-13 00:00:07.000 GROUP BY 1 FILL METHOD PREVIOUS
Result:
+-----------------------------+-------+ | b_time|b_value| +-----------------------------+-------+ |2025-05-13T00:00:00.000+08:00| 35.1| |2025-05-13T00:00:01.000+08:00| 37.2| |2025-05-13T00:00:02.000+08:00| 37.2| |2025-05-13T00:00:03.000+08:00| 35.2| |2025-05-13T00:00:04.000+08:00| 38.2| |2025-05-13T00:00:05.000+08:00| 35.4| |2025-05-13T00:00:06.000+08:00| 35.1| +-----------------------------+-------+
Example: Aligning multiple sequences by integer time:
IoTDB> SELECT time, a_value, b_value FROM (SELECT date_bin_gapfill(1s, TIME) AS time, first(temperature) AS a_value FROM table1 WHERE device_id = 'd1' AND TIME >= 2025-05-13 00:00:00.000 AND TIME <= 2025-05-13 00:00:07.000 GROUP BY 1 FILL METHOD PREVIOUS) A JOIN (SELECT date_bin_gapfill(1s, TIME) AS time, first(humidity) AS b_value FROM table2 WHERE device_id = 'd1' AND TIME >= 2025-05-13 00:00:00.000 AND TIME <= 2025-05-13 00:00:07.000 GROUP BY 1 FILL METHOD PREVIOUS) B USING (time)
Result:
+-----------------------------+-------+-------+ | time|a_value|b_value| +-----------------------------+-------+-------+ |2025-05-13T00:00:00.000+08:00| 90.0| 35.1| |2025-05-13T00:00:01.000+08:00| 85.0| 37.2| |2025-05-13T00:00:02.000+08:00| 85.0| 37.2| |2025-05-13T00:00:03.000+08:00| 85.0| 35.2| |2025-05-13T00:00:04.000+08:00| 90.0| 38.2| |2025-05-13T00:00:05.000+08:00| 85.0| 35.4| |2025-05-13T00:00:06.000+08:00| 90.0| 35.1| +-----------------------------+-------+-------+
IoTDB> SELECT time, a_value, b_value FROM (SELECT date_bin_gapfill(1s, TIME) AS time, first(temperature) AS a_value FROM table1 WHERE device_id = 'd1' AND TIME >= 2025-05-13 00:00:00.000 AND TIME <= 2025-05-13 00:00:07.000 GROUP BY 1) A JOIN (SELECT date_bin_gapfill(1s, TIME) AS time, first(humidity) AS b_value FROM table2 WHERE device_id = 'd1' AND TIME >= 2025-05-13 00:00:00.000 AND TIME <= 2025-05-13 00:00:07.000 GROUP BY 1) B USING (time)
Result:
+-----------------------------+-------+-------+ | time|a_value|b_value| +-----------------------------+-------+-------+ |2025-05-13T00:00:00.000+08:00| 90.0| 35.1| |2025-05-13T00:00:01.000+08:00| 85.0| 37.2| |2025-05-13T00:00:02.000+08:00| 85.0| null| |2025-05-13T00:00:03.000+08:00| null| 35.2| |2025-05-13T00:00:04.000+08:00| 90.0| 38.2| |2025-05-13T00:00:05.000+08:00| 85.0| 35.4| |2025-05-13T00:00:06.000+08:00| 90.0| 35.1| +-----------------------------+-------+-------+
Table 1: Sampling Frequency: 1s
| Time | device_id | temperature |
|---|---|---|
| 00:00:00.001 | d1 | 90.0 |
| 00:00:01.002 | d1 | 85.0 |
| 00:00:02.101 | d1 | 85.0 |
| 00:00:03.201 | d1 | null |
| 00:00:04.105 | d1 | 90.0 |
| 00:00:05.023 | d1 | 85.0 |
| 00:00:06.129 | d1 | 90.0 |
Table 3: Sampling Frequency: 2s
| Time | device_id | humidity |
|---|---|---|
| 00:00:00.005 | d1 | 35.1 |
| 00:00:02.106 | d1 | 37.2 |
| 00:00:04.187 | d1 | null |
| 00:00:06.156 | d1 | 35.1 |
Example: Querying the downsampled data of table1:
IoTDB> SELECT date_bin_gapfill(1s, TIME) AS a_time, first(temperature) AS a_value FROM table1 WHERE device_id = 'd1' AND TIME >= 2025-05-13 00:00:00.000 AND TIME <= 2025-05-13 00:00:07.000 GROUP BY 1 FILL METHOD PREVIOUS
Result:
+-----------------------------+-------+ | a_time|a_value| +-----------------------------+-------+ |2025-05-13T00:00:00.000+08:00| 90.0| |2025-05-13T00:00:01.000+08:00| 85.0| |2025-05-13T00:00:02.000+08:00| 85.0| |2025-05-13T00:00:03.000+08:00| 85.0| |2025-05-13T00:00:04.000+08:00| 90.0| |2025-05-13T00:00:05.000+08:00| 85.0| |2025-05-13T00:00:06.000+08:00| 90.0| +-----------------------------+-------+
Example: Querying the downsampled data of table3:
IoTDB> SELECT date_bin_gapfill(1s, TIME) AS c_time, first(humidity) AS c_value FROM table3 WHERE device_id = 'd1' AND TIME >= 2025-05-13 00:00:00.000 AND TIME <= 2025-05-13 00:00:07.000 GROUP BY 1 FILL METHOD PREVIOUS
Result:
+-----------------------------+-------+ | c_time|c_value| +-----------------------------+-------+ |2025-05-13T00:00:00.000+08:00| 35.1| |2025-05-13T00:00:01.000+08:00| 35.1| |2025-05-13T00:00:02.000+08:00| 37.2| |2025-05-13T00:00:03.000+08:00| 37.2| |2025-05-13T00:00:04.000+08:00| 37.2| |2025-05-13T00:00:05.000+08:00| 37.2| |2025-05-13T00:00:06.000+08:00| 35.1| +-----------------------------+-------+
Example: Aligning multiple sequences by the higher sampling frequency:
IoTDB> SELECT time, a_value, c_value FROM (SELECT date_bin_gapfill(1s, TIME) AS time, first(temperature) AS a_value FROM table1 WHERE device_id = 'd1' AND TIME >= 2025-05-13 00:00:00.000 AND TIME <= 2025-05-13 00:00:07.000 GROUP BY 1 FILL METHOD PREVIOUS) A JOIN (SELECT date_bin_gapfill(1s, TIME) AS time, first(humidity) AS c_value FROM table3 WHERE device_id = 'd1' AND TIME >= 2025-05-13 00:00:00.000 AND TIME <= 2025-05-13 00:00:07.000 GROUP BY 1 FILL METHOD PREVIOUS) C USING (time)
Result:
+-----------------------------+-------+-------+ | time|a_value|c_value| +-----------------------------+-------+-------+ |2025-05-13T00:00:00.000+08:00| 90.0| 35.1| |2025-05-13T00:00:01.000+08:00| 85.0| 35.1| |2025-05-13T00:00:02.000+08:00| 85.0| 37.2| |2025-05-13T00:00:03.000+08:00| 85.0| 37.2| |2025-05-13T00:00:04.000+08:00| 90.0| 37.2| |2025-05-13T00:00:05.000+08:00| 85.0| 37.2| |2025-05-13T00:00:06.000+08:00| 90.0| 35.1| +-----------------------------+-------+-------+
Example: Query the records within a specified time range where device_id is ‘100’. If there are missing data points, fill them using the previous non-null value.
IoTDB> SELECT time, temperature, humidity FROM table1 WHERE time >= 2024-11-26 00:00:00 and time <= 2024-11-30 11:00:00 AND region='East' AND plant_id='1001' AND device_id='101' FILL METHOD PREVIOUS;
Result:
+-----------------------------+-----------+--------+ | time|temperature|humidity| +-----------------------------+-----------+--------+ |2024-11-27T16:38:00.000+08:00| null| 35.1| |2024-11-27T16:39:00.000+08:00| 85.0| 35.3| |2024-11-27T16:40:00.000+08:00| 85.0| 35.3| |2024-11-27T16:41:00.000+08:00| 85.0| 35.3| |2024-11-27T16:42:00.000+08:00| 85.0| 35.2| |2024-11-27T16:43:00.000+08:00| 85.0| 35.2| |2024-11-27T16:44:00.000+08:00| 85.0| 35.2| +-----------------------------+-----------+--------+ Total line number = 7 It costs 0.101s
Example: Query records from the table, sorting by humidity in descending order and placing null values (NULL) at the end. Skip the first 2 rows and return the next 8 rows.
IoTDB> SELECT time, temperature, humidity FROM table1 ORDER BY humidity desc NULLS LAST OFFSET 2 LIMIT 10;
Result:
+-----------------------------+-----------+--------+ | time|temperature|humidity| +-----------------------------+-----------+--------+ |2024-11-28T09:00:00.000+08:00| null| 40.9| |2024-11-29T18:30:00.000+08:00| 90.0| 35.4| |2024-11-27T16:39:00.000+08:00| 85.0| 35.3| |2024-11-28T10:00:00.000+08:00| 85.0| 35.2| |2024-11-30T09:30:00.000+08:00| 90.0| 35.2| |2024-11-27T16:42:00.000+08:00| null| 35.2| |2024-11-26T13:38:00.000+08:00| 90.0| 35.1| |2024-11-26T13:37:00.000+08:00| 90.0| 35.1| |2024-11-27T16:38:00.000+08:00| null| 35.1| |2024-11-30T14:30:00.000+08:00| 90.0| 34.8| +-----------------------------+-----------+--------+ Total line number = 10 It costs 0.093s