Querying data
Working with where expressions
The query expression where expects a boolean operator (Op<Boolean>). We've split the allowed conditions in different categories:
Basic conditions
eqChecks if an expression is equal to some value.
neqChecks if an expression is not equal to some value.
isNull()Returns
trueif this expression is null,falseotherwise.isNotNull()Returns
falseif this expression is null,trueotherwise.lessChecks if an expression is less than some value.
lessEqChecks if an expression is less than or equal to some value.
greaterChecks if an expression is greater than some value.
greaterEqChecks if an expression is greater than or equal to some value.
existsChecks if a subquery returns at least one row.
notExistsChecks if a subquery does not return any row.
isDistinctFromChecks if this expression is not equal to another value, with null treated as a comparable value.
isNotDistinctFromChecks if an expression is equal to another value, with null treated as a comparable value.
Logical conditions
notReturns the inverse of an expression.
andReturns the result of performing a logical
andoperation between two expressions.orReturns the result of performing a logical
oroperation between two expressions.andIfNotNullReturns the result of performing a logical
andoperation between two expressions if the second one is not null.orIfNotNullReturns the result of performing a logical
oroperation between two expressions if the second is not null.compoundAnd()Reduces a list to a single expression by performing an
andoperation between all the expressions in the list.compoundOr()Reduces a list to a single expression by performing an
oroperation between all the expressions in the list.
Check for a match in a pattern
likeChecks if an expression matches the specified pattern.
StarWarsFilms.selectAll().where { StarWarsFilms.name like "The %" }notLikeChecks if an expression doesn't match the specified pattern.
regexpChecks if an expression matches a regular expression.
StarWarsFilms.selectAll().where { StarWarsFilms.name regexp "^The(\\s\\w+){2}\$" }matchChecks whether an expression matches a given pattern based on a specific mode.
Supported only on MySQL and MariaDB.
Check for a match in a range
between(from: T, to: T)Checks if an expression is between the values from and to. Returns
trueif the expression is between the lower and upper range values (inclusive). Date and time values are also supported as arguments.StarWarsFilms.selectAll().where { StarWarsFilms.sequelId.between(4, 6) }
Check for a match in a collection
inListChecks if an expression is equal to any element from list.
StarWarsFilms.selectAll().where { StarWarsFilms.sequelId inList listOf(6, 4) }inListalso accepts multiple expressions to check for equality, either as aPairor aTriple:val topRated = listOf(5 to "Empire Strikes Back", 4 to "A New Hope") StarWarsFilms.selectAll().where { StarWarsFilms.sequelId to StarWarsFilms.name inList topRated }notInListChecks if an expression is not equal to any element in the provided collection.
In addition to the IN operator, the ANY and ALL operators are available with any preceding comparison operator:
anyFrom() and allFrom() also accept subqueries, tables, and array expressions as arguments.
Conditional where
It is a rather common case to have a query with a where clause that depends on some other code's conditions. Moreover, independent or nested conditions could make it more complicated to prepare such where clauses.
Let's imagine that we have a form on a website where a user can optionally filter "Star Wars" films by a director and/or a sequel.
But what if we want to conditionally select from another table and join it only when a condition is true? You have to use adjustColumnSet and adjustSelect functions, which allow to extend and modify join and select parts of a query (see kdoc on that functions):
Aggregating and sorting data
Count
count() is a method of Query that is used like in the example below:
Order by
orderBy() accepts a list of columns mapped to boolean indicates if sorting should be ascending or descending. Example:
Group by
In groupBy, define fields and their functions by the select() method.
Available field functions are: count, sum, average, min and max.
Limiting result sets
You can use the limit function to prevent loading large data sets or to accomplish pagination by using the offset function.
Joining tables
Join
For the join examples below, consider the following tables:
Join to count how many actors star in each movie:
Instead of specifying onColumn and otherColumn, additionalConstraint can be used (and allows specifying other types of join conditions).
When joining on a foreign key, the more concise innerJoin can be used:
This is equivalent to the following:
Union
You can combine the results of multiple queries using .union(...). Per the SQL specification, the queries must have the same number of columns, and not be marked for update. Subqueries may be combined when supported by the database.
Only unique rows are returned by default. Duplicates may be returned using .unionAll().
Mapping fields with alias
Aliases allow preventing ambiguity between field names and table names. Use the aliased var instead of original one:
Also, aliases allow you to use the same table in a join multiple times:
And they can be used when selecting from sub-queries:
Custom Select Queries
A Query instance, which can be instantiated by calling selectAll() or select() on a Table or Join, has many extension functions for building complex queries. Some of these have already been mentioned above, like where(), groupBy(), and orderBy().
If a SELECT query with a special clause is required, a custom extension function can be implemented to enable its use with other standard queries.
For example, MySQL index hints, which follow the table name in SQL, can be implemented on a SELECT query by using the following custom function and class: