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
eq
Checks if an expression is equal to some value.
neq
Checks if an expression is not equal to some value.
isNull()
Returns
true
if this expression is null,false
otherwise.isNotNull()
Returns
false
if this expression is null,true
otherwise.less
Checks if an expression is less than some value.
lessEq
Checks if an expression is less than or equal to some value.
greater
Checks if an expression is greater than some value.
greaterEq
Checks if an expression is greater than or equal to some value.
exists
Checks if a subquery returns at least one row.
notExists
Checks if a subquery does not return any row.
isDistinctFrom
Checks if this expression is not equal to another value, with null treated as a comparable value.
isNotDistinctFrom
Checks if an expression is equal to another value, with null treated as a comparable value.
Logical conditions
not
Returns the inverse of an expression.
and
Returns the result of performing a logical
and
operation between two expressions.or
Returns the result of performing a logical
or
operation between two expressions.andIfNotNull
Returns the result of performing a logical
and
operation between two expressions if the second one is not null.orIfNotNull
Returns the result of performing a logical
or
operation between two expressions if the second is not null.compoundAnd()
Reduces a list to a single expression by performing an
and
operation between all the expressions in the list.compoundOr()
Reduces a list to a single expression by performing an
or
operation between all the expressions in the list.
Check for a match in a pattern
like
Checks if an expression matches the specified pattern.
StarWarsFilms.selectAll().where { StarWarsFilms.name like "The %" }notLike
Checks if an expression doesn't match the specified pattern.
regexp
Checks if an expression matches a regular expression.
StarWarsFilms.selectAll().where { StarWarsFilms.name regexp "^The(\\s\\w+){2}\$" }match
Checks 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
true
if 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
inList
Checks if an expression is equal to any element from list.
StarWarsFilms.selectAll().where { StarWarsFilms.sequelId inList listOf(6, 4) }inList
also accepts multiple expressions to check for equality, either as aPair
or aTriple
:val topRated = listOf(5 to "Empire Strikes Back", 4 to "A New Hope") StarWarsFilms.selectAll().where { StarWarsFilms.sequelId to StarWarsFilms.name inList topRated }notInList
Checks 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: