CBR-to-SQL: Rethinking Retrieval-based Text-to-SQL using Case-based Reasoning in the Healthcare Domain
Organizations: Department of Computer Science Aalto University · Department of Clinical Medicine Aalborg University
Abstract
Extracting insights from Electronic Health Record (EHR) databases often requires SQL expertise, creating a barrier for clinical decision-making and research. A promising approach is to use Large Language Models (LLMs) to translate natural language questions into SQL through Retrieval-Augmented Generation (RAG), where relevant question-SQL examples are retrieved to generate new queries via few-shot learning. However, adapting this method to the medical domain is non-trivial, as effective retrieval requires examples that align with both the logical structure of the question and its referenced entities (e.g., drug names, procedure titles). Standard single-step RAG struggles to optimize both aspects simultaneously and often relies on near-exact matches to generalize effectively. This issue is especially severe in healthcare, as questions often contain noisy and inconsistent medical jargon. To address this, we present CBR-to-SQL, a framework inspired by Case-based Reasoning theory that decomposes RAG's single-step retrieval into two explicit stages: one that focuses on retrieving structurally relevant examples, and one that aligns entities with the target database schema. Evaluated on two clinical benchmarks, CBR-to-SQL achieves competitive accuracies compared to fine-tuned methods. More importantly, it demonstrates considerably higher sample efficiency and robustness than the standard RAG approach, particularly under data scarcity and retrieval perturbations.
Figures & tables
| Method | ||
| SQLNet | 0.260 | 0.142 |
| PtrGen | 0.292 | 0.180 |
| Coarse2Fine | 0.378 | 0.496 |
| TREQS | 0.654 | 0.556 |
| MedTS | 0.899 | 0.784 |
| Chen et al. (2026) | 0.942 | – |
| Method | MIMICSQL | EHRSQL | ||
| RAG-to-SQL | 0.074 | 0.052 | 0.138 | 0.149 |
| CBR-to-SQL | 0.028 | 0.033 | 0.043 | 0.128 |
| Method | MIMICSQL | EHRSQL | ||
| RAG-to-SQL | 0.781 | 0.779 | 0.718 | 0.542 |
| CBR-to-SQL | 0.871 | 0.836 | 0.837 | 0.644 |
| Method | MIMICSQL | EHRSQL | ||
| RAG-to-SQL | 0.082 | 0.079 | 0.171 | 0.184 |
| CBR-to-SQL | 0.065 | 0.061 | 0.086 | 0.182 |
| Method | ||
| CBR-to-SQL | 0.894 | 0.862 |
| Replace Proposal stage | 0.889 | 0.851 |
| No Revision stage | 0.786 | 0.776 |
Appendix figures & tables17 assets
Supplementary material from the paper’s appendix.
Appendix
| Metric | MIMICSQL | EHRSQL |
| Tables Processed | 5 | 17 |
| Columns Analyzed | 70 | 226 |
| Text Columns | 30 | 97 |
| Passed Simple Filter | 22 | 75 |
| Passed LLM Filter | 19 | 26 |
| Entities Indexed | 82,685 | 200,146 |
| Category | MIMICSQL | EHRSQL |
| Accepted Columns | ||
| Demographics | NAME, MARITAL_STATUS, | admission_type, |
| RELIGION, ETHNICITY | admission_location, | |
| discharge_location, | ||
| insurance | ||
| Diagnoses | SHORT_TITLE, LONG_TITLE | long_title |
| Statistic | Value |
| Number of patients | 46,520 |
| Number of tables | 5 |
| Number of columns in tables 2 2 2 Columns are listed in order: Demographics, Diagnoses, Procedures, Prescriptions, Laboratory Tests. | 23 / 5 / 5 / 7 / 9 |
| Number of Question-SQL pairs | 10,000 |
| Average template question length (words) | 18.39 |
| Average natural language question length (words) | 16.45 |
| Table | Description |
| Demographics | Contains patient demographic information such as age, gender, ethnicity, admission details, and other identifiers. |
| Diagnoses | Records all diagnosis codes (ICD-9) assigned during hospital stays, capturing the medical conditions and comorbidities of patients. |
| Procedures | Contains procedural codes (ICD-9) for interventions or surgeries performed on patients during their hospital stay. |
| Prescriptions | Details of medication orders including drug name, dosage, route, frequency, and timing, prescribed to patients. |
| Laboratory Tests | Laboratory measurements and test results such as blood tests, biochemistry, and hematology with timestamps. |
| Train | Valid | Test | |
| Answerable question template | 100 | 134 | 134 |
| Answerable samples | 4674 | 931 | 934 |
| Unanswerable samples | 450 | 232 | 233 |
| Total samples | 5124 | 1163 | 1167 |
| Dataset | Cluster Size | Representative Questions |
| MIMICSQL | 124 | How many patients were diagnosed with anxiety state NOS and were given drugs orally? What is the number of patients whose diagnoses title is acute nephritis nec and drug route is po? Provide the number of patients who are diagnosed with Wegener’s granulomatosis and have sc route of drug administration. |
| 102 | Count the number of patients whose primary disease is left internal jugular vein thrombosis; left arm edema and who died in or before the year 2115. How many patients died of colangitis in or before the year 2180? What is the number of patients who had rash as primary disease and died in 2155 or before that? | |
| EHRSQL | 75 | Tell me the price of trimethoprim. Prazosin – How much does it cost? What are the prices of the drug albumin 25% (12.5g / 50ml)? |
| 72 | How much does the pH change in patient 10021666 last measured on the last hospital visit compared to the first value measured on the last hospital visit? What are patient 10027602’s changes in glucose, CSF second measured on the first hospital visit compared to the first value measured on the first hospital visit? How much difference is there in phosphate levels among patient 10015931 second measured on the first hospital visit compared to the value first measured on the first hospital visit? |
| Method | MIMICSQL | EHRSQL | ||
| Latency ( ) | Token ( ) | Latency ( ) | Token ( ) | |
| RAG-to-SQL | 1.67 | 2251.68 | 3.02 | 6819.99 |
| CBR-to-SQL | 2.60 | 5399.09 | 4.43 | 9512.79 |
| Metric | MIMICSQL | EHRSQL | ||
| Per 1% Gain | Per 1% Gain | Per 1% Gain | Per 1% Gain | |
| Latency Overhead ( ) | 0.103 | 0.163 | 0.118 | 0.138 |
| Token Overhead ( ) | 349.71 | 552.18 | 226.29 | 264.00 |
| Method | ||
| RAG-to-SQL | 0.818 | 0.677 |
| RAG-to-SQL + Neural Re-ranking | 0.828 | 0.725 |
| RAG-to-SQL + Hybrid Retrieval | 0.842 | 0.733 |
| CBR-to-SQL | 0.895 | 0.729 |
| CBR-to-SQL + Neural Re-ranking | 0.893 | 0.739 |
| CBR-to-SQL + Hybrid Retrieval | 0.909 | 0.775 |
| Method | |||||
| Coarse2Fine | 0.321 | 0.313 | 0.321 | 0.260 | 0.214 |
| Seq2Seq | 0.978 | 0.872 | 0.926 | 0.471 | 0.174 |
| SQLNet | 0.994 | 0.939 | 0.933 | 0.722 | 0.080 |
| PtrGen | 0.987 | 0.917 | 0.944 | 0.795 | 0.236 |
| TREQS | 0.990 | 0.912 | 0.942 | 0.834 | 0.694 |
| MedTS | 0.994 | 0.988 | 0.971 | 0.893 | 0.785 |
| Method | |||||
| RAG-to-SQL | 0.993 | 0.943 | 0.948 | 0.875 | 0.830 |
| CBR-to-SQL | 0.992 | 0.944 | 0.980 | 0.910 | 0.883 |
| Method | ||
| RAG-to-SQL | 0.841 | 0.820 |
| CBR-to-SQL | 0.862 | 0.835 |
| Method | |||||
| CBR-to-SQL | 0.994 | 0.957 | 0.985 | 0.927 | 0.900 |
| Replace Proposal | 0.993 | 0.958 | 0.971 | 0.909 | 0.889 |
| No Revision | 0.994 | 0.952 | 0.947 | 0.867 | 0.816 |
| Error | Representative Example |
|---|---|
| Canonical Form Mismatch | Question: How many patients born before the year 2109 were primarily diagnosed for guillain barre syndrome ? CBR-to-SQL (Fail): SELECT COUNT ( DISTINCT DEMOGRAPHIC."SUBJECT_ID" ) FROM DEMOGRAPHIC WHERE DEMOGRAPHIC."DIAGNOSIS" = "GUILLAIN-BARRE SYNDROME" AND DEMOGRAPHIC."DOB_YEAR" < "2109" RAG-to-SQL (Success): SELECT COUNT ( DISTINCT DEMOGRAPHIC."SUBJECT_ID" ) FROM DEMOGRAPHIC WHERE DEMOGRAPHIC."DIAGNOSIS" = "GUILLAIN BARRE SYNDROME" AND DEMOGRAPHIC."DOB_YEAR" < "2109" |
| Incorrect Source Retrieval | Question: Count the number of patients for whom csf lab test was ordered. CBR-to-SQL (Fail): SELECT COUNT ( DISTINCT DEMOGRAPHIC."SUBJECT_ID" ) FROM DEMOGRAPHIC INNER JOIN LAB on DEMOGRAPHIC.HADM_ID = LAB.HADM_ID WHERE LAB."LABEL" = "Glucose, CSF" RAG-to-SQL (Success): SELECT COUNT ( DISTINCT DEMOGRAPHIC."SUBJECT_ID" ) FROM DEMOGRAPHIC INNER JOIN LAB on DEMOGRAPHIC.HADM_ID = LAB.HADM_ID WHERE LAB."FLUID" = "Cerebrospinal Fluid (CSF)" |
| Unrecognized Entity | Question: What is the number of patients with replaced route of drug administration? CBR-to-SQL (Fail): SELECT COUNT ( DISTINCT DEMOGRAPHIC."SUBJECT_ID" ) FROM DEMOGRAPHIC INNER JOIN PRESCRIPTIONS ON DEMOGRAPHIC.HADM_ID = PRESCRIPTIONS.HADM_ID WHERE PRESCRIPTIONS."ROUTE" = "REPLACED" RAG-to-SQL (Success): SELECT COUNT ( DISTINCT DEMOGRAPHIC."SUBJECT_ID" ) FROM DEMOGRAPHIC INNER JOIN PRESCRIPTIONS ON DEMOGRAPHIC.HADM_ID = PRESCRIPTIONS.HADM_ID WHERE PRESCRIPTIONS."ROUTE" = "REPLACE" |
| Incorrect Entity Tagging | Question: What is the maximum number of days of hospital stay for patients with hypertension but not coronary artery disease/cardiac cath ? CBR-to-SQL (Fail): SELECT MAX ( DEMOGRAPHIC."DAYS_STAY" ) FROM DEMOGRAPHIC WHERE DEMOGRAPHIC."DIAGNOSIS" = "HYPERTENSION" AND DEMOGRAPHIC."DIAGNOSIS" <> "CORONARY ARTERY DISEASE\CARDIAC CATH" RAG-to-SQL (Success): SELECT MAX ( DEMOGRAPHIC."DAYS_STAY" ) FROM DEMOGRAPHIC WHERE DEMOGRAPHIC."DIAGNOSIS" = "HYPERTENSION;RULE OUT CORONARY ARTERY DISEASE\CARDIAC CATH" |