{"id":14766,"date":"2023-08-01T12:00:00","date_gmt":"2023-08-01T19:00:00","guid":{"rendered":"https:\/\/tdengine.com\/?p=14766"},"modified":"2025-03-30T22:17:25","modified_gmt":"2025-03-31T05:17:25","slug":"sql-support","status":"publish","type":"post","link":"https:\/\/tdengine.com\/sql-support\/","title":{"rendered":"Standard SQL Support"},"content":{"rendered":"\n<p>TDengine supports standard SQL for querying data, using a model that is familiar to users of relational databases. SQL support makes it easier to fit TDengine into your existing data stack and reduces the learning curve for new users. This is a departure from many <a href=\"https:\/\/tdengine.com\/what-is-a-time-series-database\/\">time-series databases<\/a> (TSDB) and traditional <a href=\"https:\/\/tdengine.com\/what-is-a-data-historian\/\">data historians<\/a>, which often use proprietary query languages and sometimes provide only APIs for querying. In addition, TDengine has extended the standard SQL syntax to facilitate analysis and processing of time series data.<\/p>\n\n\n\n<h2 class=\"gb-headline gb-headline-f3381734 gb-headline-text\">Query Syntax: InfluxDB vs. TDengine<\/h2>\n\n\n\n<p>We can demonstrate the simplicity of SQL as opposed to proprietary query languages with a quick example. Suppose we want to query the average voltage per minute of all smart meters in Chicago in the past hour from the database power. The query statement in InfluxDB&#8217;s Flux language would be written as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\" data-line=\"\">from(bucket:&quot;power&quot;)\n|&gt; range(start:-1h)\n|&gt; filter(fn:(r) =&gt;\nr._measurement == &quot;smeter&quot; and\nr._field = &quot;voltage&quot; and\nr.location == &quot;chicago&quot;\n)\n|&gt; aggregateWindow(every: 1m, fn: mean)<\/code><\/pre>\n\n\n\n<p>In TDengine, the same query would be written as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-sql\"><code class=\"\" data-line=\"\">SELECT AVG(voltage) FROM power.smeter WHERE ts &gt; now -1h AND location = &quot;chicago&quot; INTERVAL(1m);<\/code><\/pre>\n\n\n\n<p>This shows how much easier it can be to compose SQL statements for common time series operations instead of using proprietary query languages.<\/p>\n\n\n\n<h2 class=\"gb-headline gb-headline-f2e7f47c gb-headline-text\">Data Ingestion<\/h2>\n\n\n\n<p>TDengine uses the traditional relational model for data ingestion. In this model, a schema is defined for the data when tables are created. Although this does require extra effort, its advantages quickly become clear as datasets increase in size: querying data remains a simple task even for large-scale data. In addition, because most IoT and industrial devices transmit data with a consistent structure, the benefits of schemaless ingestion are much less evident than in other scenarios where data structure may be unknown or mutable.<\/p>\n\n\n\n<p>For use cases that require schemaless ingestion, however, TDengine supports the InfluxDB Line Protocol and the OpenTSDB telnet line protocol and JSON format protocol.<\/p>\n\n\n\n<h2 class=\"gb-headline gb-headline-ee764ab8 gb-headline-text\">SQL Extensions<\/h2>\n\n\n\n<p>The analysis of time series data has its own characteristics and requires some special analysis functions that are not supported by traditional relational databases. To make it easier to perform analytics, TDengine provides the following extensions to standard SQL:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code class=\"\" data-line=\"\">INTERVAL<\/code>: TDengine aggregates the collected data by time interval, such as calculating the average voltage every 5 minutes<\/li>\n\n\n\n<li><code class=\"\" data-line=\"\">FILL<\/code>: At a specific time point, when there is no data, TDengine can perform interpolation calculation based on the data before and after this time point and provide it to the application.<\/li>\n\n\n\n<li><code class=\"\" data-line=\"\">SLIDING<\/code>: Sliding window. TDengine can perform query calculation at specified time intervals.<\/li>\n\n\n\n<li><code class=\"\" data-line=\"\">LAST_ROW<\/code>: Returns the last non-NULL value of a column in a table. This is very important for IoT applications, because the current state of the device needs to be quickly obtained.<\/li>\n\n\n\n<li><code class=\"\" data-line=\"\">TAVG<\/code>: Time-weighted average, which is often used in the Industrial Internet.<\/li>\n\n\n\n<li><code class=\"\" data-line=\"\">SPREAD<\/code>: count the difference between the maximum and minimum values \u200b\u200bof a column.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>TDengine uses standard SQL as its query language, making it simple for anyone to learn.<\/p>\n","protected":false},"author":2,"featured_media":21174,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"content-type":"","footnotes":""},"categories":[21],"tags":[],"ppma_author":[120],"class_list":["post-14766","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-engineering"],"authors":[{"term_id":120,"user_id":2,"is_guest":0,"slug":"jianhui-tao","display_name":"Jeff Tao","avatar_url":{"url":"https:\/\/tdengine.com\/wp-content\/uploads\/29.04-15-jhtao.jpg","url2x":"https:\/\/tdengine.com\/wp-content\/uploads\/29.04-15-jhtao.jpg"},"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/tdengine.com\/wp-json\/wp\/v2\/posts\/14766","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tdengine.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tdengine.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tdengine.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/tdengine.com\/wp-json\/wp\/v2\/comments?post=14766"}],"version-history":[{"count":10,"href":"https:\/\/tdengine.com\/wp-json\/wp\/v2\/posts\/14766\/revisions"}],"predecessor-version":[{"id":24688,"href":"https:\/\/tdengine.com\/wp-json\/wp\/v2\/posts\/14766\/revisions\/24688"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tdengine.com\/wp-json\/wp\/v2\/media\/21174"}],"wp:attachment":[{"href":"https:\/\/tdengine.com\/wp-json\/wp\/v2\/media?parent=14766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tdengine.com\/wp-json\/wp\/v2\/categories?post=14766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tdengine.com\/wp-json\/wp\/v2\/tags?post=14766"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/tdengine.com\/wp-json\/wp\/v2\/ppma_author?post=14766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}