<?php require_once( dirname(dirname(dirname( __FILE__ ))) . '/wp-load.php' ); ?>
<!-- START of header -->
<?php get_header(); ?>
<!-- END of header -->

<!-- warapper -->

<div class="docs-content">
    <!-- START of page navigation -->
    <?php get_template_part( 'docs_navigation' ); ?>
    <!-- END of page navigation -->
    <!-- START of pusher -->
    <div class="docs-content-body">
        <div id="content" class="docs-content-body__inner">


            <div id="outline-container-1" class="outline-3">
            <h1 id="sec-1"><span class="section-number-3">2.3</span> GridDB Query Language - TQL </h1>
            <div class="outline-text-3" id="text-1">

            If you prefer learning via video: 

<div style="text-align:center">
<iframe width="560" height="315" src="https://www.youtube.com/embed/-PCQUUimQEM" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>


<h3> What is TQL? </h3>
TQL is the query language used by GridDB; it is very similar to SQL but with some notable differences. In this blog post, we'll go over the basic structure of the query statement and the different methods to alter what results are returned. First, let's look at the simplest TQL query:

<code>select *</code>

Unlike SQL, no table or container is specified. This is because the query is run against a specific container/collection object. Unless you're performing an aggregation query, you cannot specify individual columns. More info on aggregation can be found in the <a href=https://griddb.net/en/blog/aggregation-with-griddb/>Aggregation blog post</a>.

<H3>Conditional Operators</H3>

Just like SQL, the <B>where</b> operator along with <b>and</b>, <b>not</b>, <b>or</b>, and parentheses are used to only select fetch data that meet a certain set of conditions. 

<code>select * where temperature > 25.0 and (dayofweek = "Saturday" or dayofweek = "Sunday")</code>

If you want to use a conditional operator on a Date/TIMESTAMP column you cannot just compare against a String date or Unix Epoch value, instead you need to use built-in functions:

<code>select * where timestamp > TO_TIMESTAMP_MS(1560208106016)</code>

or

<code>select * where timestamp > TIMESTAMP('2019-01-01T18:00:00Z')</code>

Conditional operators for booleans slightly differ from its SQL counterpart as well. For example, in SQL, you'd check to see if the column is equal to True or False. But in TQL:

<code>select * where completed</code>

or

<code>select * where not completed</code>

<h3>Ordering</h3>

By default GridDB will return results in the order they were inserted into the database: oldest results are returned first. To change this, the <b>order by</b> operator is used along with either the <b>asc</b> or <b>desc</b> operator. <b>asc</b> or ascending will return the lowest result first while <b>desc</b> is the opposite, returning the highest result first followed by the next highest and so on. 

<code>select * order by timestamp asc</code>

<code>select * order by timestamp desc</code>

<h3>Limits and Offsets</h3>

Just like SQL, TQL supports the limit operator, which limits the number of rows returned by a query.

<code>select * limit 100</code>

To fetch the next 100 rows, offset is also used:

<code>select * limit 100 offset 100</code>

One important thing to note, when using <b>limit</b> and <b>offset</b> with multi-query: the specified numbers are per container. 

Now, if we put everything together, we end up with:

<code>select * where temperature > 20.0 order by timestamp desc limit 100</code> 
            
        </div>
    </div>
</div>
<!-- / main -->

<?php get_footer(); ?>
