DQL now supports projections and aggregates in addition to
SELECT * operations. You can select specific fields, perform calculations, and use aggregate functions like COUNT, SUM, AVG, MIN, and MAX.SELECT Statements
The following syntax outlines the basic structure and optional clauses you can use within yourSELECT statements:
DQL
projection can be:
*- Returns all fields from the documentsfield1, field2, ...- Returns specific fieldsexpression AS alias- Returns calculated values with custom names- Aggregate functions like
COUNT(*),SUM(field), etc.
cars collection WHERE the color property is set to the value βblueβ :
DQL
Clauses for Filtering
The following table provides an overview of the different clauses you can use to define specific conditions and calculations within your DQLSELECT statements to provide more granular control over your queries:
FROM
Required in eachSELECT statement you write in DQL, the FROM element identifies the collection for document retrieval.
DQL
SELECT statement querying documents from the cars collection:
DQL
USE IDS
The optionalUSE IDS clause allows you to efficiently retrieve specific documents by their IDs without performing a collection scan. This is more performant than using WHERE _id IN (...) filters.
Syntax:
DQL
DQL
Performance Tip: Where possible, simple equality filters on
_id (like WHERE _id = '123') are automatically optimized internally similar to USE IDS. However, for explicit control and guaranteed performance, USE IDS is recommended. See also the #auto_use_ids directive in Directives.Collection Aliasing
You can assign an alias to a collection in the FROM clause to create shorter, more readable queries. TheAS keyword is optional:
DQL
- Complex queries: Shortening long collection names for readability
- Qualified field references: Explicitly referencing fields from a specific collection
- Query directives: Specifying collection-level directives (see Directives)
DQL
JOIN
Joins are only supported for local SDK queries. Joins are not supported in subscription queries or Big Peer/Server queries. See restrictions for more details.
FROM clause may be followed by one or more JOIN terms to correlate documents from additional collections. Ditto supports INNER JOIN, LEFT OUTER JOIN, and RIGHT OUTER JOIN. Each join requires an access path that isnβt a full collection scan on the inner collection (by default); the ADVISE statement can recommend appropriate indexes.
DQL
WHERE
TheWHERE clause filters data based on either an expression or a set of conditions that narrow the result set returned to you:
DQL
SELECT statement querying documents from the cars collection based on a given address:
DQL
SELECT statement that queries using multiple expressions and logical operators to further refine the criteria for document retrieval:
DQL
ORDER BY
With theORDER BY clause, if youβd like, you can integrate calculations or expressions in your SELECT statement. Then sort the resulting documents to return in either ascending (ASC) or descending (DESC) alphabetical order:
DQL
SELECT statement that uses the ORDER BY clause to query and sort documents from the cars collection in descending (DESC) alphabetical order based on the field value set for the color property:
DQL
your_collection_nameis the name of the collection from which you want to retrieve the data.expression_1, expression_2, ...are the expressions evaluated to sort the result. Expressions are resolved in order.[ASC|DESC]is an optional parameter that specifies the sort order. If omitted, the default sort order is ascending (ASC). To sort in descending order, you can specifyDESC.
DQL
"blue" cars return first and other cars sort by the natural order in the collection:
DQL
Sort Order by Object Type
In DQL, the hierarchy for comparing and sorting objects varies based on the following criteria:- If ascending (
ASC) order operations:booleannumberbinarystringarrayobjectnullmissing
- If descending (
DESC) order operations, sorting order is reversed:missingnullobjectarraystringbinarynumberboolean
- If evaluating values,
trueresults are prioritized and ordered first followed byfalseresults.
Expressing Sort Order
Unless explicitly defined asDESC in your query, Ditto defaults to sorting in ascending (ASC). So, if you want to sort in ascending order, you do not have to express that in your query.
LIMIT
TheLIMIT clause is used to restrict the number of documents returned by a query, allowing you to specify a maximum limit on the number of documents to be included in the result set:
DQL
your_collection_nameis the name of the collection from which you want to retrieve the data.limit_valueis the maximum number of documents you want to include in the result set.
your_collection_name collection:
DQL
OFFSET
TheOFFSET clause is used to specify the number of records to skip before starting to return documents from the query result:
DQL
your_collection_nameis the name of the collection from which you want to retrieve the data.number_of_items_to_skipis the number of items before returning the result set.
OFFSET with LIMIT is a common way to utilize OFFSET; for example:
DQL
Projections
Projections allow you to specify exactly which fields or calculated values to return from your query, rather than returning entire documents. This reduces data transfer and processing overhead.The same projection syntax is used by the
RETURNING clause of INSERT, UPDATE and DELETE/EVICT/TOMBSTONE statements, with a small number of restrictions.Basic Field Selection
Select specific fields from documents:DQL
Excluding Fields
To exclude specific fields from a projection, use theMISSING keyword. This can be combined with * to select all fields except those explicitly excluded:
DQL
Aliasing
Use aliases to rename fields in your results:DQL
- Simple field references use the field name as the alias
- Expressions get aliases like
($1),($2), etc.
Alias Constraints
Aliases must follow these rules:- Uniqueness: Each alias in a SELECT list must be unique. Duplicate aliases will raise an error.
- Identifier Rules: Aliases must be valid identifiers following field naming conventions.
- Special Characters: Use backticks (grave accents) to quote aliases containing special characters or reserved words.
DQL
Alias Scope
Projection aliases defined in the SELECT list can be referenced by the ORDER BY clause to sort by an aliased expression:DQL
GROUP BY and HAVING do not accept projection aliases. They must reference the underlying source expression or aggregate function directly: use GROUP BY color (the source field) rather than GROUP BY c (a projection alias), and HAVING COUNT(*) > 5 (the aggregate) rather than HAVING car_count > 5 (the alias).
Calculated Fields
Create new fields using expressions:DQL
DISTINCT Results
Remove duplicate rows from your results:DQL
Projections in a
RETURNING clause may not specify DISTINCT; there, the word is taken to be a field name rather than a keyword.Aggregate Functions
Aggregate functions perform calculations across multiple documents and return a single result. DQL supports the following aggregate functions:For a quick reference table of all aggregate functions and their syntax, see Operator Expressions - Aggregate Functions.
COUNT
Count documents or non-null values:DQL
SUM
Calculate the sum of numeric values:DQL
AVG
Calculate the average of numeric values:DQL
MIN and MAX
Find minimum and maximum values:DQL
MID
Calculate the midpoint between minimum and maximum values:DQL
MID function returns the midpoint value between MIN(expr) and MAX(expr). Non-numeric values are silently ignored.
MEDIAN
Calculate the median (middle) value:DQL
MEDIAN function returns the middle value when all values are sorted. Non-numeric values are silently ignored.
Combining Aggregates
Use multiple aggregate functions in a single query:DQL
GROUP BY
Group results and calculate aggregates for each group:DQL
GROUP BY, non-aggregate projections must be part of the GROUP BY clause:
DQL
HAVING
Filter grouped results based on aggregate values:DQL
Aggregate functions form a βdamβ in the execution pipeline - all documents must be processed before results can be returned. This differs from non-aggregate queries which can stream results.
Aggregate Function Behavior
Understanding how aggregate functions work internally helps optimize query performance: Memory Requirements:- Aggregates accumulate results per group, so memory usage depends on the number of groups
- A small number of groups requires little memory regardless of document count
- A large number of groups increases memory requirements proportionally
DISTINCTaggregates must buffer all distinct values in memory, which can significantly increase memory usage with many unique values
- If no documents qualify for an aggregate in a group, the result is
MISSING(absent from results) - To always include a result, use conditional functions:
SUM(IFMISSING(field, 0)) MINandMAXsilently ignoreMISSINGvaluesCOUNTdoes not countNULL,MISSING, orFALSEvalues unless explicitly handled
SUM,AVG,MID, andMEDIANsilently ignore non-numeric valuesMINandMAXcompare values using Dittoβs standard type ordering rulesCOUNT(*)is equivalent toCOUNT(true)with no additional overhead
- Use
COUNT(*)instead ofCOUNT(field)when counting all documents - Avoid
DISTINCTwith aggregates unless necessary due to memory overhead - Minimize the number of groups in
GROUP BYfor better memory efficiency - Use
HAVINGto filter groups rather than filtering after aggregation
Advanced Aggregate Examples
Handling MISSING Values:DQL
DQL
DQL
DQL
DQL
DQL
DQL
Joins
Joins are available in SDK version 5.1 and later and are supported only on the Ditto SDK (small peer). They are not supported in Big Peer (cloud/server) environments. See Restrictions for details.
SELECT statement. Rather than issuing separate queries and stitching results together in application code, you express the relationship directly in DQL and let the query engine handle it efficiently.
A join appends fields from an additional collection to each row produced by the preceding collection (or join result), based on a condition you supply in the ON clause. The engine iterates over the outer (driving) collection (or join result) and, for each outer row, probes the inner (joined) collection using an appropriate access path in a strategy known as a nested-loop join (nlJoin in EXPLAIN output).
Why use joins?
- Avoid data duplication. Store related data in separate collections and compose views on demand rather than embedding copies everywhere.
- Cross-collection queries. Filter, sort, and project fields from multiple collections in one round trip to the store.
- Richer aggregations. Group and aggregate across collections β for example, sum order amounts per customer, or count products per order.
customers collection and an orders collection. Without joins you would fetch all orders, then issue a second query per customer to look up the name. With a join you express this directly:
DQL
Syntax
Each term (collection or prior join) in the FROM clause may be followed by a JOIN.Join types
ON condition
TheON expression may reference any collection alias that has already been introduced in the FROM clause β that is, the driving collection and any collections joined before the current term. It may be any valid DQL expression, including compound conditions with AND or OR.
Collection aliases
Aliases are strongly recommended when joining so that field references are unambiguous. Aliases must be unique across all collections in the statement.DQL
USE IDS, USE INDEX, and USE DIRECTIVES
Each collection term in a join may includeUSE IDS, USE INDEX, or USE DIRECTIVES sub-clauses to control how that collection is accessed. These apply per-collection and are particularly useful for the inner leg of a join.
DQL
Index requirements
Creating a join index
The join key on the inner (right-hand) collection must be indexed. Create the index before executing a join query:DQL
INCLUDE MISSING option ensures documents that lack the cust_id field are covered by the index and will not be silently omitted from results.
Getting index recommendations with ADVISE
If you are unsure which indexes a join requires, runADVISE on the statement. The engine analyses the query and suggests the optimal indexes, including composite covering indexes where beneficial:
DQL
ADVISE confirms this:
Examples
The examples below use these two collections:customersβ_id,cust_id(integer),name,tierordersβ_id,order_id(integer),cust_id(integer),amount,status
DQL
INNER JOIN
Returns only rows where the ON condition is satisfied in both collections. Customers with no orders and orders with no matching customer are excluded.DQL
LEFT OUTER JOIN
All customers are returned. Where no matching order exists, the order-side fields (o.amount) are MISSING in the result document.
DQL
MISSING:
DQL
RIGHT OUTER JOIN
Supported only as the first join in the statement, and is internally rewritten to aLEFT OUTER JOIN with the tables swapped. All orders are returned; customers with no matching order have MISSING name fields.
DQL
Post-join WHERE filter
WHERE predicates are evaluated after the join. Predicates that reference only one collection are pushed down into the join condition where possible.
DQL
Multi-expression ON clause
TheON clause may contain compound expressions. Conditions that can be evaluated against the outer row alone are filtered early; the remainder form the join condition.
DQL
Wildcard projection across joined collections
Use* to include all collections, each under its alias:
DQL
DQL
Multi-collection chains
You may chain any number of join terms (See Directives). Each subsequent join may reference all previously introduced aliases in itsON clause.
DQL
DQL
Joining on document ID
When theON condition equates a field from the outer collection to _id of the inner collection, the planner uses an efficient idScan rather than a regular index scan β no additional index is required on the inner collection.
DQL
Directives
All query directives are available when using joins. A number are particularly relevant.Limiting the number of join terms
The#max_joins global directive limits how many join terms a statement may contain. The default is 10. Set it to 0 to prohibit joins entirely in a given query.
DQL
Controlling access paths per collection
Use theUSE INDEX or USE DIRECTIVES sub-clause on any collection term to influence the access path for that specific leg of the join.
DQL
The
USE INDEX and USE DIRECTIVES sub-clauses on join collections are collection-level directives and apply to that collection instance only. They follow the same precedence rules as described in Query Directives.Overriding the index requirement
You can explicitly permit a collection scan on an inner join leg by supplying an empty string toUSE INDEX. This overrides the default requirement for indexed access and should only be used when the inner collection is small or when you have a specific reason to avoid an index access path.
DQL
USE INDEX "" on only one, the other two must still have index access paths.
Prefer-order with joins
The#prefer_order directive applies to the driving collection. It is rarely necessary for joins β the join order is fixed by the statement β but can influence index selection on the outer leg when ORDER BY is present.
Understanding query plans with EXPLAIN
Prefix any joinSELECT with EXPLAIN to inspect the plan without executing the query. The plan reveals which access path the planner chose for each leg of the join β index scan, covering index scan, intersect scan, or ID scan β and confirms whether LEFT OUTER semantics are in effect.
See EXPLAIN β Join plans for a full description of the nlJoin operator, dynamic span bounds, covering and intersect scans, and how LEFT OUTER, RIGHT OUTER, and multi-collection joins appear in the plan.
Best practices
- Index every join key on the inner collection. Use
ADVISEto confirm which indexes are needed. - Use composite covering indexes that include both the join key and any projected or filtered fields from the inner collection. This eliminates the
fetchstep and reduces document reads. - Order join terms thoughtfully. Place the most selective (smallest) collection first as the outer (driving) leg to reduce the number of inner-leg probes.
- Qualify all field references with aliases. Unqualified field names in multi-collection queries can be ambiguous.
- Use
EXPLAINduring development to confirm the planner has chosen index scans rather than collection scans for inner legs. - Use
ADVISEbefore deploying new join queries to ensure optimal indexes are in place.
Restrictions
- Not supported in Big Peer environments. Joins are a Ditto SDK (small peer) feature only.
- Each inner collection must have an indexed access path β either an index scan, a covering index scan, an intersect/union scan, or an ID scan. A collection scan on an inner leg is rejected unless explicitly permitted via
USE INDEX "". RIGHT OUTER JOINis only supported as the first join term. Subsequent join terms must useINNERorLEFT OUTER. The right join is rewritten internally; the rewrite is transparent to the result.- The join count limit defaults to 10 join terms per statement. Adjust with
/*+ {"#max_joins": N} */. - Using joins in
registerSubscriptionwill result in an unsupported query error. Joins are supported inexecuteandregisterObserver.