Subqueries
A typical case you may require is running a Query
which depends on another Query
result.
Start by importing:
import scalaql._
// Docs classes
import scalaql.docs.Hogwarts._
Assume you'd like to find faculties that have adult students.
This could be done via exists
method:
def hasAdults(faculty: Faculty) =
select[Student]
.where(_.faculty == faculty.name)
.exists(_.age >= 18)
You could apply this function (which uses a Query
) on another Query
via whereSubQuery
:
val subSelectQuery =
select[Faculty]
.whereSubQuery(hasAdults)
// subSelectQuery: Query[From[Faculty] with From[Student], Faculty] = FROM(Hogwarts::Faculty) -> WHERE SUBQUERY
Then run it:
subSelectQuery
.show(truncate = false)
.run(
from(students) & from(faculties)
)
// +----------+-----------------+---------------------+
// |name |founder |description |
// +----------+-----------------+---------------------+
// |Gryffindor|Godric Gryffindor|100 points by default|
// |Slytherin |Salazar Slytherin|The bad guys |
// +----------+-----------------+---------------------+
//