documentation/query/sql/select.md
SELECT allows you to specify a list of columns and expressions to be selected
and evaluated from a table.
:::tip
Looking for SELECT best practices? Checkout our Maximize your SQL efficiency: SELECT best practices blog.
:::
Note: table can either a specified table in your database or passed forward as
the result of a sub-query.
QuestDB supports SELECT * FROM tablename. When selecting all, you can also
omit most of the statement and pass the table name.
The two examples below are equivalent
trades;
SELECT * FROM trades;
To select specific columns, replace * by the names of the columns you are interested in.
Example:
SELECT timestamp, symbol, side FROM trades;
Using aliases allow you to give expressions or column names of your choice. You can assign an alias to a column or an expression by writing the alias name you want after that expression.
:::note
Alias names and column names must be unique.
:::
SELECT timestamp, symbol,
price AS rate,
amount quantity
FROM trades;
Notice how you can use or omit the AS keyword.
SELECT is capable of evaluating multiple expressions and functions. You can
mix comma separated lists of expressions with the column names you are
selecting.
SELECT timestamp, symbol,
price * 0.25 AS price25pct,
amount > 10 AS over10
FROM trades;
The result of amount > 10 is a boolean. The column will be named "over10" and
take values true or false.
Supports AND/OR, NOT & XOR.
AND returns true if both operands are true, and false otherwise.
OR returns true if at least one of the operands is true.
SELECT
(true AND false) AS this_will_return_false,
(true OR false) AS this_will_return_true;
NOT inverts the truth value of the operand.
SELECT
NOT (true AND false) AS this_will_return_true;
^ is the bitwise XOR operator. It applies only to the Long data type. Depending on what you need, you might prefer to cast the input and output to boolean values.
SELECT
(1 ^ 1) AS will_return_0,
(1 ^ 20) AS will_return_21,
(true::int ^ false::long)::boolean AS will_return_true,
(true::int ^ true::long)::boolean AS will_return_false;
Supported aggregation functions are listed on the aggregation reference.
QuestDB evaluates aggregation functions without need for traditional GROUP BY
whenever there is a mix of column names and aggregation functions
in a SELECT clause. You can have any number of discrete value columns and
any number of aggregation functions. The three statements below are equivalent.
SELECT symbol, avg(price), count()
FROM trades;
SELECT symbol, avg(price), count()
FROM trades
GROUP BY symbol;
SELECT symbol, avg(price), count()
FROM trades
GROUP BY 1;
Aggregation functions can be used in arithmetic expressions. The following
computes mid of prices for every symbol.
SELECT symbol, (min(price) + max(price))/2 mid, count() count
FROM trades;
:::tip
Whenever possible, it is recommended to perform arithmetic outside of
aggregation functions as this can have a dramatic impact on performance. For
example, min(price/2) is going to execute considerably more slowly than
min(price)/2, although both return the same result.
:::
QuestDB supports the following standard SQL clauses within SELECT statements.
Conditional results based on expressions.
For more information, please refer to the CASE reference
Convert values and expression between types.
For more information, please refer to the CAST reference
Returns distinct values of the specified column(s).
For more information, please refer to the DISTINCT reference.
Defines filling strategy for missing data in aggregation queries. This function complements SAMPLE BY queries.
For more information, please refer to the FILL reference.
Join tables based on a key or timestamp.
For more information, please refer to the JOIN reference
Transforms rows into columns by aggregating data across specified values. Useful for analytics, charting, and reshaping time-series data.
For more information, please refer to the PIVOT reference
Specify the number and position of records returned by a query.
For more information, please refer to the LIMIT reference.
Orders the results of a query by one or several columns.
For more information, please refer to the ORDER BY reference
Combine the results of two or more select statements. Can include or ignore duplicates.
For more information, please refer to the UNION, EXCEPT & INTERSECT reference
Filters query results
QuestDB supports complex WHERE clauses along with type-specific searches. For more information, please refer to the WHERE reference. There are different syntaxes for text, numeric, or timestamp filters.
QuestDB augments SQL with the following clauses.
Retrieves the latest entry by timestamp for a given key or combination of keys This function requires a designated timestamp.
For more information, please refer to the LATEST ON reference.
Aggregates time-series data into homogeneous time chunks. For example daily average, monthly maximum etc. This function requires a designated timestamp.
For more information, please refer to the SAMPLE BY reference.
Dynamically creates a designated timestamp on the output of a query. This allows to perform timestamp operations like SAMPLE BY or LATEST ON on tables which originally do not have a designated timestamp.
:::caution
The output query must be ordered by time. TIMESTAMP() does not check for order
and using timestamp functions on unordered data may produce unexpected results.
:::
For more information, refer to the TIMESTAMP reference