query

A query is the primary data retrieval and transformation construct in CQL. It includes a source clause, optional let clause, optional inclusion clauses (with/without), optional where clause, optional aggregate or return clause, and an optional sort clause.

Examples

Simple filtered query

from [Condition] C
  where C.clinicalStatus ~ "Active"

Multi-source query with with-clause join and return projection

from [MedicationRequest] M, [Condition] C
  with [Patient] P such that P.id = M.subject.id
  where M.status = 'active'
  return { medication: M.medication, condition: C.code }

Query with let-binding, sort, and distinct return

from [Observation] O
  let value: O.value as FHIR.Quantity
  where value.value > 100
  return distinct O.code
  sort by O.effective desc
Railroad Diagram
100%
sourceClause letClause queryInclusionClause whereClause aggregateClause returnClause sortClause

scroll to zoom · drag to pan · click green rules or blue tokens to navigate

EBNF

query
  : sourceClause letClause? queryInclusionClause* whereClause?
    (aggregateClause | returnClause)? sortClause?
  ;

Used In