SQL and data types · Engineering
SQL over documents without joins
NonoDB’s SQL subset translates SELECT, INSERT, UPDATE, and DELETE into the normal document and aggregation paths. Here is what it does well and where it stops.
SQL is a useful interface for reports, BI tools, and operators who do not want to learn a document API. NonoDB supports a practical SQL subset over one collection, and it does that by translating statements into the same storage and query paths used by the JSON API.
That last point matters. SQL is not a second engine with its own indexes and consistency rules. A SELECT sees the same documents, TTL state, indexes, and authorization as find. A write goes through the same single writer and LMDB transaction.
What is supported
The supported statements are:
- read-only
SELECT; - one-collection
INSERT; - one-collection
UPDATE; - one-collection
DELETE.
SELECT supports DISTINCT, WHERE, GROUP BY, HAVING, aggregates including COUNT/SUM/AVG/MIN/MAX(DISTINCT ...), CASE, scalar functions, ORDER BY, LIMIT/OFFSET, and positional parameters.
Conditions include equality and range comparisons, BETWEEN, IN, IS NULL, and LIKE. LIKE is case-sensitive and anchored to the whole value; % matches any sequence and _ matches one character. For advanced regex, use the JSON API’s $regex.
Writes support dotted assignment paths, literal values, parameters, and document expressions:
UPDATE usersSET age = age + 1, profile.level = 9WHERE team = 'core'RETURNING name, age AS next_age;INSERT, UPDATE, and DELETE accept RETURNING. Each SQL write statement is atomic in its own writer transaction. A multi-row INSERT that fails rolls back entirely.
What is not supported
There are no joins, subqueries, INSERT ... SELECT, UPDATE ... FROM, or CREATE/DROP DDL. There are no general transactions spanning statements. One statement reads or writes one collection.
That is a real boundary, not a temporary bug. If the report needs a multi-collection join, use the JSON API’s $lookup (for equality joins) or join in the application. If the write needs atomicity across collections, redesign the model or use write_batch within one collection.
There is also no LIMIT on UPDATE or DELETE, and no upsert. Use the JSON API for find_and_modify, upserts, pipeline updates, and the features SQL does not expose: full-text, fuzzy, geo, and vector search.
How execution works
A simple field-only SELECT delegates to the normal find path, so it inherits index selection, _id ranges, early exit, and covering projections. A grouped, distinct, or computed query becomes an aggregation pipeline: WHERE is a leading $match, GROUP BY becomes $group, HAVING becomes a post-group $match, and the final stages project, sort, skip, and limit.
That means SELECT is subject to the aggregation memory cap when it uses aggregation. The default NONODB_AGGREGATE_MAX_DOCS is 100,000 intermediate documents. A simple indexed select is not subject to that cap and can stop early.
EXPLAIN is available through explain_sql. It returns either the find plan or the generated pipeline, including access path and index bounds. Use it before adding an index for a report.
A practical example
{ "op": "sql", "query": "SELECT team, COUNT(*) AS n, SUM(score) AS total FROM users WHERE active = TRUE GROUP BY team HAVING SUM(score) > 100 ORDER BY total DESC LIMIT 20"}The WHERE can use an index on active or a compound index whose prefix matches. The GROUP BY still holds group state in memory. If the number of teams is small, that is cheap. If it is a million distinct keys, it is not.
Authorization and errors
The collection name is parsed before execution. SELECT and explain_sql require read:<collection>; writes require write:<collection>. SQL writes are not allowed in vault collections or inside a read batch.
Parse and planning failures return invalid_sql with HTTP 400. An unknown or unsupported construct is rejected rather than silently translated into something surprising.
When SQL is the right interface
Use SQL when:
- an operator or BI tool already speaks SQL;
- the query is a report over one collection;
- the write is a straightforward
INSERT/UPDATE/DELETE; - you want
RETURNINGwithout building a separate API flow.
Use the JSON API when:
- the operation needs upserts,
find_and_modify, or pipeline updates; - the query needs text, fuzzy, geo, or vector search;
- the write must atomically cover several collections;
- the report needs joins or a query shape SQL does not support.
The SQL guide has the exact grammar, functions, limits, and error codes. SQL is a convenient surface over the document engine, not a second database.