select ~where ~join (from t1 t2 ... tm)
selects attributes t1
, t2
, ..., tm
, join them by the fields specified in the join
clause, and filters those that satisfy the condition defined with the where
clause.
Examples:
Select all students that has the GPA rate greater than 3.8.
select
~where:(student.(gpa) > float 3.8)
(from students)
Select all students and their corresponding teachers, that have a GPA greater than 3.8 (assuming that teacher is a foreign key to the table of teachers).
select
~where:(student.(gpa) > float 3.8)
~join:[[field teacher ~from:student; field id ~from:teacher]]
(from students)
You may notice, that the select
query lacks the SQL's WHAT
clause, i.e., it is not possible or needed to specify columns. The reason for this, is that the query used as a value that is passed to some command constructor, (e.g.,foreach
), that can work with fields individually, e.g., the following is a complete correspondence of the SQL's:
SELECT name FROM students WHERE gpa > 3.5
foreach Query.(select
~where:(student.(gpa) > float 3.8)
(from students))
~f:(fun s -> return (Student.name s))
It is nearly three times as long, but in return it is type-safe, and composable.