Skip to main content

SQL Extensions

On top of standard PostgreSQL syntax, SereneDB offers several SQL extensions and syntactic sugar that make queries more concise and readable.

Clauses​

Query Features​

Literals and Identifiers​

Data Types​

Data Import​

Functions and Expressions​

Join Types​

Trailing Commas​

SereneDB allows trailing commas, both when listing entities (e.g., column and table names) and when constructing LIST items. For example, the following query works:

Query
SELECT    42 AS x,    ['a', 'b', 'c',] AS y,    'hello world' AS z,;
Result
 x  | y       | z----+---------+------------- 42 | {a,b,c} | hello world

"Top-N in Group" Queries​

Computing the "top-N rows in a group" ordered by some criteria is a common task in SQL that unfortunately often requires a complex query involving window functions and/or subqueries.

To aid in this, SereneDB provides the aggregate functions max(arg, n), min(arg, n), arg_max(arg, val, n), arg_min(arg, val, n), max_by(arg, val, n) and min_by(arg, val, n) to efficiently return the "top" n rows in a group based on a specific column in either ascending or descending order.

For example, let's use the following table:

Query
SELECT * FROM t1;
Result
 grp | val-----+----- a   |   2 a   |   1 b   |   5 b   |   4 a   |   3 b   |   6

We want to get a list of the top-3 val values in each group grp. The conventional way to do this is to use a window function in a subquery:

Query
SELECT array_agg(rs.val), rs.grpFROM    (SELECT val, grp, row_number() OVER (PARTITION BY grp ORDER BY val DESC) AS rid    FROM t1 ORDER BY val DESC) AS rsWHERE rid < 4GROUP BY rs.grp;
Result
 array_agg | grp-----------+----- {3,2,1}   | a {6,5,4}   | b

But in SereneDB, we can do this much more concisely (and efficiently!):

Query
SELECT max(val, 3) FROM t1 GROUP BY grp;
Result
 max--------- {3,2,1} {6,5,4}