SQL on FHIR
2.1.0-pre - release International flag

SQL on FHIR, published by SQL on FHIR Working Group. This guide is not an authorized publication; it is the continuous build for version 2.1.0-pre built by the FHIR (HL7® FHIR® Standard) CI Build. This version is based on the current content of https://github.com/FHIR/sql-on-fhir-v2/ and changes regularly. See the Directory of published versions

Library: SQL on FHIR Example

Official URL: https://sql-on-fhir.org/ig/Library/SqlOnFhirExample Version: 2.1.0-pre
Active as of 2025-09-04 Computable Name: SqlOnFhirExample

Demonstrating a SQL Query Library with basic annotations that can assist in generating properties and metadata.

/*
@title: SQL on FHIR Example
@description: Demonstrating converting SQL to FHIR Library with basic annotations 
@version: 4.2.0
@status: active
@author: Clinical Informatics Team
@publisher: Regional Medical Center
*/

-- @relatedDependency: https://sql-on-fhir.org/ig/StructureDefinition/ViewDefinition/PatientDemographics
-- @relatedDependency: https://sql-on-fhir.org/ig/StructureDefinition/ViewDefinition/PatientAddresses
-- @param: city string
WITH RankedAddresses AS (
    SELECT 
        pd.*,
        pa.*,
        ROW_NUMBER() OVER (PARTITION BY pd.patient_id ORDER BY pa.address_id) AS address_rank
    FROM 
        patient_demographics pd
    JOIN 
        patient_addresses pa ON pd.patient_id = pa.patient_id
    WHERE 
        pd.age > 18
        AND pa.city = :city
)

Participants

AuthorClinical Informatics Team

Related Artifacts

Depends Onhttps://sql-on-fhir.org/ig/StructureDefinition/ViewDefinition/PatientDemographics
Depends Onhttps://sql-on-fhir.org/ig/StructureDefinition/ViewDefinition/PatientAddresses

Parameters

cityinstring

Contents

sql_on_fhir_example.sql: (application/sql)

/*
@title: Trivial SQL on FHIR Example
@description: Demonstrating converting SQL to FHIR Library with basic annotations 
@version: 4.2.0
@status: active
@author: Clinical Informatics Team
@publisher: Regional Medical Center
*/

-- @relatedDependency: https://sql-on-fhir.org/ig/StructureDefinition/ViewDefinition/PatientDemographics
-- @relatedDependency: https://sql-on-fhir.org/ig/StructureDefinition/ViewDefinition/PatientAddresses
-- @param: city string
WITH RankedAddresses AS (
    SELECT 
        pd.*,
        pa.*,
        ROW_NUMBER() OVER (PARTITION BY pd.patient_id ORDER BY pa.address_id) AS address_rank
    FROM 
        patient_demographics pd
    JOIN 
        patient_addresses pa ON pd.patient_id = pa.patient_id
    WHERE 
        pd.age > 18
        AND pa.city = :city
)