SQL Function Reference¶
This page documents SQL functions and aggregate/window additions recently implemented in DecentDB.
For broader syntax coverage, see the SQL reference and feature matrix.
Subquery operators¶
Supported:
EXISTS (subquery)/NOT EXISTS (subquery)expr op ANY (subquery)andexpr op SOME (subquery)(SOMEis a synonym)expr op ALL (subquery)
Behavior notes:
- Subquery comparison operators support
=,<>/!=,<,<=,>,>=. ANYreturnsTRUEif at least one comparison is true;ALLreturnsTRUEonly if all comparisons are true.- Empty subquery semantics follow SQL quantifier rules:
ANYyieldsFALSE,ALLyieldsTRUE. NULLcomparison propagation follows SQL three-valued logic.
Examples:
SELECT * FROM users u
WHERE EXISTS (SELECT 1 FROM orders o WHERE o.user_id = u.id);
SELECT * FROM employees
WHERE salary > ANY (SELECT salary FROM peers);
SELECT * FROM employees
WHERE salary >= ALL (SELECT salary FROM peers);
Regex comparison operators¶
Supported:
left ~ pattern(case-sensitive match)left ~* pattern(case-insensitive match)left !~ pattern(case-sensitive non-match)left !~* pattern(case-insensitive non-match)
Behavior notes:
- Both operands must be
TEXT; otherwise an SQL type error is raised. NULLoperands yieldNULL.- Invalid regex patterns return an SQL error.
Examples:
SELECT name FROM users WHERE name ~ '^A';
SELECT name FROM users WHERE name ~* '^admin';
SELECT name FROM users WHERE name !~ 'bot$';
SELECT name FROM users WHERE name !~* '^test_';
Math functions¶
Trigonometric¶
Supported:
SIN(x)COS(x)TAN(x)ASIN(x)ACOS(x)ATAN(x)ATAN2(y, x)PI()DEGREES(x)RADIANS(x)COT(x)
Behavior notes:
- Numeric inputs are accepted (
INT64,FLOAT64,DECIMAL); outputs areFLOAT64. ASINandACOSreturnNULLfor out-of-domain values outside[-1, 1].TANreturnsNULLnear undefined points (odd multiples ofπ/2).COTreturnsNULLwhentan(x)is approximately zero.
Examples:
SELECT SIN(PI() / 2), COS(0), TAN(PI() / 4);
SELECT ASIN(1), ACOS(0), ATAN2(1, 1);
SELECT DEGREES(PI()), RADIANS(180), COT(PI() / 4);
Conditional functions¶
Supported:
GREATEST(value1, value2, ...)LEAST(value1, value2, ...)IIF(condition, then_value, else_value)
Behavior notes:
GREATEST/LEASTreturnNULLif any argument isNULL.IIFfollowsCASE-like behavior and uses DecentDB truthiness semantics for the condition.
Examples:
Date/time functions¶
Supported:
DATE_TRUNC(precision, timestamp)DATE_PART(field, timestamp)DATE_DIFF(part, start, end)LAST_DAY(timestamp)NEXT_DAY(timestamp, weekday)MAKE_DATE(year, month, day)MAKE_TIMESTAMP(year, month, day, hour, minute, second)TO_TIMESTAMP(epoch_or_text [, format])AGE(timestamp [, timestamp])INTERVAL '...'(for timestamp arithmetic)
Behavior notes:
DATE_TRUNCsupports: microsecond, millisecond, second, minute, hour, day, week, month, quarter, year, decade, century, millennium.TO_TIMESTAMP(text, format)currently supports formats:YYYY-MM-DD HH24:MI:SS,YYYY-MM-DD, andDD/MM/YYYY.AGEreturns a textual interval (for example,"1 days 00:00:00").INTERVALliteral parsing supports integeryear/month/week/day/hour/minute/secondunits in amount-unit pairs.- Timestamp interval arithmetic supports
timestamp +/- INTERVAL '...'and date/timestamp text on the left side.
Examples:
SELECT DATE_TRUNC('month', '2024-03-15 14:30:45');
SELECT DATE_PART('doy', '2024-03-15');
SELECT DATE_DIFF('day', '2024-03-10', '2024-03-15');
SELECT LAST_DAY('2024-02-11'), NEXT_DAY('2024-03-15', 'Monday');
SELECT MAKE_DATE(2024, 3, 15), MAKE_TIMESTAMP(2024, 3, 15, 14, 30, 0);
SELECT TO_TIMESTAMP(1710505800), TO_TIMESTAMP('15/03/2024', 'DD/MM/YYYY');
SELECT AGE('2024-03-15', '2024-03-14');
SELECT '2024-03-15 14:30:00'::timestamp + INTERVAL '1 day';
String functions¶
Supported:
CONCAT(expr, ...)CONCAT_WS(separator, expr, ...)POSITION(substring IN string)INITCAP(string)ASCII(string)REGEXP_REPLACE(string, pattern, replacement [, flags])SPLIT_PART(string, delimiter, index)STRING_TO_ARRAY(string, delimiter)QUOTE_IDENT(string)QUOTE_LITERAL(string)MD5(string)SHA256(string)
Behavior notes:
CONCATtreatsNULLarguments as empty strings.CONCAT_WSskipsNULLvalue arguments;NULLseparator returnsNULL.POSITIONreturns 1-based positions, and0if no match exists.REGEXP_REPLACEsupportsg(global) andi(case-insensitive) flags.STRING_TO_ARRAYreturns a JSON text array.
Examples:
SELECT CONCAT('hello', ' ', 'world');
SELECT CONCAT_WS(', ', 'Alice', NULL, 'Bob');
SELECT POSITION('world' IN 'hello world');
SELECT INITCAP('hello world from decentdb');
SELECT ASCII('A');
SELECT REGEXP_REPLACE('abc123def', '\d', '', 'g');
SELECT SPLIT_PART('a,b,c', ',', 2);
SELECT STRING_TO_ARRAY('a,b,c', ',');
SELECT QUOTE_IDENT('table name'), QUOTE_LITERAL('O''Brien');
SELECT MD5('hello'), SHA256('hello');
Aggregate functions¶
Statistical aggregates¶
Supported:
STDDEV(expr)(alias ofSTDDEV_SAMP)STDDEV_SAMP(expr)STDDEV_POP(expr)VARIANCE(expr)(alias ofVAR_SAMP)VAR_SAMP(expr)VAR_POP(expr)
Behavior notes:
- Implemented using a numerically stable online (Welford-style) accumulation strategy.
*_SAMPforms returnNULLwhen fewer than 2 non-NULLvalues exist.- Population forms return
NULLfor empty input sets. DISTINCTis supported.
Boolean aggregates¶
Supported:
BOOL_AND(expr)BOOL_OR(expr)
Behavior notes:
NULLinputs are ignored.- If all values are
NULL, result isNULL. - Non-boolean non-
NULLinputs are rejected.
Collection and ordered-set aggregates¶
Supported:
ARRAY_AGG(expr [ORDER BY ...])MEDIAN(expr)PERCENTILE_CONT(fraction) WITHIN GROUP (ORDER BY expr)PERCENTILE_DISC(fraction) WITHIN GROUP (ORDER BY expr)
Behavior notes:
ARRAY_AGGreturns JSON text arrays (for example,"[1,null,2]").ARRAY_AGG(DISTINCT ...)is supported.MEDIANreturnsFLOAT64and ignoresNULLinputs.- Percentile fraction must be between
0and1inclusive. PERCENTILE_CONTinterpolates and returnsFLOAT64.PERCENTILE_DISCreturns a value from the ordered input domain.
Examples:
SELECT STDDEV(amount), VARIANCE(amount), BOOL_AND(amount > 0), BOOL_OR(amount > 100) FROM orders;
SELECT ARRAY_AGG(amount ORDER BY created_at) FROM orders;
SELECT MEDIAN(amount) FROM orders;
SELECT PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY amount) FROM orders;
SELECT PERCENTILE_DISC(0.9) WITHIN GROUP (ORDER BY amount) FROM orders;
Window functions¶
Additional supported window features include:
NTILE(n)PERCENT_RANK()CUME_DIST()- Aggregate window functions such as
SUM(...) OVER (...),COUNT(...) OVER (...),MIN/MAX/AVG/... OVER (...) ROWSframe clausesRANGEframes forUNBOUNDED/CURRENT ROWstyle bounds (offset-basedRANGEbounds are not yet supported)
Examples:
SELECT id, NTILE(4) OVER (ORDER BY salary DESC) AS quartile FROM employees;
SELECT id,
PERCENT_RANK() OVER (ORDER BY score) AS pct_rank,
CUME_DIST() OVER (ORDER BY score) AS cume_dist
FROM results;
SELECT created_at, amount,
SUM(amount) OVER (
ORDER BY created_at
ROWS BETWEEN 1 PRECEDING AND CURRENT ROW
) AS rolling_sum
FROM orders;