To find the latest values, QuestDB will search time series from the newest values to the oldest.

  • For single SYMBOL columns, QuestDB will know all distinct values upfront and stop once the latest entry has been found for each symbol value.

:::note

To use LATEST BY, one column needs to be designated as timestamp. Find out more in the designated timestamp section.

:::

:::note

By default, QuestDB executes clauses before latest by. To execute where after , you need to use sub-queries using brackets. You can learn how to do this in the .

:::

Examples

LATEST BY can be used with single columns. When this column is of type SYMBOL, the query will end as soon as all distinct symbol values have been found.

The below queries illustrate how to change the execution order in a query by using brackets. Assume the following table

This query executes WHERE before LATEST BY. It will return the latest balance which is above 800. The steps are:

  • Filter out all balances below 800.
  • Finds the latest balance for each combination of cust_id and balance_ccy.

Since the latest USD balance for customer 1 is equal to 330.5, it is filtered out in the first step. Therefore, the returned balance is 1500, which is the latest possible balance above 800.

This query executes before WHERE. It returns the latest balances, then filters out those below 800. The steps are

  • Find the latest balances, regardless of value