({{, (RefLink[Query, paclet:ref/Query])[(Subscript[operator, 1]),(Subscript[operator, 2]),…]
represents a query that can be applied to a (RefLink[Dataset, paclet:ref/Dataset]) object, in which the successive (Subscript[operator, i]) are applied at successively deeper levels. }})

OpenersNotesSection(Spacer1)Details and Options

(RefLink[Query, paclet:ref/Query]) can perform complex filtering, reorganization, and aggregation of arbitrary rectangular and hierarchical data, typically returning further rectangular or hierarchical data.

(RefLink[Query, paclet:ref/Query]) can operate on a (RefLink[Dataset, paclet:ref/Dataset]) object or an arbitrary nested expression consisting of lists and associations.

(RefLink[Query, paclet:ref/Query]) can be seen as a generalization of (RefLink[Part, paclet:ref/Part]) that allows computations to be performed in the process of traversing an expression or dataset.

The (Subscript[operator, i]) can be any of the following forms:

In (RefLink[Query, paclet:ref/Query])[(Subscript[operator, 1]),…][expr], the (Subscript[operator, i]) are applied at successively deeper levels in expr, but any given one may be applied either while "descending" into expr or while "ascending" out of it. In general, part specifications and filtering operators are "descending" operators. Aggregation operators, subquery operators, and arbitrary functions are "ascending" operators.

A "descending" operator is applied to corresponding parts of the original dataset, before subsequent operators are applied at deeper levels. Descending operators have the feature that they do not change the structure of deeper levels of the data when applied at a certain level. This ensures that subsequent operators will encounter subexpressions whose structure is identical to the corresponding levels of the original dataset. The simplest descending operator is (RefLink[All, paclet:ref/All]), which selects all parts at a given level and therefore leaves the structure of the data at that level unchanged.

An "ascending" operator is applied after all subsequent operators have been applied to deeper levels. Whereas descending operators correspond to the levels of the original data, ascending operators correspond to the levels of the result. Unlike descending operators, ascending operators do not necessarily preserve the structure of the data on which they operate. Unless an operator is specifically recognized to be descending, it is assumed to be ascending.

The "descending" part operators specify which elements to take at a level before applying any subsequent operators to deeper levels:

The "descending" filtering operators specify how to rearrange or filter elements at a level before applying subsequent operators to deeper levels:

The "ascending" aggregation operators combine or summarize the results of applying subsequent operators to deeper levels:

The "ascending" subquery operators perform a subquery after applying subsequent operators to deeper levels:

When one or more descending operators are composed with one or more ascending operators (e.g. desc/*asc), the descending part will be applied, then subsequent operators will be applied to deeper levels, and lastly the ascending part will be applied to the result.

The special descending operator (RefLink[GroupBy, paclet:ref/GroupBy])[spec] will introduce a new association at the level at which it appears and can be inserted or removed from an existing query without affecting the behavior of other operators.

The syntax (RefLink[GroupBy, paclet:ref/GroupBy])["string"] can be used as a synonym for (RefLink[GroupBy, paclet:ref/GroupBy])[(RefLink[Key, paclet:ref/Key])["string"]]. The same syntax is also available for (RefLink[SortBy, paclet:ref/SortBy]), (RefLink[CountsBy, paclet:ref/CountsBy]), (RefLink[MaximalBy, paclet:ref/MaximalBy]), (RefLink[MinimalBy, paclet:ref/MinimalBy]), and (RefLink[DeleteDuplicatesBy, paclet:ref/DeleteDuplicatesBy]).

The following options can be given:

Possible values for (RefLink[FailureAction, paclet:ref/FailureAction]) include:

The option (RefLink[MissingBehavior, paclet:ref/MissingBehavior]) describes how numeric and other functions should treat expressions with head (RefLink[Missing, paclet:ref/Missing]). Possible values include:

The option (RefLink[PartBehavior, paclet:ref/PartBehavior]) describes how operators that refer to nonexistent parts are evaluated. Possible values include:

OpenersPrimaryExamplesSection(Spacer1)Examples  (6)

OpenersExampleSection0(Spacer1)Basic Examples  (1)

Construct tabular data on which to perform queries:

data={
<|"a"->1,"b"->"x","c"->{1}|>,
<|"a"->2,"b"->"y","c"->{2,3}|>,
<|"a"->3,"b"->"z","c"->{3}|>,
<|"a"->4,"b"->"x","c"->{4,5}|>,
<|"a"->5,"b"->"y","c"->{5,6,7}|>,
<|"a"->6,"b"->"z","c"->{}|>};

Take a set of rows:

Query[1;;3] @ data

Take a specific row:

Query[2] @ data

Take a specific element from a specific row:

Query[3,"a"] @ data

Query[4,"b"] @ data

Query[5,"c"] @ data

Take the contents of a specific column:

Query[All,"a"] @ data

Query[All,"b"] @ data

Query[All,"c"] @ data

Take a specific part within a column:

Query[All,"c",1] @ data

Take a subset of the rows and columns:

Query[1;;4,{"a","b"}] @ data

Apply a function to a specific column:

Query[Total,"a"] @ data

Query[StringJoin,"b"] @ data

Query[Catenate,"c"] @ data

Partition the data based on a column, applying the rest of the query to each group:

Query[GroupBy["b"],Catenate,"c"] @ data

Apply a function to each row:

Query[All,Reverse] @ data

Apply a function both to each row and to the entire result:

Query[Reverse, Reverse] @ data

Apply a function f to every element in every row:

Query[All,All,f] @ data

Apply specific operators to each column independently:

Query[All,{"a"->f,"b"->g,"c"->h}] @ data

Construct a new table by specifying operators that will compute each column:

Query[All,"c"/*<|"ctotal"->Total,"clength"->Length|>] @ data

Use the same technique to rename columns:

Query[All,<|"A"->"a","B"->"b","C"->"c"|>] @ data

Select specific rows based on a criterion:

Query[Select[#a<5&]] @ data

Take the contents of a column after selecting the rows:

Query[Select[#a<5&],"b"] @ data

Take a subset of the available columns after selecting the rows:

Query[Select[#a<5&],{"b","c"}] @ data

Take a value from the first row satisfying a criterion:

Query[SelectFirst[#a>5&],"b"] @ data

Sort the rows by a criterion:

Query[SortBy[Length[#c]&]] @ data

Take the rows that give the maximal value of a scoring function:

Query[MaximalBy[Length[#c]&]] @ data

Delete rows that duplicate a criterion:

Query[DeleteDuplicatesBy["b"]] @ data

Compose an ascending and a descending operator to aggregate values of a column after filtering the rows:

Query[Select[#b=="z"&]/*Total,"a"]  @ data

Do the same thing by composing (RefLink[Total, paclet:ref/Total]) with the (RefLink[Query, paclet:ref/Query]):

(Query[Select[#b=="z"&],"a"] /*Total) @ data

OpenersExampleSection1(Spacer1)Options  (4)

OpenersExampleSubsection0(Spacer1)FailureAction  (2)

The setting (RefLink[FailureAction, paclet:ref/FailureAction])->(RefLink[None, paclet:ref/None]) allows failed operations to remain symbolic:

Query[All,Total,FailureAction->None] @ {{1,2},{3,4},x}

The setting (RefLink[FailureAction, paclet:ref/FailureAction])->"Encapsulate" will encapsulate all failed results in a (RefLink[Failure, paclet:ref/Failure]) object:

Query[All,Total,FailureAction->"Encapsulate"] @ {{1,2},{3,4},x}

The setting (RefLink[FailureAction, paclet:ref/FailureAction])->"Replace" replaces failed results with a placeholder. For the function (RefLink[Query, paclet:ref/Query]), this placeholder is a (RefLink[Missing, paclet:ref/Missing]) object:

Query[All,Total,FailureAction->"Replace"] @{{1,2},{3,4},x}

The setting (RefLink[FailureAction, paclet:ref/FailureAction])->"Drop" causes failed results to be dropped from the final expression:

Query[All,Total,FailureAction->"Drop"] @{{1,2},{3,4},x}

The setting (RefLink[FailureAction, paclet:ref/FailureAction])->"Abort" aborts the computation, returning a (RefLink[Failure, paclet:ref/Failure]) object instead:

Query[All,Total,FailureAction->"Abort"] @{{1,2},{3,4},x}

The setting (RefLink[FailureAction, paclet:ref/FailureAction])->{"Drop",f} can be used to perform an action before dropping a failed result:

Query[All,Total,FailureAction->{"Drop",Print}] @{{1,2},{3,4},x}

The setting (RefLink[FailureAction, paclet:ref/FailureAction])->{"Replace",f} can be used to specify a replacement that is a function of the failure:

Query[All,Total,FailureAction->{"Replace",ToString}] @{{1,2},{3,4},x}

OpenersExampleSubsection1(Spacer1)MissingBehavior  (1)

The default option value (RefLink[MissingBehavior, paclet:ref/MissingBehavior])->(RefLink[Automatic, paclet:ref/Automatic]) applies special rules to operators that encounter (RefLink[Missing, paclet:ref/Missing]):

Query[Total] @ {1,2,3,Missing[]}

Use (RefLink[MissingBehavior, paclet:ref/MissingBehavior])->(RefLink[None, paclet:ref/None]) to specify the ordinary behavior of (RefLink[Missing, paclet:ref/Missing]) for all operators:

Query[Total,MissingBehavior->None] @ {1,2,3,Missing[]}

OpenersExampleSubsection2(Spacer1)PartBehavior  (1)

The default option value (RefLink[PartBehavior, paclet:ref/PartBehavior])->(RefLink[Automatic, paclet:ref/Automatic]) invokes special behavior for operators that would otherwise fail:

Query[1;;5] @ {1,2,3}

Using (RefLink[PartBehavior, paclet:ref/PartBehavior])->(RefLink[None, paclet:ref/None]) specifies that the ordinary behavior of (RefLink[Part, paclet:ref/Part]) should be used:

Query[1;;5,PartBehavior->None] @ {1,2,3}

OpenersExampleSection2(Spacer1)Properties & Relations  (1)

(RefLink[Query, paclet:ref/Query]) is the operator form of the query language supported by (RefLink[Dataset, paclet:ref/Dataset]):

Query["b",Total] @ <|"a"->{1,2},"b"->{3,4}|>

Dataset[<|"a"->{1,2},"b"->{3,4}|>]["b", Total]

Before being applied, (RefLink[Query, paclet:ref/Query]) expressions are "compiled" into ordinary compositions of ordinary Wolfram Language functions and their operator forms. To see the compiled form of a (RefLink[Query, paclet:ref/Query]), use (RefLink[Normal, paclet:ref/Normal]):

Query[All,f]//Normal

Query[f,g]//Normal

Query[GroupBy["a"],Total]//Normal

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Dataset, paclet:ref/Dataset])  ▪  (RefLink[Part, paclet:ref/Part])  ▪  (RefLink[FailureAction, paclet:ref/FailureAction])  ▪  (RefLink[MissingBehavior, paclet:ref/MissingBehavior])  ▪  (RefLink[PartBehavior, paclet:ref/PartBehavior])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Computation with Structured Datasets, paclet:guide/ComputationWithStructuredDatasets])}})}})