For AI agents: the documentation index is at /docs/aidbox/llms.txt. A Markdown version of this page is available at /docs/aidbox/notebooks.md or by requesting it with the Accept: text/markdown header.
Aidbox Docs
All notebooks

Getting Started: SQL on FHIR

This notebook explains how to flatten FHIR resources and run analytical queries using SQL

24 cells · updated May 27, 2026

This is a read-only view. Responses shown were saved when the notebook was published — open it in your own Aidbox to run the cells against live data.

SQL on FHIR provides a convenient way to query and manipulate FHIR data using SQL. In this notebook, we'll explore the basics of SQL on FHIR and how it can be used.

Let's import some synthetic FHIR data to Aidbox

REST Send Cells run only inside Aidbox. Use Run in Aidbox above to open this notebook in your own instance.
POST /fhir/$load
Accept: text/yaml
Content-Type: text/yaml

source: 'https://storage.googleapis.com/aidbox-public/synthea/100/all.ndjson.gz'
Response: Body
Status: 200
format: fhir
resources: {CarePlan: 356, Observation: 20382, MedicationAdministration: 150, Goal: 301, Patient: 124, DiagnosticReport: 1430, Practitioner: 181, ExplanationOfBenefit: 3460, Immunization: 1636, Claim: 4488, MedicationRequest: 1028, Encounter: 3460, Condition: 871, Procedure: 2854, Organization: 181, AllergyIntolerance: 40, ImagingStudy: 134}

The most important part of the SQL on FHIR standard is the ViewDefinition resource. View Definitions allow users to define flattened views of FHIR data that are portable between systems.

name is the name of the column in the resulting table.
expr is the FhirPath expression defining the data for this column.
forEach expression convert arrays into a set of rows.

Let's create a view using a ViewDefinition resource

REST Send Cells run only inside Aidbox. Use Run in Aidbox above to open this notebook in your own instance.
PUT /fhir/ViewDefinition/patient_demo_view
Content-Type: text/yaml
Accept: text/yaml

name: patient_demo_view
status: active
resource: Patient
url: http://example.org/viewdefinition/patient_demo_view
select:
  - column:
      - name: patient_id
        path: id
      - name: gender
        path: gender
      - name: dob
        path: birthDate
      - name: patient_ssn
        path: "identifier.where(system = 'http://hl7.org/fhir/sid/us-ssn').value"
    select:
      - forEach: name
        column:
          - name: given
            path: "given.join(' ')"
          - name: family
            path: family
Response: Body
Status: 200
name: patient_demo_view
status: active
resource: Patient
url: http://example.org/viewdefinition/patient_demo_view
select:
- column:
  - {name: patient_id, path: id}
  - {name: gender, path: gender}
  - {name: dob, path: birthDate}
  - {name: patient_ssn, path: 'identifier.where(system = ''http://hl7.org/fhir/sid/us-ssn'').value'}
  select:
  - forEach: name
    column:
    - {name: given, path: given.join(' ')}
    - {name: family, path: family}
id: patient_demo_view
resourceType: ViewDefinition

The View has been created.

View-definition Send Cells run only inside Aidbox. Use Run in Aidbox above to open this notebook in your own instance.
http://example.org/viewdefinition/patient_demo_view

Let's materialize the view to make it accessible through SQL.

REST Send Cells run only inside Aidbox. Use Run in Aidbox above to open this notebook in your own instance.
POST /fhir/ViewDefinition/patient_demo_view/$materialize
Content-Type: application/json
Accept: application/json

{
  "resourceType": "Parameters",
  "parameter": [
    {
      "name": "type",
      "valueCode": "view"
    }
  ]
}
Response: Body
Status: 201
{
  "resourceType": "Parameters",
  "parameter": [
    {
      "name": "viewName",
      "valueString": "sof.patient_demo_view"
    },
    {
      "name": "viewType",
      "valueString": "view"
    },
    {
      "name": "viewSchema",
      "valueString": "sof"
    }
  ]
}
SQL Send Cells run only inside Aidbox. Use Run in Aidbox above to open this notebook in your own instance.
select * from sof.patient_demo_view
limit 10
Result
dobgivenfamilygenderpatient_idpatient_ssn
2011-12-12Kristina583Tromp100female179adc51-6875-4191-ad16-160339ed45cc999-96-1646
2015-01-08Irvin970Gaylord332male28572f59-c778-4d3e-91ad-c5b2488ebc1d999-72-5260
1945-07-09Jan231Oberbrunner298male9f1ee7e4-ec67-487e-a16f-0ceeccbe4409999-67-6627
2000-11-10Sunday568MacGyver246female9fce4714-ed2b-4d98-be2e-f29e29677240999-55-7933
2017-11-04Glenna97Hills818femaled9b90399-0504-4f1e-bf52-2f45354994dd999-76-9134
1978-05-31Frank378Hermiston71male3eb31aa6-4be8-4e2b-8ee6-42d6f044b953999-10-3640
2019-07-16Felisa186Pfeffer420femalee9adac47-eb98-4fce-b871-512226086c97999-81-2584
1984-03-15Rodolfo763Thompson596male4747365a-a6c0-4240-8d24-5b58d94f38cd999-95-9162
1987-06-06Lenny728Huel628malea8cf2e08-8f8d-4800-88f3-aa1d1c1a8cec999-10-5654
1970-08-31Hilton264Fadel536malec860b52d-fdf5-4174-87ac-71cf68d97e87999-58-4982

Let's create a more complex view, which extracts condition data and patient_id foreign key.

REST Send Cells run only inside Aidbox. Use Run in Aidbox above to open this notebook in your own instance.
PUT /fhir/ViewDefinition/condition_demo_view
Content-Type: text/yaml
Accept: text/yaml

name: condition_demo_view
url: http://example.org/viewdefinition/condition_demo_view
resource: Condition
status: active
select:
- column:
  - name: condition_id
    path: id
  - name: onset
    path: onset.dateTime
  - name: abatement
    path: abatement.dateTime
  - name: status
    path: clinicalStatus.coding.code.first()
  - name: code
    path: code.coding.where(system='http://snomed.info/sct').code.first()
  - name: display
    path: code.text
  - name: patient_id
    path: subject.id
Response: Body
Status: 201
name: condition_demo_view
url: http://example.org/viewdefinition/condition_demo_view
resource: Condition
status: active
select:
- column:
  - {name: condition_id, path: id}
  - {name: onset, path: onset.dateTime}
  - {name: abatement, path: abatement.dateTime}
  - {name: status, path: clinicalStatus.coding.code.first()}
  - {name: code, path: 'code.coding.where(system=''http://snomed.info/sct'').code.first()'}
  - {name: display, path: code.text}
  - {name: patient_id, path: subject.id}
id: condition_demo_view
resourceType: ViewDefinition

Just like with the Patient view, we can utilize the Condition view in SQL queries, but we need to materialize it first

REST Send Cells run only inside Aidbox. Use Run in Aidbox above to open this notebook in your own instance.
POST /fhir/ViewDefinition/condition_demo_view/$materialize
Content-Type: application/json
Accept: application/json

{
  "resourceType": "Parameters",
  "parameter": [
    {
      "name": "type",
      "valueCode": "view"
    }
  ]
}
Response: Body
Status: 201
{
  "resourceType": "Parameters",
  "parameter": [
    {
      "name": "viewName",
      "valueString": "sof.condition_demo_view"
    },
    {
      "name": "viewType",
      "valueString": "view"
    },
    {
      "name": "viewSchema",
      "valueString": "sof"
    }
  ]
}
SQL Send Cells run only inside Aidbox. Use Run in Aidbox above to open this notebook in your own instance.
select * from sof.condition_demo_view
limit 10;
Result
codeonsetstatusdisplaypatient_idcondition_idabatement
2306900072018-10-06T15:48:09+03:00activeStroke6bcca3ba-8470-4f6c-805d-291586192a25622e58a4-9eba-4e1c-947d-9c0fe94376ed
4241320001999-12-10T23:48:18+03:00activeNon-small cell carcinoma of lung, TNM stage 1 (disorder)b262c1ba-d4da-4791-877c-c69a128f0c2b00633c33-a296-4eaa-a169-3b4b1e31f7a5
888050092014-06-26T12:50:13+04:00activeChronic congestive heart failure (disorder)38ad2730-be38-4e9a-97a0-2b97c857481400bb715a-d112-4843-befd-837fc5445e27
1628640051987-03-16T12:35:17+03:00activeBody mass index 30+ - obesity (finding)83991f61-aff5-4ae6-8559-3ef100c931a70119106a-365f-49a2-8bf5-b5ce8df6f2f3
4448140092016-08-06T02:13:33+03:00resolvedViral sinusitis (disorder)5e23837b-8ef5-46cd-9046-82cd4033bdb3011b6e34-80c1-473e-9c0e-38ac003a404f2016-08-13T02:13:33+03:00
1956620092016-05-27T13:44:17+03:00resolvedAcute viral pharyngitis (disorder)93f09189-7ec2-4d0e-9310-f14efce8e4b6014774ea-8dac-46c0-a9b7-1554613d86252016-06-03T13:44:17+03:00
1956620092003-02-14T18:25:00+03:00resolvedAcute viral pharyngitis (disorder)44d86263-d939-4dff-8ce7-a5a86d70c34802116b05-4f06-4eb8-bc02-63380e64ebc92003-02-24T18:25:00+03:00
105090022019-03-30T08:53:34+03:00resolvedAcute bronchitis (disorder)41907da4-2926-4f7a-9326-bb18585b07bf0287a9bc-ae77-4763-9282-39d4799e2b852019-04-06T08:53:34+03:00
438780082013-07-04T14:17:52+04:00resolvedStreptococcal sore throat (disorder)5bad6369-e2f1-41d3-aae2-daf1d603cb5002a79009-e719-43b5-9aad-ae68243066c82013-07-16T14:17:52+04:00
1956620092016-10-06T05:24:13+03:00resolvedAcute viral pharyngitis (disorder)8742d4ba-19ee-4f45-a8c1-3a53bd01cf0102bfc9af-dbcf-44d0-852d-32914be494582016-10-14T05:24:13+03:00

Let's now identify patients who have recovered from Acute Viral Pharyngitis within the past 20 years.

SQL Send Cells run only inside Aidbox. Use Run in Aidbox above to open this notebook in your own instance.
SELECT *
FROM sof.patient_demo_view pt
JOIN sof.condition_demo_view cond using (patient_id)
WHERE code = '195662009' and max_text_date_bound(cond.abatement) > (NOW() - interval '20 years')
Result
patient_idonsetgivencondition_idfamilyabatementpatient_ssndobstatuscodedisplaygender
93f09189-7ec2-4d0e-9310-f14efce8e4b62016-05-27T13:44:17+03:00Mariann762014774ea-8dac-46c0-a9b7-1554613d8625White1932016-06-03T13:44:17+03:00999-71-16731988-02-27resolved195662009Acute viral pharyngitis (disorder)female
93f09189-7ec2-4d0e-9310-f14efce8e4b62016-05-27T13:44:17+03:00Mariann762014774ea-8dac-46c0-a9b7-1554613d8625Ankunding2772016-06-03T13:44:17+03:00999-71-16731988-02-27resolved195662009Acute viral pharyngitis (disorder)female
8742d4ba-19ee-4f45-a8c1-3a53bd01cf012016-10-06T05:24:13+03:00Wallace64702bfc9af-dbcf-44d0-852d-32914be49458Mayert7102016-10-14T05:24:13+03:00999-39-72731971-05-29resolved195662009Acute viral pharyngitis (disorder)male
7431e052-639d-41bd-b20d-7883640f1a5a2013-08-07T23:29:55+04:00Vella93004931984-f7e8-426a-bcb3-242fbaab6a32O'Keefe542013-08-15T23:29:55+04:00999-30-98751990-11-01resolved195662009Acute viral pharyngitis (disorder)female
7431e052-639d-41bd-b20d-7883640f1a5a2013-08-07T23:29:55+04:00Vella93004931984-f7e8-426a-bcb3-242fbaab6a32VonRueden3762013-08-15T23:29:55+04:00999-30-98751990-11-01resolved195662009Acute viral pharyngitis (disorder)female
ba8ca47f-385f-4b20-acdb-57d6d00783e32011-09-17T04:52:22+04:00Federico5890735a939-a828-428e-84f5-4d9b3d36d497Satterfield3052011-09-27T04:52:22+04:00999-93-49281955-08-27resolved195662009Acute viral pharyngitis (disorder)male
102e001e-df1b-4fa8-adfe-6e2acb69aae82009-12-20T14:14:25+03:00Hollis70827f77d-34c6-4444-b593-667c23e2e560Ortiz1862009-12-31T14:14:25+03:00999-56-26801944-01-08resolved195662009Acute viral pharyngitis (disorder)male
14025d1b-3e46-4cbb-9ef2-f913cb6be1392015-07-31T17:13:15+03:00Kassandra2560aba7690-5016-4958-8002-f4868dd0734eMorar5932015-08-10T17:13:15+03:00999-72-37031995-12-16resolved195662009Acute viral pharyngitis (disorder)female
0ec5c51b-56d4-433e-a320-297faf0612372011-09-02T10:15:45+04:00Somer380eb5d1d7-8735-4f9d-85b1-59bd253d3be4Nicolas7692011-09-14T10:15:45+04:00999-24-86591936-10-27resolved195662009Acute viral pharyngitis (disorder)female
5b5b7a5b-c586-4694-88e1-afa97116f7772014-09-17T19:36:52+04:00Shameka870122a0e5f-a978-4672-84a9-990dfa9257e4Hettinger5942014-09-30T19:36:52+04:00999-45-86171974-03-21resolved195662009Acute viral pharyngitis (disorder)female
5b5b7a5b-c586-4694-88e1-afa97116f7772017-02-18T18:36:52+03:00Shameka8701e5e25c9-71be-4597-a468-da61b0abba38Hettinger5942017-03-02T18:36:52+03:00999-45-86171974-03-21resolved195662009Acute viral pharyngitis (disorder)female
49cad500-1ca6-4a51-8c6f-daf7e8ec9c712018-02-24T17:15:40+03:00Rex532046bc10-1342-4363-a338-3ce21ca64032Schroeder4472018-03-06T17:15:40+03:00999-37-89911980-09-02resolved195662009Acute viral pharyngitis (disorder)male
41907da4-2926-4f7a-9326-bb18585b07bf2012-04-07T09:53:34+04:00Karon9072d8c29e6-33a8-4473-aea0-56b5d65d7ab8Howe4132012-04-16T09:53:34+04:00999-88-76521964-02-15resolved195662009Acute viral pharyngitis (disorder)female
b2d58f0f-4499-4392-ad3a-1c2141c8a6c12011-02-27T08:14:00+03:00Patty6002dde8371-6bb4-4d56-a72e-3f7956c2b4a4Lindgren2552011-03-09T08:14:00+03:00999-40-35331992-10-01resolved195662009Acute viral pharyngitis (disorder)female
6fae52cd-c193-4316-be74-420a4656ea672015-05-30T00:22:41+03:00Tod26531c22780-594c-443e-a85c-dadd8ec96535Romaguera672015-06-12T00:22:41+03:00999-96-46801984-08-03resolved195662009Acute viral pharyngitis (disorder)male
c60ac337-2ebe-48f0-92bf-9312991421642016-12-16T06:51:36+03:00Faviola5263316f3b3-fa2c-4c45-b8cc-d9c692c02b15Langosh7902016-12-23T06:51:36+03:00999-82-87781974-01-29resolved195662009Acute viral pharyngitis (disorder)female
c60ac337-2ebe-48f0-92bf-9312991421642016-12-16T06:51:36+03:00Faviola5263316f3b3-fa2c-4c45-b8cc-d9c692c02b15Lubowitz582016-12-23T06:51:36+03:00999-82-87781974-01-29resolved195662009Acute viral pharyngitis (disorder)female
8fe97232-f264-4f4f-873e-ae61ae6c63442019-03-12T03:35:59+03:00Stuart9133658dc2e-0ebf-4ff5-929a-0448ba3424fbKing7432019-03-24T03:35:59+03:00999-51-70371936-10-29resolved195662009Acute viral pharyngitis (disorder)male
43b3d8de-e4d6-41b9-a98f-3818ed42c4132016-10-03T13:47:20+03:00Sally2273f4176a2-1ff7-4012-8299-85aa5839f1dbFarrell9622016-10-11T13:47:20+03:00999-97-70311987-04-28resolved195662009Acute viral pharyngitis (disorder)female
43b3d8de-e4d6-41b9-a98f-3818ed42c4132016-10-03T13:47:20+03:00Sally2273f4176a2-1ff7-4012-8299-85aa5839f1dbHowell9472016-10-11T13:47:20+03:00999-97-70311987-04-28resolved195662009Acute viral pharyngitis (disorder)female
b47bf393-a5fa-449c-89e1-ab022e02e00b2019-01-24T19:56:48+03:00Rosita520459698a9-3ea3-4095-a329-3eb3e3774c1cHermann1032019-02-02T19:56:48+03:00999-14-53822013-09-18resolved195662009Acute viral pharyngitis (disorder)female
d291a9be-9f47-4801-a4da-67b0984daaa02013-02-15T12:45:42+04:00Fred1554a4c092f-2e3b-4bc7-a317-77c0d9a95e42Dicki442013-02-28T12:45:42+04:00999-44-92982006-02-21resolved195662009Acute viral pharyngitis (disorder)male
23781071-a79a-4d2f-89d2-d84c003cfcb42016-09-21T17:18:25+03:00Cheryl6054b745b26-7300-46ad-b2b5-880d5aa1e9ceHeller3422016-10-04T17:18:25+03:00999-80-77661940-08-25resolved195662009Acute viral pharyngitis (disorder)female
23781071-a79a-4d2f-89d2-d84c003cfcb42016-09-21T17:18:25+03:00Cheryl6054b745b26-7300-46ad-b2b5-880d5aa1e9ceChamplin9462016-10-04T17:18:25+03:00999-80-77661940-08-25resolved195662009Acute viral pharyngitis (disorder)female
8fe97232-f264-4f4f-873e-ae61ae6c63442018-08-03T03:35:59+03:00Stuart9134c1e8b0d-d20c-4561-8146-26868660df31King7432018-08-10T03:35:59+03:00999-51-70371936-10-29resolved195662009Acute viral pharyngitis (disorder)male
b2d58f0f-4499-4392-ad3a-1c2141c8a6c12019-01-14T08:14:00+03:00Patty60053a60e10-e2cc-4e1f-bd69-9935675f662cLindgren2552019-01-21T08:14:00+03:00999-40-35331992-10-01resolved195662009Acute viral pharyngitis (disorder)female
e0f57407-86ed-405b-aa55-56fb0bebc4c72013-12-27T09:51:18+04:00Shara355588b0f0a-a4d0-458c-adbf-b17ee147f042Yundt8422014-01-05T09:51:18+04:00999-37-67461996-05-28resolved195662009Acute viral pharyngitis (disorder)female
5e23837b-8ef5-46cd-9046-82cd4033bdb32012-05-20T03:13:33+04:00Bettye6715a0f3284-bb70-481f-b5a7-f43f03de1088Davis9232012-05-27T03:13:33+04:00999-82-15361970-03-30resolved195662009Acute viral pharyngitis (disorder)female
5deb2b4c-e1e2-4c1a-a74e-f028db8aa4102016-08-09T22:43:34+03:00Mari7635c9a219b-fa09-4522-bfb5-8a0c17eb0dbfSchaden6042016-08-17T22:43:34+03:00999-53-79731998-11-29resolved195662009Acute viral pharyngitis (disorder)female
83991f61-aff5-4ae6-8559-3ef100c931a72014-04-28T13:35:17+04:00Rickey8215e8c8839-2502-45e9-808e-931b618af463Carter5492014-05-11T13:35:17+04:00999-13-97521947-03-10resolved195662009Acute viral pharyngitis (disorder)male
427e7fc7-b8cc-4d60-9139-0a4f0ae94d552014-10-30T17:46:41+03:00Adán60060f7f900-f83a-4063-9e9f-6c2ec55afc41Galván1692014-11-12T17:46:41+03:00999-97-92232012-10-06resolved195662009Acute viral pharyngitis (disorder)male
93f09189-7ec2-4d0e-9310-f14efce8e4b62017-06-30T13:44:17+03:00Mariann762672cfc60-f0b0-491b-bc4f-a4c2d0d5cf74White1932017-07-11T13:44:17+03:00999-71-16731988-02-27resolved195662009Acute viral pharyngitis (disorder)female
93f09189-7ec2-4d0e-9310-f14efce8e4b62017-06-30T13:44:17+03:00Mariann762672cfc60-f0b0-491b-bc4f-a4c2d0d5cf74Ankunding2772017-07-11T13:44:17+03:00999-71-16731988-02-27resolved195662009Acute viral pharyngitis (disorder)female
77b530c6-f309-44cc-bbfb-db33b8e7db7b2014-10-06T08:58:37+04:00Roselyn2706cfa9c7f-6eca-4d30-80da-e871a14281c1Herman7632014-10-18T08:58:37+04:00999-10-91902011-12-20resolved195662009Acute viral pharyngitis (disorder)female
f83ce939-05b8-4428-805b-f2f118de94162018-12-31T23:07:46+03:00Candida6546df4e1e4-e210-4ae4-85c4-a0567b0ef423Schroeder4472019-01-12T23:07:46+03:00999-55-96382008-08-07resolved195662009Acute viral pharyngitis (disorder)female
7431e052-639d-41bd-b20d-7883640f1a5a2016-12-28T22:29:55+03:00Vella93072a37033-0f55-4e0e-be46-99f985ec07edO'Keefe542017-01-08T22:29:55+03:00999-30-98751990-11-01resolved195662009Acute viral pharyngitis (disorder)female
7431e052-639d-41bd-b20d-7883640f1a5a2016-12-28T22:29:55+03:00Vella93072a37033-0f55-4e0e-be46-99f985ec07edVonRueden3762017-01-08T22:29:55+03:00999-30-98751990-11-01resolved195662009Acute viral pharyngitis (disorder)female
51f199d3-9d88-42d8-b478-6ed89d57dbf42010-08-24T01:48:47+04:00Nicolas76976c5dc99-b475-406b-b172-6578d3852ffdStracke6112010-09-05T01:48:47+04:00999-45-46051979-10-08resolved195662009Acute viral pharyngitis (disorder)male
5e23837b-8ef5-46cd-9046-82cd4033bdb32018-07-13T02:13:33+03:00Bettye6717b20a692-8246-484b-bf63-24603ccaf497Davis9232018-07-22T02:13:33+03:00999-82-15361970-03-30resolved195662009Acute viral pharyngitis (disorder)female
ec0115d1-ac56-477e-b27c-0f5c51c0c2162011-04-04T03:20:50+04:00Gricelda417b28b597-a1a3-48a1-b143-3ce636c617f4Koch1692011-04-13T03:20:50+04:00999-78-40991945-01-09resolved195662009Acute viral pharyngitis (disorder)female
ec0115d1-ac56-477e-b27c-0f5c51c0c2162011-04-04T03:20:50+04:00Gricelda417b28b597-a1a3-48a1-b143-3ce636c617f4Schinner6822011-04-13T03:20:50+04:00999-78-40991945-01-09resolved195662009Acute viral pharyngitis (disorder)female
a8cf2e08-8f8d-4800-88f3-aa1d1c1a8cec2017-12-13T01:12:04+03:00Lenny7287b4cb9e1-2926-4848-886a-ce2e76a5e26eHuel6282017-12-23T01:12:04+03:00999-10-56541987-06-06resolved195662009Acute viral pharyngitis (disorder)male
b2d58f0f-4499-4392-ad3a-1c2141c8a6c12014-05-25T09:14:00+04:00Patty60084c77958-0101-4096-83b3-e99a6668dde3Lindgren2552014-06-06T09:14:00+04:00999-40-35331992-10-01resolved195662009Acute viral pharyngitis (disorder)female
16dc22c0-96c4-42f0-b42a-afa5f3f28f532015-05-30T00:04:14+03:00Josefine519853cf941-9b28-44ee-a40a-d96d9681c9c8Sanford8612015-06-07T00:04:14+03:00999-85-15122006-05-16resolved195662009Acute viral pharyngitis (disorder)female
ba8ca47f-385f-4b20-acdb-57d6d00783e32016-07-03T03:52:22+03:00Federico58987f3d7ce-6b39-4db1-88d0-52a3f4f5615cSatterfield3052016-07-12T03:52:22+03:00999-93-49281955-08-27resolved195662009Acute viral pharyngitis (disorder)male
357444e9-3159-4063-a975-ccd72923ee8b2011-08-05T07:52:41+04:00Guadalupe2068a0a055a-3009-436b-bd3b-6c149e9eafb4Tijerina142011-08-17T07:52:41+04:00999-49-68781996-12-04resolved195662009Acute viral pharyngitis (disorder)female
179adc51-6875-4191-ad16-160339ed45cc2015-01-27T19:58:05+03:00Kristina5839490e61a-0b4b-41ee-902a-e3a37e260f85Tromp1002015-02-07T19:58:05+03:00999-96-16462011-12-12resolved195662009Acute viral pharyngitis (disorder)female
8d174758-12f6-4b37-96e3-64ab3788eedd2019-05-21T05:27:17+03:00Brendon2989d9693e8-ddba-4ac4-84f5-a5b73ccbe279Koepp5212019-05-29T05:27:17+03:00999-73-80962015-10-23resolved195662009Acute viral pharyngitis (disorder)male
98470b8c-7de8-4d04-a42d-6babf09a85ec2012-03-12T00:48:18+04:00Tatum703a3a15db7-19e8-49f6-9112-bd1ccc1aa408Boehm5812012-03-20T00:48:18+04:00999-36-41431942-10-19resolved195662009Acute viral pharyngitis (disorder)female
98470b8c-7de8-4d04-a42d-6babf09a85ec2012-03-12T00:48:18+04:00Tatum703a3a15db7-19e8-49f6-9112-bd1ccc1aa408Cole1172012-03-20T00:48:18+04:00999-36-41431942-10-19resolved195662009Acute viral pharyngitis (disorder)female
Showing 50 of 76 rows

Compare with the equivalent SQL without using SQL on FHIR

SQL Send Cells run only inside Aidbox. Use Run in Aidbox above to open this notebook in your own instance.
SELECT
	pt.id AS patient_id,
    array_to_string(
      (
        SELECT array_agg(x)
        FROM jsonb_array_elements_text(
          jsonb_path_query_array(name.element, '$.given[*]')
        ) AS x
      ), ' ') AS given,
    jsonb_path_query_first(name.element, '$.family') #>> '{}' AS family,
    jsonb_path_query_first(pt.resource, '$.gender') #>> '{}' AS gender,
    cond.id as condition_id,
    jsonb_path_query_first(cond.resource, '$.onset.dateTime') #>> '{}' AS onset,
    jsonb_path_query_first(cond.resource, '$.abatement.dateTime') #>> '{}' AS abatement,
    jsonb_path_query_first(cond.resource, '$.clinicalStatus.coding.code[0]') #>> '{}' AS status,
    jsonb_path_query_first(cond.resource, '$.code.coding ? (@.system == "http://snomed.info/sct").code[0]') #>> '{}' AS code
FROM Patient AS pt
JOIN Condition cond ON jsonb_path_query_first(cond.resource, '$.subject.id') #>> '{}' = pt.id
JOIN LATERAL jsonb_path_query(pt.resource, '$.name [*]') name(element) ON true
WHERE
	jsonb_path_query_first(cond.resource, '$.code.coding ? (@.system == "http://snomed.info/sct").code[0]') #>> '{}' = '195662009'
    AND max_text_date_bound(jsonb_path_query_first(cond.resource, '$.abatement.dateTime') #>> '{}') > (NOW() - interval '20 years')
Result
patient_idonsetgivencondition_idfamilyabatementstatuscodegender
93f09189-7ec2-4d0e-9310-f14efce8e4b62016-05-27T13:44:17+03:00Mariann762014774ea-8dac-46c0-a9b7-1554613d8625White1932016-06-03T13:44:17+03:00resolved195662009female
93f09189-7ec2-4d0e-9310-f14efce8e4b62016-05-27T13:44:17+03:00Mariann762014774ea-8dac-46c0-a9b7-1554613d8625Ankunding2772016-06-03T13:44:17+03:00resolved195662009female
8742d4ba-19ee-4f45-a8c1-3a53bd01cf012016-10-06T05:24:13+03:00Wallace64702bfc9af-dbcf-44d0-852d-32914be49458Mayert7102016-10-14T05:24:13+03:00resolved195662009male
7431e052-639d-41bd-b20d-7883640f1a5a2013-08-07T23:29:55+04:00Vella93004931984-f7e8-426a-bcb3-242fbaab6a32O'Keefe542013-08-15T23:29:55+04:00resolved195662009female
7431e052-639d-41bd-b20d-7883640f1a5a2013-08-07T23:29:55+04:00Vella93004931984-f7e8-426a-bcb3-242fbaab6a32VonRueden3762013-08-15T23:29:55+04:00resolved195662009female
ba8ca47f-385f-4b20-acdb-57d6d00783e32011-09-17T04:52:22+04:00Federico5890735a939-a828-428e-84f5-4d9b3d36d497Satterfield3052011-09-27T04:52:22+04:00resolved195662009male
102e001e-df1b-4fa8-adfe-6e2acb69aae82009-12-20T14:14:25+03:00Hollis70827f77d-34c6-4444-b593-667c23e2e560Ortiz1862009-12-31T14:14:25+03:00resolved195662009male
14025d1b-3e46-4cbb-9ef2-f913cb6be1392015-07-31T17:13:15+03:00Kassandra2560aba7690-5016-4958-8002-f4868dd0734eMorar5932015-08-10T17:13:15+03:00resolved195662009female
0ec5c51b-56d4-433e-a320-297faf0612372011-09-02T10:15:45+04:00Somer380eb5d1d7-8735-4f9d-85b1-59bd253d3be4Nicolas7692011-09-14T10:15:45+04:00resolved195662009female
5b5b7a5b-c586-4694-88e1-afa97116f7772014-09-17T19:36:52+04:00Shameka870122a0e5f-a978-4672-84a9-990dfa9257e4Hettinger5942014-09-30T19:36:52+04:00resolved195662009female
5b5b7a5b-c586-4694-88e1-afa97116f7772017-02-18T18:36:52+03:00Shameka8701e5e25c9-71be-4597-a468-da61b0abba38Hettinger5942017-03-02T18:36:52+03:00resolved195662009female
49cad500-1ca6-4a51-8c6f-daf7e8ec9c712018-02-24T17:15:40+03:00Rex532046bc10-1342-4363-a338-3ce21ca64032Schroeder4472018-03-06T17:15:40+03:00resolved195662009male
41907da4-2926-4f7a-9326-bb18585b07bf2012-04-07T09:53:34+04:00Karon9072d8c29e6-33a8-4473-aea0-56b5d65d7ab8Howe4132012-04-16T09:53:34+04:00resolved195662009female
b2d58f0f-4499-4392-ad3a-1c2141c8a6c12011-02-27T08:14:00+03:00Patty6002dde8371-6bb4-4d56-a72e-3f7956c2b4a4Lindgren2552011-03-09T08:14:00+03:00resolved195662009female
6fae52cd-c193-4316-be74-420a4656ea672015-05-30T00:22:41+03:00Tod26531c22780-594c-443e-a85c-dadd8ec96535Romaguera672015-06-12T00:22:41+03:00resolved195662009male
c60ac337-2ebe-48f0-92bf-9312991421642016-12-16T06:51:36+03:00Faviola5263316f3b3-fa2c-4c45-b8cc-d9c692c02b15Langosh7902016-12-23T06:51:36+03:00resolved195662009female
c60ac337-2ebe-48f0-92bf-9312991421642016-12-16T06:51:36+03:00Faviola5263316f3b3-fa2c-4c45-b8cc-d9c692c02b15Lubowitz582016-12-23T06:51:36+03:00resolved195662009female
8fe97232-f264-4f4f-873e-ae61ae6c63442019-03-12T03:35:59+03:00Stuart9133658dc2e-0ebf-4ff5-929a-0448ba3424fbKing7432019-03-24T03:35:59+03:00resolved195662009male
43b3d8de-e4d6-41b9-a98f-3818ed42c4132016-10-03T13:47:20+03:00Sally2273f4176a2-1ff7-4012-8299-85aa5839f1dbFarrell9622016-10-11T13:47:20+03:00resolved195662009female
43b3d8de-e4d6-41b9-a98f-3818ed42c4132016-10-03T13:47:20+03:00Sally2273f4176a2-1ff7-4012-8299-85aa5839f1dbHowell9472016-10-11T13:47:20+03:00resolved195662009female
b47bf393-a5fa-449c-89e1-ab022e02e00b2019-01-24T19:56:48+03:00Rosita520459698a9-3ea3-4095-a329-3eb3e3774c1cHermann1032019-02-02T19:56:48+03:00resolved195662009female
d291a9be-9f47-4801-a4da-67b0984daaa02013-02-15T12:45:42+04:00Fred1554a4c092f-2e3b-4bc7-a317-77c0d9a95e42Dicki442013-02-28T12:45:42+04:00resolved195662009male
23781071-a79a-4d2f-89d2-d84c003cfcb42016-09-21T17:18:25+03:00Cheryl6054b745b26-7300-46ad-b2b5-880d5aa1e9ceHeller3422016-10-04T17:18:25+03:00resolved195662009female
23781071-a79a-4d2f-89d2-d84c003cfcb42016-09-21T17:18:25+03:00Cheryl6054b745b26-7300-46ad-b2b5-880d5aa1e9ceChamplin9462016-10-04T17:18:25+03:00resolved195662009female
8fe97232-f264-4f4f-873e-ae61ae6c63442018-08-03T03:35:59+03:00Stuart9134c1e8b0d-d20c-4561-8146-26868660df31King7432018-08-10T03:35:59+03:00resolved195662009male
b2d58f0f-4499-4392-ad3a-1c2141c8a6c12019-01-14T08:14:00+03:00Patty60053a60e10-e2cc-4e1f-bd69-9935675f662cLindgren2552019-01-21T08:14:00+03:00resolved195662009female
e0f57407-86ed-405b-aa55-56fb0bebc4c72013-12-27T09:51:18+04:00Shara355588b0f0a-a4d0-458c-adbf-b17ee147f042Yundt8422014-01-05T09:51:18+04:00resolved195662009female
5e23837b-8ef5-46cd-9046-82cd4033bdb32012-05-20T03:13:33+04:00Bettye6715a0f3284-bb70-481f-b5a7-f43f03de1088Davis9232012-05-27T03:13:33+04:00resolved195662009female
5deb2b4c-e1e2-4c1a-a74e-f028db8aa4102016-08-09T22:43:34+03:00Mari7635c9a219b-fa09-4522-bfb5-8a0c17eb0dbfSchaden6042016-08-17T22:43:34+03:00resolved195662009female
83991f61-aff5-4ae6-8559-3ef100c931a72014-04-28T13:35:17+04:00Rickey8215e8c8839-2502-45e9-808e-931b618af463Carter5492014-05-11T13:35:17+04:00resolved195662009male
427e7fc7-b8cc-4d60-9139-0a4f0ae94d552014-10-30T17:46:41+03:00Adán60060f7f900-f83a-4063-9e9f-6c2ec55afc41Galván1692014-11-12T17:46:41+03:00resolved195662009male
93f09189-7ec2-4d0e-9310-f14efce8e4b62017-06-30T13:44:17+03:00Mariann762672cfc60-f0b0-491b-bc4f-a4c2d0d5cf74White1932017-07-11T13:44:17+03:00resolved195662009female
93f09189-7ec2-4d0e-9310-f14efce8e4b62017-06-30T13:44:17+03:00Mariann762672cfc60-f0b0-491b-bc4f-a4c2d0d5cf74Ankunding2772017-07-11T13:44:17+03:00resolved195662009female
77b530c6-f309-44cc-bbfb-db33b8e7db7b2014-10-06T08:58:37+04:00Roselyn2706cfa9c7f-6eca-4d30-80da-e871a14281c1Herman7632014-10-18T08:58:37+04:00resolved195662009female
f83ce939-05b8-4428-805b-f2f118de94162018-12-31T23:07:46+03:00Candida6546df4e1e4-e210-4ae4-85c4-a0567b0ef423Schroeder4472019-01-12T23:07:46+03:00resolved195662009female
7431e052-639d-41bd-b20d-7883640f1a5a2016-12-28T22:29:55+03:00Vella93072a37033-0f55-4e0e-be46-99f985ec07edO'Keefe542017-01-08T22:29:55+03:00resolved195662009female
7431e052-639d-41bd-b20d-7883640f1a5a2016-12-28T22:29:55+03:00Vella93072a37033-0f55-4e0e-be46-99f985ec07edVonRueden3762017-01-08T22:29:55+03:00resolved195662009female
51f199d3-9d88-42d8-b478-6ed89d57dbf42010-08-24T01:48:47+04:00Nicolas76976c5dc99-b475-406b-b172-6578d3852ffdStracke6112010-09-05T01:48:47+04:00resolved195662009male
5e23837b-8ef5-46cd-9046-82cd4033bdb32018-07-13T02:13:33+03:00Bettye6717b20a692-8246-484b-bf63-24603ccaf497Davis9232018-07-22T02:13:33+03:00resolved195662009female
ec0115d1-ac56-477e-b27c-0f5c51c0c2162011-04-04T03:20:50+04:00Gricelda417b28b597-a1a3-48a1-b143-3ce636c617f4Koch1692011-04-13T03:20:50+04:00resolved195662009female
ec0115d1-ac56-477e-b27c-0f5c51c0c2162011-04-04T03:20:50+04:00Gricelda417b28b597-a1a3-48a1-b143-3ce636c617f4Schinner6822011-04-13T03:20:50+04:00resolved195662009female
a8cf2e08-8f8d-4800-88f3-aa1d1c1a8cec2017-12-13T01:12:04+03:00Lenny7287b4cb9e1-2926-4848-886a-ce2e76a5e26eHuel6282017-12-23T01:12:04+03:00resolved195662009male
b2d58f0f-4499-4392-ad3a-1c2141c8a6c12014-05-25T09:14:00+04:00Patty60084c77958-0101-4096-83b3-e99a6668dde3Lindgren2552014-06-06T09:14:00+04:00resolved195662009female
16dc22c0-96c4-42f0-b42a-afa5f3f28f532015-05-30T00:04:14+03:00Josefine519853cf941-9b28-44ee-a40a-d96d9681c9c8Sanford8612015-06-07T00:04:14+03:00resolved195662009female
ba8ca47f-385f-4b20-acdb-57d6d00783e32016-07-03T03:52:22+03:00Federico58987f3d7ce-6b39-4db1-88d0-52a3f4f5615cSatterfield3052016-07-12T03:52:22+03:00resolved195662009male
357444e9-3159-4063-a975-ccd72923ee8b2011-08-05T07:52:41+04:00Guadalupe2068a0a055a-3009-436b-bd3b-6c149e9eafb4Tijerina142011-08-17T07:52:41+04:00resolved195662009female
179adc51-6875-4191-ad16-160339ed45cc2015-01-27T19:58:05+03:00Kristina5839490e61a-0b4b-41ee-902a-e3a37e260f85Tromp1002015-02-07T19:58:05+03:00resolved195662009female
8d174758-12f6-4b37-96e3-64ab3788eedd2019-05-21T05:27:17+03:00Brendon2989d9693e8-ddba-4ac4-84f5-a5b73ccbe279Koepp5212019-05-29T05:27:17+03:00resolved195662009male
98470b8c-7de8-4d04-a42d-6babf09a85ec2012-03-12T00:48:18+04:00Tatum703a3a15db7-19e8-49f6-9112-bd1ccc1aa408Boehm5812012-03-20T00:48:18+04:00resolved195662009female
98470b8c-7de8-4d04-a42d-6babf09a85ec2012-03-12T00:48:18+04:00Tatum703a3a15db7-19e8-49f6-9112-bd1ccc1aa408Cole1172012-03-20T00:48:18+04:00resolved195662009female
Showing 50 of 76 rows

The same views allow us to find all patients who were born in or after 1970 and were diagnosed with Viral sinusitis in or after 2018.

SQL Send Cells run only inside Aidbox. Use Run in Aidbox above to open this notebook in your own instance.
SELECT *
FROM sof.patient_demo_view pt
JOIN sof.condition_demo_view cond using (patient_id)
WHERE code = '444814009' and pt.dob >= '1970-01-01' and cond.abatement >= '2018-01-01'
Result
patient_idonsetgivencondition_idfamilyabatementpatient_ssndobstatuscodedisplaygender
d9b90399-0504-4f1e-bf52-2f45354994dd2018-01-10T16:36:19+03:00Glenna970acfc938-7731-4210-8252-40c28d209ab3Hills8182018-01-17T16:36:19+03:00999-76-91342017-11-04resolved444814009Viral sinusitis (disorder)female
14025d1b-3e46-4cbb-9ef2-f913cb6be1392018-11-21T17:13:15+03:00Kassandra2562ccac637-c24b-4ce4-8831-46588ad9e9a7Morar5932018-12-05T17:13:15+03:00999-72-37031995-12-16resolved444814009Viral sinusitis (disorder)female
f6315469-d594-422a-ba9f-e6cc7548aec12019-07-05T02:07:21+03:00Joye5755a34347-b73a-4288-ae13-5ccefb03c5e5Stoltenberg4892019-07-26T02:07:21+03:00999-80-43192011-08-27resolved444814009Viral sinusitis (disorder)female
b2d58f0f-4499-4392-ad3a-1c2141c8a6c12019-04-02T08:14:00+03:00Patty6007929b2db-fe4c-47ad-8bd7-f665e652c855Lindgren2552019-04-16T08:14:00+03:00999-40-35331992-10-01resolved444814009Viral sinusitis (disorder)female
2420476f-4a8d-455f-9991-a19f09d2c9a62019-02-07T20:18:47+03:00Teodoro3748096d5c8-26bd-4bfd-8e7a-04601335b16bChristiansen2512019-02-28T20:18:47+03:00999-57-90141996-03-06resolved444814009Viral sinusitis (disorder)male
b52f3afa-bbce-4594-8e38-9a30ca8a99c02019-04-26T19:13:43+03:00Kris249ac2b4bbc-cda9-469e-af5d-ff1d1a7eadf6Jakubowski8322019-05-10T19:13:43+03:00999-47-75111997-04-10resolved444814009Viral sinusitis (disorder)male
49cad500-1ca6-4a51-8c6f-daf7e8ec9c712018-06-23T17:15:40+03:00Rex53d6e9900f-3c51-44bf-a345-1fdf584122f5Schroeder4472018-07-07T17:15:40+03:00999-37-89911980-09-02resolved444814009Viral sinusitis (disorder)male
d4734450-3eab-44b1-b2fb-0940c9e3bb102018-06-17T18:51:05+03:00Blythe746d9c46161-8af0-4c1c-922a-e89c3ff41b74Kub8002018-07-08T18:51:05+03:00999-82-40461981-01-17resolved444814009Viral sinusitis (disorder)female
d4734450-3eab-44b1-b2fb-0940c9e3bb102018-06-17T18:51:05+03:00Blythe746d9c46161-8af0-4c1c-922a-e89c3ff41b74Fay3982018-07-08T18:51:05+03:00999-82-40461981-01-17resolved444814009Viral sinusitis (disorder)female

Let's find the most prevalent conditions observed

SQL Send Cells run only inside Aidbox. Use Run in Aidbox above to open this notebook in your own instance.
SELECT code, display, count (*) from sof.condition_demo_view
GROUP BY 1,2
ORDER BY 3 desc
limit 10
Result
codecountdisplay
444814009125Viral sinusitis (disorder)
19566200973Acute viral pharyngitis (disorder)
1050900256Acute bronchitis (disorder)
16286400547Body mass index 30+ - obesity (finding)
7289200238Normal pregnancy
1577700038Prediabetes
27173700037Anemia (disorder)
5962100036Hypertension
6536300230Otitis media
4005500028Chronic sinusitis (disorder)