Queries are the output of a QL program. They evaluate to sets of results.
There are two kinds of queries. For a given query module, the queries in that module are:The select clause, if any, defined in that module.
Any query predicates in that module’s predicate namespace. That is, they can be defined in the module itself, or imported from a different module.
We often also refer to the whole QL program as a query.
When writing a query module, you can include a select clause (usually at the end of the file) of the following form:
The from and where parts are optional.
Apart from the expressions described in “Expressions,” you can also include:The as keyword, followed by a name. This gives a “label” to a column of results, and allows you to use them in subsequent select expressions.
The order by keywords, followed by the name of a result column, and optionally the keyword asc or desc. This determines the order in which to display the results.
For example:
This select clause returns the following results:
3 |
0 |
0 |
product: 0 |
3 |
1 |
3 |
product: 3 |
3 |
2 |
6 |
product: 6 |
You could also add order by y desc at the end of the select clause. Now the results are ordered according to the values in the y column, in descending order:
3 |
2 |
6 |
product: 6 |
3 |
1 |
3 |
product: 3 |
3 |
0 |
0 |
product: 0 |
A query predicate is a non-member predicate with a query annotation. It returns all the tuples that the predicate evaluates to.
For example:
This predicate returns the following results:
3 |
0 |
0 |
3 |
1 |
3 |
3 |
2 |
6 |
A benefit of writing a query predicate instead of a select clause is that you can call the predicate in other parts of the code too. For example, you can call getProduct inside the body of a class:
In contrast, the select clause is like an anonymous predicate, so you can’t call it later.
It can also be helpful to add a query annotation to a predicate while you debug code. That way you can explicitly see the set of tuples that the predicate evaluates to.