conversations
stringlengths
3.03k
7.6k
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: Which patients have had the most appointments with a specific doctor in the last 3 years, considering only the appointments that were completed?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n p.patient_id, \n p.first_name, \n p.last_name, \n d.doctor_id, \n d.first_name AS doctor_first_name, \n d.last_name AS doctor_last_name, \n COUNT(a.appointment_id) AS num_appointments\nFROM \n Patients p\nJOIN \n Appointments a ON p.patient_id = a.patient_id\nJOIN \n Doctors d ON a.doctor_id = d.doctor_id\nWHERE \n a.appointment_date >= DATE('now', '-3 years') AND\n a.status = 'Completed' AND\n d.doctor_id = ? -- specify the doctor's ID\nGROUP BY \n p.patient_id, \n p.first_name, \n p.last_name, \n d.doctor_id, \n d.first_name, \n d.last_name\nORDER BY \n num_appointments DESC;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the total number of appointments for each doctor, considering only the appointments that were scheduled in the last 2 years and are not recurring?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n d.doctor_id, \n d.first_name, \n d.last_name, \n COUNT(a.appointment_id) AS total_appointments\nFROM \n Doctors d\nJOIN \n Appointments a ON d.doctor_id = a.doctor_id\nWHERE \n a.appointment_date >= DATE('now', '-2 years') AND \n a.status = 'Scheduled'\nGROUP BY \n d.doctor_id, \n d.first_name, \n d.last_name"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: Which doctors have the highest number of appointments with patients who have a specific medical condition, considering only the appointments that were completed in the last 2 years?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n d.doctor_id, \n d.first_name, \n d.last_name, \n COUNT(a.appointment_id) AS num_appointments\nFROM \n Doctors d\nJOIN \n Appointments a ON d.doctor_id = a.doctor_id\nJOIN \n MedicalHistory mh ON a.patient_id = mh.patient_id\nWHERE \n a.appointment_date >= DATE('now', '-2 years') AND \n a.status = 'Completed' AND \n mh.diagnosis = 'Asthma'\nGROUP BY \n d.doctor_id, \n d.first_name, \n d.last_name\nORDER BY \n num_appointments DESC\nLIMIT 10"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the average number of appointments per quarter for each doctor, considering only the appointments that were scheduled in the last year and are recurring?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n d.doctor_id,\n d.first_name,\n d.last_name,\n AVG(COUNT(a.appointment_id)) / 4 AS avg_appointments_per_quarter\nFROM \n Doctors d\nJOIN \n Appointments a ON d.doctor_id = a.doctor_id\nWHERE \n a.appointment_date >= DATE('now', '-1 year') \n AND a.status = 'Scheduled'\nGROUP BY \n d.doctor_id,\n d.first_name,\n d.last_name,\n strftime('%Q', a.appointment_date)"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: Which patients have had the most appointments with a specific doctor in the last 4 years, considering only the appointments that were completed?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "-- Query to find patients with the most appointments with a specific doctor in the last 4 years\n\nSELECT \n p.patient_id, \n p.first_name, \n p.last_name, \n COALESCE(COUNT(*), 0) AS num_appointments\nFROM \n Patients p\nLEFT JOIN \n Appointments a ON p.patient_id = a.patient_id\nLEFT JOIN \n Doctors d ON a.doctor_id = d.doctor_id\nWHERE \n a.appointment_date >= DATE('now') - INTERVAL 4 YEAR AND \n a.status = 'Completed' AND \n d.doctor_id = :doctor_id -- Named parameter for the doctor's ID\nGROUP BY \n p.patient_id, \n p.first_name, \n p.last_name\nORDER BY \n num_appointments DESC\nLIMIT 10;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the total number of appointments for each doctor, considering only the appointments that were scheduled in the last 3 years and are not recurring?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n d.doctor_id, \n d.first_name, \n d.last_name, \n COUNT(a.appointment_id) AS total_appointments\nFROM \n Doctors d\nJOIN \n Appointments a ON d.doctor_id = a.doctor_id\nWHERE \n a.appointment_date >= DATE('now', '-3 years') AND \n a.status = 'Scheduled'\nGROUP BY \n d.doctor_id, \n d.first_name, \n d.last_name"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: Which doctors have the highest number of appointments with patients who have a specific medical condition, considering only the appointments that were completed in the last 3 years?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n d.doctor_id, \n d.first_name, \n d.last_name, \n COUNT(a.appointment_id) AS num_appointments\nFROM \n Doctors d\nJOIN \n Appointments a ON d.doctor_id = a.doctor_id\nJOIN \n MedicalHistory mh ON a.patient_id = mh.patient_id\nWHERE \n a.appointment_date >= DATE('now', '-3 years') AND \n a.status = 'Completed' AND \n mh.diagnosis = 'Chronic Obstructive Pulmonary Disease'\nGROUP BY \n d.doctor_id, \n d.first_name, \n d.last_name\nORDER BY \n num_appointments DESC\nLIMIT 10"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the average number of appointments per year for each doctor, considering only the appointments that were scheduled in the last 4 years and are recurring?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "WITH recurring_doctors AS (\n SELECT \n d.doctor_id,\n d.first_name,\n d.last_name\n FROM \n Doctors d\n JOIN \n Appointments a ON d.doctor_id = a.doctor_id\n WHERE \n a.appointment_date >= DATE('now', '-4 years') AND\n a.status = 'Scheduled'\n GROUP BY \n d.doctor_id,\n d.first_name,\n d.last_name\n HAVING \n COUNT(DISTINCT STRFTIME('%Y', a.appointment_date)) > 1\n)\nSELECT \n rd.doctor_id,\n rd.first_name,\n rd.last_name,\n COUNT(a.appointment_id) / 4 AS avg_appointments_per_year\nFROM \n recurring_doctors rd\n LEFT JOIN Appointments a ON rd.doctor_id = a.doctor_id AND a.appointment_date >= DATE('now', '-4 years') AND a.status = 'Scheduled'\nGROUP BY \n rd.doctor_id,\n rd.first_name,\n rd.last_name"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: Which patients have had the most appointments with a specific doctor in the last 5 years, considering only the appointments that were completed?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "WHERE \n a.appointment_date >= DATE('now', '-5 years') AND \n a.status = 'Completed' AND \n d.doctor_id = ? -- replace with the desired doctor ID"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the total number of appointments for each doctor, considering only the appointments that were scheduled in the last 4 years and are not recurring?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n d.doctor_id, \n d.first_name, \n d.last_name, \n COUNT(a.appointment_id) AS total_appointments\nFROM \n Doctors d\nJOIN \n Appointments a ON d.doctor_id = a.doctor_id\nWHERE \n a.appointment_date >= DATE('now', '-4 years') AND \n a.status = 'Scheduled'\nGROUP BY \n d.doctor_id, \n d.first_name, \n d.last_name"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: Which doctors have the highest number of appointments with patients who have a specific medical condition, considering only the appointments that were completed in the last 4 years?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n d.doctor_id, \n d.first_name, \n d.last_name, \n COUNT(a.appointment_id) AS num_appointments\nFROM \n Doctors d\nJOIN \n Appointments a ON d.doctor_id = a.doctor_id\nJOIN \n MedicalHistory mh ON a.patient_id = mh.patient_id\nWHERE \n a.appointment_date >= DATE('now', '-4 years') AND \n a.status = 'Completed' AND \n mh.diagnosis = 'Heart Failure'\nGROUP BY \n d.doctor_id, \n d.first_name, \n d.last_name\nORDER BY \n num_appointments DESC\nLIMIT 10"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the average number of appointments per month for each doctor, considering only the appointments that were scheduled in the last 5 years and are recurring?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n d.doctor_id,\n d.first_name,\n d.last_name,\n COUNT(a.appointment_id) / (5 * 12) AS avg_appointments_per_month\nFROM \n Doctors d\nJOIN \n Appointments a ON d.doctor_id = a.doctor_id\nWHERE \n a.appointment_date >= DATE('now', '-5 years') AND\n a.status = 'Scheduled'\nGROUP BY \n d.doctor_id,\n d.first_name,\n d.last_name"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of the patients who have been prescribed medication by doctors with a specialization in cardiology and have an appointment scheduled within the next 30 days?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT DISTINCT pat.patient_id, pat.first_name, pat.last_name \nFROM Patients pat \nJOIN Appointments appt ON pat.patient_id = appt.patient_id \nJOIN Prescriptions pres ON pat.patient_id = pres.patient_id \nJOIN Doctors doc ON pres.doctor_id = doc.doctor_id \nWHERE doc.specialization = 'Cardiology' \nAND appt.appointment_date BETWEEN CURRENT_DATE AND CURRENT_DATE + 30\nAND appt.status = 'Scheduled';"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the total amount billed to patients who have undergone a medical test with a specific doctor and have an outstanding balance?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT SUM(b.total_amount) \nFROM Billing b \nJOIN PatientTests pt ON b.patient_id = pt.patient_id \nJOIN Doctors d ON pt.doctor_id = d.doctor_id \nWHERE pt.doctor_id = ? AND b.paid = 0;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of the medications prescribed to patients with a specific medical history diagnosis and are currently taking medication with a specific side effect?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT DISTINCT m.medication_name \nFROM Medications m \nJOIN PrescriptionDetails pd ON m.medication_id = pd.medication_id \nJOIN Prescriptions p ON pd.prescription_id = p.prescription_id \nJOIN MedicalHistory mh ON p.patient_id = mh.patient_id \nWHERE mh.diagnosis = ? AND m.side_effects LIKE '%?%';"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of the patients who have an appointment scheduled with a doctor who has prescribed medication to another patient with the same medical history diagnosis?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT DISTINCT pt.first_name, pt.last_name \nFROM Patients pt \nJOIN Appointments a ON pt.patient_id = a.patient_id \nJOIN Doctors d ON a.doctor_id = d.doctor_id \nJOIN Prescriptions pr ON d.doctor_id = pr.doctor_id \nWHERE a.status = 'Scheduled' \n AND pt.patient_id != pr.patient_id \n AND EXISTS (\n SELECT 1 \n FROM MedicalHistory mh \n WHERE mh.patient_id = pt.patient_id \n AND mh.diagnosis = (SELECT diagnosis FROM MedicalHistory WHERE patient_id = pr.patient_id)\n );"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the average coverage amount of insurance policies held by patients who have undergone a specific medical test and have a billing amount greater than a certain amount?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT AVG(ip.coverage_amount) \nFROM InsurancePolicies ip \nJOIN Patients p ON ip.patient_id = p.patient_id \nJOIN PatientTests pt ON p.patient_id = pt.patient_id \nJOIN Billing b ON p.patient_id = b.patient_id \nWHERE pt.test_id = ? AND b.total_amount > ?"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of the doctors who have prescribed medication to patients with a specific medical history diagnosis and have an appointment scheduled within the next 14 days?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT DISTINCT d.first_name, d.last_name \nFROM Doctors d \nJOIN Prescriptions p ON d.doctor_id = p.doctor_id \nJOIN MedicalHistory mh ON p.patient_id = mh.patient_id \nJOIN Appointments a ON p.patient_id = a.patient_id \nWHERE mh.diagnosis = ? \nAND a.appointment_date BETWEEN CURRENT_DATE AND DATE(CURRENT_DATE, '+14 days')\nAND a.status = 'Scheduled';"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the total number of patients who have undergone a medical test with a specific doctor and have a billing amount greater than a certain amount?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT COUNT(DISTINCT p.patient_id) \nFROM Patients p \nJOIN PatientTests pt ON p.patient_id = pt.patient_id \nJOIN Billing b ON p.patient_id = b.patient_id \nWHERE pt.doctor_id = ? AND b.total_amount > ?"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of the medications prescribed to patients who have an appointment scheduled with a specific doctor and have a medical history diagnosis that matches a specific condition?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT DISTINCT m.medication_name \nFROM Medications m \nJOIN PrescriptionDetails pd ON m.medication_id = pd.medication_id \nJOIN Prescriptions p ON pd.prescription_id = p.prescription_id \nJOIN Appointments a ON p.patient_id = a.patient_id \nJOIN MedicalHistory mh ON p.patient_id = mh.patient_id \nWHERE a.doctor_id = ? AND mh.diagnosis LIKE '%?%';"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the average billing amount of patients who have undergone a medical test with a specific doctor and have an outstanding balance?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "-- Calculate the average total amount of unpaid bills for a specific doctor\n-- who has performed medical tests on patients.\n\nSELECT AVG(b.total_amount) \nFROM Billing b \nJOIN PatientTests ptests ON b.patient_id = ptests.patient_id \nWHERE ptests.doctor_id = ? \n AND EXISTS (SELECT 1 FROM Doctors d WHERE d.doctor_id = ptests.doctor_id)\n AND b.paid = 0;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of the patients who have been prescribed medication by a specific doctor and have an appointment scheduled within the next 7 days?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT DISTINCT p.first_name, p.last_name \nFROM Patients p \nJOIN Prescriptions pr ON p.patient_id = pr.patient_id \nJOIN Doctors d ON pr.doctor_id = d.doctor_id \nJOIN Appointments a ON p.patient_id = a.patient_id \nWHERE d.doctor_id = ? \nAND a.appointment_date BETWEEN CURRENT_DATE AND DATE(CURRENT_DATE, '+7 days')\nAND a.status = 'Scheduled';"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the total coverage amount of insurance policies held by patients who have undergone a specific medical test and have a billing amount greater than a certain amount?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT SUM(ip.coverage_amount) \nFROM InsurancePolicies ip \nJOIN Patients p ON ip.patient_id = p.patient_id \nJOIN PatientTests pt ON p.patient_id = pt.patient_id \nJOIN Billing b ON p.patient_id = b.patient_id \nWHERE pt.test_id = ? AND b.total_amount > ?"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of the medications prescribed to patients who have a medical history diagnosis that matches a specific condition and are currently taking medication with a specific side effect?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT DISTINCT m.medication_name \nFROM Medications m \nJOIN PrescriptionDetails pd ON m.medication_id = pd.medication_id \nJOIN Prescriptions p ON pd.prescription_id = p.prescription_id \nJOIN MedicalHistory mh ON p.patient_id = mh.patient_id \nWHERE mh.diagnosis LIKE '%specific_condition%' \nAND m.side_effects LIKE '%specific_side_effect%';"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of the patients who have undergone a medical test with a specific doctor and have an outstanding balance?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT DISTINCT p.first_name, p.last_name \nFROM Patients p \nJOIN PatientTests pt ON p.patient_id = pt.patient_id \nJOIN Billing b ON p.patient_id = b.patient_id \nWHERE pt.doctor_id = ? AND b.paid = 0;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the average billing amount of patients who have been prescribed medication by a specific doctor and have an appointment scheduled within the next 14 days?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT AVG(b.total_amount) \nFROM Billing b \nJOIN Prescriptions p ON b.patient_id = p.patient_id \nJOIN Doctors d ON p.doctor_id = d.doctor_id \nJOIN Appointments a ON p.patient_id = a.patient_id \nWHERE d.doctor_id = ? \nAND a.appointment_date BETWEEN CURRENT_DATE AND DATE(CURRENT_DATE, '+14 days')\nAND a.status = 'Scheduled';"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of the medications prescribed to patients who have undergone a medical test with a specific doctor and have a medical history diagnosis that matches a specific condition?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT DISTINCT m.medication_name \nFROM Medications m \nJOIN PrescriptionDetails pd ON m.medication_id = pd.medication_id \nJOIN Prescriptions p ON pd.prescription_id = p.prescription_id \nJOIN PatientTests pt ON p.patient_id = pt.patient_id \nJOIN MedicalHistory mh ON p.patient_id = mh.patient_id \nWHERE pt.doctor_id = ? AND mh.diagnosis LIKE '%?%';"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the total number of patients who have been prescribed medication by a specific doctor and have an appointment scheduled within the next 30 days?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "DELIMITER //\nSELECT COUNT(DISTINCT patients.patient_id) \nFROM Appointments appointments \nJOIN Patients patients ON appointments.patient_id = patients.patient_id \nJOIN Prescriptions prescriptions ON patients.patient_id = prescriptions.patient_id \nJOIN Doctors doctors ON prescriptions.doctor_id = doctors.doctor_id \nWHERE (doctors.doctor_id = ?) \nAND (appointments.appointment_date BETWEEN CURRENT_DATE AND DATE(CURRENT_DATE, '+30 days'))\nAND (appointments.status = 'Scheduled')\nLIMIT 100;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of the patients who have undergone a medical test with a specific doctor and have a billing amount greater than a certain amount?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT DISTINCT p.first_name, p.last_name \nFROM Patients p \nJOIN PatientTests pt ON p.patient_id = pt.patient_id \nJOIN Billing b ON p.patient_id = b.patient_id \nWHERE pt.doctor_id = ? AND b.total_amount > ?"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the average coverage amount of insurance policies held by patients who have been prescribed medication by a specific doctor and have an appointment scheduled within the next 14 days?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT AVG(ip.coverage_amount) \nFROM InsurancePolicies ip \nJOIN Patients p ON ip.patient_id = p.patient_id \nJOIN Prescriptions pr ON p.patient_id = pr.patient_id \nJOIN Doctors d ON pr.doctor_id = d.doctor_id \nJOIN Appointments a ON p.patient_id = a.patient_id \nWHERE d.doctor_id = ? \nAND a.appointment_date BETWEEN CURRENT_DATE AND DATE(CURRENT_DATE, '+14 days')\nAND a.status = 'Scheduled';"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of the medications prescribed to patients who have undergone a medical test with a specific doctor and are currently taking medication with a specific side effect?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT DISTINCT m.medication_name \nFROM Medications m \nJOIN PrescriptionDetails pd ON m.medication_id = pd.medication_id \nJOIN Prescriptions p ON pd.prescription_id = p.prescription_id \nJOIN PatientTests pt ON p.patient_id = pt.patient_id \nWHERE pt.doctor_id = ? AND m.side_effects LIKE '%?%';"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the total billing amount of patients who have undergone a medical test with a specific doctor and have an outstanding balance?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "-- Get the total unpaid amount for a specific doctor\nSELECT SUM(bill.total_amount) \nFROM Billing bill \nJOIN Appointments appt ON bill.appointment_id = appt.appointment_id \nWHERE appt.doctor_id = ? AND bill.paid = 0;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of the patients who have been prescribed medication by a specific doctor and have a medical history diagnosis that matches a specific condition?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT DISTINCT p.first_name, p.last_name \nFROM Patients p \nJOIN Prescriptions pr ON p.patient_id = pr.patient_id \nJOIN Doctors d ON pr.doctor_id = d.doctor_id \nJOIN MedicalHistory mh ON p.patient_id = mh.patient_id \nWHERE d.doctor_id = ? AND mh.diagnosis LIKE '%?%';"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the average billing amount of patients who have undergone a medical test with a specific doctor and have a billing amount greater than a certain amount?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "-- Calculate the average billing amount of patients who have undergone a medical test with a specific doctor and have a billing amount greater than a certain amount.\nSELECT AVG(b.total_amount) AS average_total_amount\nFROM Billing b\nJOIN PatientTests pt ON b.patient_id = pt.patient_id\nWHERE pt.doctor_id = ? AND b.total_amount > ?"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of high-risk patients who have been diagnosed with a disease that requires a specific medication and have not taken the prescribed medication as directed, and also have a history of not attending scheduled appointments?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT DISTINCT p.first_name, p.last_name\nFROM Patients p\nJOIN MedicalHistory mh ON p.patient_id = mh.patient_id\nJOIN Prescriptions pr ON p.patient_id = pr.patient_id\nJOIN PrescriptionDetails pd ON pr.prescription_id = pd.prescription_id\nJOIN Medications m ON pd.medication_id = m.medication_id\nJOIN Appointments a ON p.patient_id = a.patient_id\nWHERE mh.diagnosis IN ('Diabetes', 'Hypertension', 'Heart Disease') \nAND m.medication_name IN ('Metformin', 'Lisinopril', 'Atorvastatin')\nAND pd.dosage NOT IN ('1000mg', '20mg', '40mg')\nAND a.status = 'Scheduled'\nAND a.appointment_date < CURRENT_DATE"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: Which high-risk patients have a history of missing scheduled appointments and also have a high number of medical tests with abnormal results?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT DISTINCT p.first_name, p.last_name\nFROM Patients p\nJOIN Appointments a ON p.patient_id = a.patient_id\nJOIN PatientTests pt ON p.patient_id = pt.patient_id\nWHERE a.status = 'Scheduled' AND a.appointment_date < CURRENT_DATE AND pt.results = 'Abnormal'\nGROUP BY p.patient_id\nHAVING COUNT(DISTINCT a.appointment_id) > 2 AND COUNT(pt.patient_test_id) > 5"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of high-risk patients who have been prescribed a specific medication but have not taken it as directed, and also have a history of hospitalization due to non-adherence to medication?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT DISTINCT p.first_name, p.last_name\nFROM Patients p\nJOIN Prescriptions pr ON p.patient_id = pr.patient_id\nJOIN PrescriptionDetails pd ON pr.prescription_id = pd.prescription_id\nJOIN Medications m ON pd.medication_id = m.medication_id\nJOIN MedicalHistory mh ON p.patient_id = mh.patient_id\nWHERE m.medication_name = 'Lisinopril' \n AND pd.dosage <> '20mg' \n AND mh.diagnosis = 'Hypertension'\n AND mh.treatment LIKE '%Hospitalization%';"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: Which high-risk patients have a history of not attending scheduled appointments and also have a high number of billing records with outstanding balances?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT p.first_name, p.last_name, p.phone, p.email\nFROM Patients p\nJOIN Appointments a ON p.patient_id = a.patient_id\nJOIN Billing b ON p.patient_id = b.patient_id\nWHERE a.status = 'Scheduled' AND a.appointment_date < DATE('now') AND b.paid = 0\nGROUP BY p.patient_id\nHAVING COUNT(DISTINCT a.appointment_id) > 2 AND COUNT(DISTINCT b.billing_id) > 3;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of high-risk patients who have been diagnosed with a disease that requires a specific medication and have not taken the prescribed medication as directed, and also have a history of not attending scheduled appointments?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT DISTINCT p.first_name, p.last_name\nFROM Patients p\nJOIN MedicalHistory mh ON p.patient_id = mh.patient_id\nJOIN Prescriptions pr ON p.patient_id = pr.patient_id\nJOIN PrescriptionDetails pd ON pr.prescription_id = pd.prescription_id\nJOIN Medications m ON pd.medication_id = m.medication_id\nJOIN Appointments a ON p.patient_id = a.patient_id\nWHERE mh.diagnosis IN ('Hyperlipidemia', 'Hypertension', 'Diabetes')\nAND m.medication_name IN ('Atorvastatin', 'Lisinopril', 'Metformin')\nAND a.status = 'Scheduled'"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: Which high-risk patients have a history of missing scheduled appointments and also have a high number of medical tests with abnormal results, and have a specific insurance policy?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "WHERE \n (a.status = 'Scheduled' AND a.appointment_date < :reference_date)\n OR a.status IN ('Canceled', 'No-Show')"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of high-risk patients who have been prescribed a specific medication but have not taken it as directed, and also have a history of hospitalization due to non-adherence to medication, and have a specific insurance policy?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT DISTINCT pat.first_name, pat.last_name \nFROM Patients pat \nJOIN MedicalHistory med_hist ON pat.patient_id = med_hist.patient_id \nJOIN Prescriptions pres ON pat.patient_id = pres.patient_id \nJOIN PrescriptionDetails pres_det ON pres.prescription_id = pres_det.prescription_id \nJOIN Medications meds ON pres_det.medication_id = meds.medication_id \nJOIN InsurancePolicies ins_pol ON pat.patient_id = ins_pol.patient_id \nWHERE meds.medication_name = 'Lisinopril' \nAND pres_det.dosage <> '20mg' \nAND med_hist.diagnosis = 'Hypertension' \nAND ins_pol.policy_number = '1234567890'\nAND med_hist.treatment LIKE 'hospitalization%';"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: Which high-risk patients have a history of not attending scheduled appointments and also have a high number of billing records with outstanding balances, and have a specific insurance policy?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n p.first_name, \n p.last_name, \n COUNT(b.billing_id) AS outstanding_bills\nFROM \n Patients p\n JOIN Billing b ON p.patient_id = b.patient_id\n JOIN InsurancePolicies ip ON p.patient_id = ip.patient_id\nWHERE \n b.paid = 0 \n AND ip.policy_number = '1234567890'\nGROUP BY \n p.first_name, \n p.last_name\nHAVING \n COUNT(b.billing_id) > 3\nORDER BY \n outstanding_bills DESC;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of high-risk patients who have been diagnosed with a disease that requires a specific medication and have not taken the prescribed medication as directed, and also have a history of not attending scheduled appointments, and have a specific insurance policy?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT DISTINCT p.first_name, p.last_name \nFROM Patients p \nJOIN MedicalHistory mh ON p.patient_id = mh.patient_id \nJOIN Prescriptions pr ON p.patient_id = pr.patient_id \nJOIN PrescriptionDetails pd ON pr.prescription_id = pd.prescription_id \nJOIN Medications m ON pd.medication_id = m.medication_id \nJOIN Appointments a ON p.patient_id = a.patient_id \nJOIN InsurancePolicies ip ON p.patient_id = ip.patient_id \nWHERE mh.diagnosis = 'Hyperlipidemia' \n AND m.medication_name = 'Atorvastatin' \n AND pd.dosage <> '40mg' \n AND a.status = 'Scheduled' \n AND ip.policy_number = '1234567890'\n AND p.patient_id IN (\n SELECT patient_id \n FROM (\n SELECT patient_id, \n ROW_NUMBER() OVER (PARTITION BY patient_id ORDER BY appointment_id) AS row_num\n FROM Appointments\n WHERE status = 'Scheduled'\n ) AS subquery\n WHERE row_num > 1\n )"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: Which high-risk patients have a history of missing scheduled appointments and also have a high number of medical tests with abnormal results, and have a specific insurance policy, and are male?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT patients.first_name, patients.last_name\nFROM Patients patients\nJOIN Appointments appointments ON patients.patient_id = appointments.patient_id\nJOIN PatientTests patient_tests ON patients.patient_id = patient_tests.patient_id\nJOIN InsurancePolicies insurance_policies ON patients.patient_id = insurance_policies.patient_id\nWHERE appointments.status = 'Scheduled' AND appointments.appointment_date < '2022-01-01' AND patient_tests.results = 'Abnormal' AND insurance_policies.policy_number = '1234567890' AND patients.gender = 'Male'\nGROUP BY patients.patient_id\nHAVING COUNT(patient_tests.patient_test_id) > 5 AND COUNT(appointments.appointment_id) > 3;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of high-risk patients who have been prescribed a specific medication but have not taken it as directed, and also have a history of hospitalization due to non-adherence to medication, and have a specific insurance policy, and are female?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT patients.first_name, patients.last_name \nFROM Patients patients \nJOIN Prescriptions prescriptions ON patients.patient_id = prescriptions.patient_id \nJOIN PrescriptionDetails prescription_details ON prescriptions.prescription_id = prescription_details.prescription_id \nJOIN Medications medications ON prescription_details.medication_id = medications.medication_id \nWHERE medications.medication_name = 'Lisinopril' \nAND prescription_details.dosage <> '20mg' \nAND patients.gender = 'Female'\nAND EXISTS (\n SELECT 1 \n FROM MedicalHistory \n WHERE patient_id = patients.patient_id \n AND diagnosis = 'Hypertension'\n)\nAND EXISTS (\n SELECT 1 \n FROM InsurancePolicies \n WHERE patient_id = patients.patient_id \n AND policy_number = '1234567890'\n);"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: Which high-risk patients have a history of not attending scheduled appointments and also have a high number of billing records with outstanding balances, and have a specific insurance policy, and are male?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT p.first_name, p.last_name\nFROM Patients p\nJOIN Appointments a ON p.patient_id = a.patient_id\nJOIN Billing b ON p.patient_id = b.patient_id\nWHERE a.status = 'Scheduled'\n AND b.paid = 0\n AND p.gender = 'Male'\n AND EXISTS (\n SELECT 1\n FROM InsurancePolicies ip\n WHERE ip.patient_id = p.patient_id\n AND ip.policy_number = '1234567890'\n )\nGROUP BY p.patient_id\nHAVING COUNT(b.billing_id) > 3 AND COUNT(a.appointment_id) > 2"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of high-risk patients who have been diagnosed with a disease that requires a specific medication and have not taken the prescribed medication as directed, and also have a history of not attending scheduled appointments, and have a specific insurance policy, and are female?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT DISTINCT p.first_name, p.last_name \nFROM Patients p \nJOIN MedicalHistory mh ON p.patient_id = mh.patient_id \nJOIN Prescriptions pr ON p.patient_id = pr.patient_id \nJOIN PrescriptionDetails pd ON pr.prescription_id = pd.prescription_id \nJOIN Medications m ON pd.medication_id = m.medication_id \nWHERE mh.diagnosis = 'Hyperlipidemia' \nAND m.medication_name = 'Atorvastatin' \nAND pd.dosage <> '40mg' \nAND p.gender = 'Female'\nAND EXISTS (\n SELECT 1 \n FROM InsurancePolicies ip \n WHERE ip.patient_id = p.patient_id \n AND ip.policy_number = '1234567890'\n)"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: Which high-risk patients have a history of missing scheduled appointments and also have a high number of medical tests with abnormal results, and have a specific insurance policy, and are between 40 and 60 years old?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "-- Retrieve high-risk patients with a history of missing scheduled appointments, \n-- a high number of medical tests with abnormal results, a specific insurance policy, \n-- and are between 40 and 60 years old.\n\nSELECT patients.first_name, patients.last_name\nFROM Patients patients\nJOIN Appointments appointments ON patients.patient_id = appointments.patient_id\nJOIN PatientTests patient_tests ON patients.patient_id = patient_tests.patient_id\nJOIN InsurancePolicies insurance_policies ON patients.patient_id = insurance_policies.patient_id\nWHERE appointments.status = 'Scheduled' AND appointments.appointment_date < CURRENT_DATE \n AND patient_tests.results = 'Abnormal' AND insurance_policies.policy_number = '1234567890' \n AND patients.birth_date BETWEEN '1970-01-01' AND '1990-01-01'\nGROUP BY patients.patient_id\nHAVING COUNT(patient_tests.patient_test_id) > 5 AND COUNT(appointments.appointment_id) > 3"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of high-risk patients who have been prescribed a specific medication but have not taken it as directed, and also have a history of hospitalization due to non-adherence to medication, and have a specific insurance policy, and are between 40 and 60 years old?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT pt.first_name, pt.last_name \nFROM Patients pt \nJOIN Prescriptions pr ON pt.patient_id = pr.patient_id \nJOIN PrescriptionDetails pd ON pr.prescription_id = pd.prescription_id \nJOIN Medications m ON pd.medication_id = m.medication_id \nWHERE m.medication_name = 'Lisinopril' \nAND pd.dosage <> '20mg' \nAND pt.birth_date BETWEEN '1960-01-01' AND '1980-01-01'\nAND DATE('now') - pt.birth_date BETWEEN 40 AND 60"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: Which high-risk patients have a history of not attending scheduled appointments and also have a high number of billing records with outstanding balances, and have a specific insurance policy, and are between 40 and 60 years old?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT DISTINCT pat.first_name, pat.last_name\nFROM Patients pat\nJOIN Appointments app ON pat.patient_id = app.patient_id\nJOIN Billing bill ON pat.patient_id = bill.patient_id\nWHERE app.status = 'Scheduled' AND app.appointment_date < CURRENT_DATE AND bill.paid = 0\n AND pat.patient_id IN (SELECT patient_id FROM InsurancePolicies WHERE policy_number = '1234567890')\n AND pat.birth_date BETWEEN '1960-01-01' AND '1980-01-01'\n AND pat.patient_id IN (SELECT patient_id FROM Billing GROUP BY patient_id HAVING COUNT(billing_id) > 3)\n AND pat.patient_id IN (SELECT patient_id FROM Appointments GROUP BY patient_id HAVING COUNT(appointment_id) > 2);"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of high-risk patients who have been diagnosed with a disease that requires a specific medication and have not taken the prescribed medication as directed, and also have a history of not attending scheduled appointments, and have a specific insurance policy, and are between 40 and 60 years old?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT DISTINCT p.first_name, p.last_name \nFROM Patients p \nJOIN MedicalHistory mh ON p.patient_id = mh.patient_id \nJOIN Prescriptions pr ON p.patient_id = pr.patient_id \nJOIN PrescriptionDetails pd ON pr.prescription_id = pd.prescription_id \nJOIN Medications m ON pd.medication_id = m.medication_id \nJOIN Appointments a ON p.patient_id = a.patient_id \nWHERE mh.diagnosis = 'Hyperlipidemia' \nAND m.medication_name = 'Atorvastatin' \nAND pd.dosage <> '40mg' \nAND a.status = 'Scheduled' \nAND a.appointment_date < CURRENT_DATE\nAND p.birth_date >= '1960-01-01' AND p.birth_date <= '1980-01-01'\nAND EXISTS (\n SELECT 1 \n FROM InsurancePolicies ip \n WHERE ip.patient_id = p.patient_id AND ip.policy_number = '1234567890'\n)"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: Which high-risk patients have a history of missing scheduled appointments and also have a high number of medical tests with abnormal results, and have a specific insurance policy, and have a history of hospitalization due to non-adherence to medication?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT p.first_name, p.last_name\nFROM Patients p\nINNER JOIN Appointments a ON p.patient_id = a.patient_id\nINNER JOIN PatientTests pt ON p.patient_id = pt.patient_id\nINNER JOIN InsurancePolicies ip ON p.patient_id = ip.patient_id\nINNER JOIN MedicalHistory mh ON p.patient_id = mh.patient_id\nWHERE a.status = 'Missed' AND pt.results = 'Abnormal' AND ip.policy_number = '1234567890' AND mh.diagnosis = 'Non-adherence to medication'\nGROUP BY p.patient_id\nHAVING COUNT(a.appointment_id) > 3 AND COUNT(pt.patient_test_id) > 5 AND COUNT(mh.history_id) > 1\nORDER BY COUNT(pt.patient_test_id) DESC;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of high-risk patients who have been prescribed a specific medication but have not taken it as directed, and also have a history of hospitalization due to non-adherence to medication, and have a specific insurance policy, and have a history of missing scheduled appointments?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT DISTINCT p.first_name, p.last_name \nFROM Patients p \nJOIN Prescriptions pr ON p.patient_id = pr.patient_id \nJOIN PrescriptionDetails pd ON pr.prescription_id = pd.prescription_id \nJOIN Medications m ON pd.medication_id = m.medication_id \nJOIN MedicalHistory mh ON p.patient_id = mh.patient_id \nWHERE m.medication_name = 'Lisinopril' \nAND pd.dosage <> '20mg' \nAND mh.diagnosis = 'Hypertension' \nAND p.patient_id IN (\n SELECT patient_id \n FROM InsurancePolicies \n WHERE policy_number = '1234567890'\n)\nAND p.patient_id IN (\n SELECT patient_id \n FROM Appointments \n WHERE status = 'Scheduled' \n AND appointment_date < CURRENT_DATE\n);"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: Which high-risk patients have a history of not attending scheduled appointments and also have a high number of billing records with outstanding balances, and have a specific insurance policy, and have a history of hospitalization due to non-adherence to medication?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n p.first_name, \n p.last_name\nFROM \n Patients p\n JOIN Appointments a ON p.patient_id = a.patient_id\n JOIN Billing b ON p.patient_id = b.patient_id\n JOIN MedicalHistory mh ON p.patient_id = mh.patient_id\nWHERE \n a.status IN ('Scheduled', 'Completed', 'Canceled') \n AND b.paid = 0 \n AND mh.diagnosis = 'Hypertension'\n AND mh.treatment LIKE '%hospitalization due to non-adherence to medication%'\n AND p.patient_id IN (SELECT patient_id FROM InsurancePolicies WHERE policy_number = '1234567890')\n AND (SELECT COUNT(*) FROM Billing WHERE patient_id = p.patient_id AND paid = 0) > 3\n AND (SELECT COUNT(*) FROM Appointments WHERE patient_id = p.patient_id AND status = 'Scheduled') > 2"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of high-risk patients who have been diagnosed with a disease that requires a specific medication and have not taken the prescribed medication as directed, and also have a history of not attending scheduled appointments, and have a specific insurance policy, and have a history of hospitalization due to non-adherence to medication?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT p.first_name, p.last_name\nFROM Patients p\nJOIN MedicalHistory mh ON p.patient_id = mh.patient_id\nJOIN Prescriptions pr ON p.patient_id = pr.patient_id\nJOIN PrescriptionDetails pd ON pr.prescription_id = pd.prescription_id\nJOIN Medications m ON pd.medication_id = m.medication_id\nJOIN InsurancePolicies ip ON p.patient_id = ip.patient_id\nWHERE mh.diagnosis IN ('Hyperlipidemia', 'Hypertension')\n AND m.medication_name = 'Atorvastatin'\n AND pd.dosage <> '40mg'\n AND ip.policy_number = '1234567890'\n AND EXISTS (\n SELECT 1\n FROM MedicalHistory mh2\n WHERE mh2.patient_id = p.patient_id\n AND mh2.diagnosis = 'Hospitalization due to non-adherence to medication'\n )"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the average number of medical tests performed per patient, considering only patients who have undergone at least 3 tests, and what is the total number of tests performed by each doctor, considering only doctors who have performed tests for more than 5 patients?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "WITH patient_test_counts AS (\n SELECT pt.patient_id, COUNT(*) AS test_count\n FROM PatientTests pt\n GROUP BY pt.patient_id\n HAVING COUNT(*) >= 3\n),\ndoctor_test_counts AS (\n SELECT pt.doctor_id, COUNT(DISTINCT pt.patient_id) AS patient_count\n FROM PatientTests pt\n GROUP BY pt.doctor_id\n HAVING COUNT(DISTINCT pt.patient_id) > 5\n)\nSELECT \n AVG(ptc.test_count) AS avg_tests_per_patient,\n dtc.doctor_id,\n COUNT(DISTINCT ptc.patient_id) AS total_patients_per_doctor\nFROM doctor_test_counts dtc\nJOIN PatientTests pt ON dtc.doctor_id = pt.doctor_id\nJOIN patient_test_counts ptc ON pt.patient_id = ptc.patient_id\nGROUP BY dtc.doctor_id;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: Which medications have been prescribed to patients with a diagnosis of 'Diabetes' and what is the total number of prescriptions for each medication?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n m.medication_name, \n COUNT(*) AS total_prescriptions \nFROM \n Medications AS m \nJOIN \n PrescriptionDetails AS pd ON m.medication_id = pd.medication_id \nJOIN \n Prescriptions AS p ON pd.prescription_id = p.prescription_id \nJOIN \n MedicalHistory AS mh ON p.patient_id = mh.patient_id \nWHERE \n mh.diagnosis = 'Diabetes' \nGROUP BY \n m.medication_name;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the total amount billed to patients who have undergone a medical test with a result of 'Positive' and what is the average amount billed per patient?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n SUM(b.total_amount) AS total_billed, \n AVG(b.total_amount) AS avg_billed_per_patient \nFROM \n Billing AS b \nJOIN \n PatientTests AS pt ON b.patient_id = pt.patient_id \nWHERE \n pt.results = 'Positive';"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: Which doctors have performed medical tests for patients with a diagnosis of 'Hypertension' and what is the total number of tests performed by each doctor?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n d.doctor_id, \n COUNT(*) AS total_tests \nFROM \n Doctors AS d \nJOIN \n PatientTests AS pt ON d.doctor_id = pt.doctor_id \nJOIN \n MedicalHistory AS mh ON pt.patient_id = mh.patient_id \nWHERE \n mh.diagnosis = 'Hypertension' \nGROUP BY \n d.doctor_id;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the total number of medical tests performed by each doctor, considering only doctors who have performed tests for more than 5 patients, and what is the average number of tests performed per patient?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n d.doctor_id, \n COUNT(*) AS total_tests, \n AVG(test_count) AS avg_tests_per_patient \nFROM \n Doctors AS d \nJOIN \n (SELECT \n doctor_id, \n patient_id, \n COUNT(*) AS test_count \n FROM \n PatientTests \n GROUP BY \n doctor_id, \n patient_id) AS pt \nON \n d.doctor_id = pt.doctor_id \nGROUP BY \n d.doctor_id \nHAVING \n COUNT(DISTINCT pt.patient_id) > 5;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: Which medications have been prescribed to patients with a diagnosis of 'Asthma' and what is the total number of prescriptions for each medication?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n m.medication_name, \n COUNT(*) AS total_prescriptions \nFROM \n Medications AS m \nJOIN \n PrescriptionDetails AS pd ON m.medication_id = pd.medication_id \nJOIN \n Prescriptions AS p ON pd.prescription_id = p.prescription_id \nJOIN \n MedicalHistory AS mh ON p.patient_id = mh.patient_id \nWHERE \n mh.diagnosis = 'Asthma' \nGROUP BY \n m.medication_name;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the total amount billed to patients who have undergone a medical test with a result of 'Negative' and what is the average amount billed per patient?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n SUM(b.total_amount) AS total_billed, \n AVG(b.total_amount) AS avg_billed_per_patient \nFROM \n Billing AS b \nJOIN \n PatientTests AS pt ON b.patient_id = pt.patient_id \nWHERE \n pt.results = 'Negative';"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: Which doctors have performed medical tests for patients with a diagnosis of 'Diabetes' and what is the total number of tests performed by each doctor?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n D.first_name, \n D.last_name, \n COUNT(DISTINCT PT.patient_test_id) AS total_tests\nFROM \n Doctors D\nLEFT JOIN \n PatientTests PT ON D.doctor_id = PT.doctor_id\nLEFT JOIN \n MedicalHistory MH ON PT.patient_id = MH.patient_id\nWHERE \n MH.diagnosis = 'Diabetes'\nGROUP BY \n D.first_name, D.last_name\nORDER BY \n total_tests DESC;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the total number of medical tests performed by each doctor, considering only doctors who have performed tests for more than 5 patients, and what is the average number of tests performed per patient?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n d.doctor_id, \n COUNT(*) AS total_tests, \n AVG(test_count) AS avg_tests_per_patient \nFROM \n Doctors AS d \nJOIN \n (SELECT \n doctor_id, \n patient_id, \n COUNT(*) AS test_count \n FROM \n PatientTests \n GROUP BY \n doctor_id, \n patient_id) AS pt \nON \n d.doctor_id = pt.doctor_id \nGROUP BY \n d.doctor_id \nHAVING \n COUNT(DISTINCT pt.patient_id) > 5;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: Which medications have been prescribed to patients with a diagnosis of 'Hypertension' and what is the total number of prescriptions for each medication?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n m.medication_name, \n COUNT(*) AS total_prescriptions \nFROM \n Medications AS m \nJOIN \n PrescriptionDetails AS pd ON m.medication_id = pd.medication_id \nJOIN \n Prescriptions AS p ON pd.prescription_id = p.prescription_id \nJOIN \n MedicalHistory AS mh ON p.patient_id = mh.patient_id \nWHERE \n mh.diagnosis = 'Hypertension' \nGROUP BY \n m.medication_name;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the total amount billed to patients who have undergone a medical test with a result of 'Positive' and what is the average amount billed per patient?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n SUM(b.total_amount) AS total_billed, \n AVG(b.total_amount) AS avg_billed_per_patient \nFROM \n Billing AS b \nJOIN \n PatientTests AS pt ON b.patient_id = pt.patient_id \nWHERE \n pt.results = 'Positive';"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: Which doctors have performed medical tests for patients with a diagnosis of 'Asthma' and what is the total number of tests performed by each doctor?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n d.doctor_id, \n COUNT(*) AS total_tests \nFROM \n Doctors AS d \nJOIN \n PatientTests AS pt ON d.doctor_id = pt.doctor_id \nJOIN \n MedicalHistory AS mh ON pt.patient_id = mh.patient_id \nWHERE \n mh.diagnosis = 'Asthma' \nGROUP BY \n d.doctor_id;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the total number of medical tests performed by each doctor, considering only doctors who have performed tests for more than 5 patients, and what is the average number of tests performed per patient?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n d.doctor_id, \n COUNT(*) AS total_tests, \n AVG(test_count) AS avg_tests_per_patient \nFROM \n Doctors AS d \nJOIN \n (SELECT \n doctor_id, \n patient_id, \n COUNT(*) AS test_count \n FROM \n PatientTests \n GROUP BY \n doctor_id, \n patient_id) AS pt \nON \n d.doctor_id = pt.doctor_id \nGROUP BY \n d.doctor_id \nHAVING \n COUNT(DISTINCT pt.patient_id) > 5;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: Which medications have been prescribed to patients with a diagnosis of 'Diabetes' and what is the total number of prescriptions for each medication?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n m.medication_name, \n COUNT(*) AS total_prescriptions \nFROM \n Medications AS m \nJOIN \n PrescriptionDetails AS pd ON m.medication_id = pd.medication_id \nJOIN \n Prescriptions AS p ON pd.prescription_id = p.prescription_id \nJOIN \n MedicalHistory AS mh ON p.patient_id = mh.patient_id \nWHERE \n mh.diagnosis = 'Diabetes' \nGROUP BY \n m.medication_name;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the total amount billed to patients who have undergone a medical test with a result of 'Negative' and what is the average amount billed per patient?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n SUM(b.total_amount) AS total_billed, \n AVG(b.total_amount) AS avg_billed_per_patient \nFROM \n Billing AS b \nJOIN \n PatientTests AS pt ON b.patient_id = pt.patient_id \nWHERE \n pt.results = 'Negative';"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: Which doctors have performed medical tests for patients with a diagnosis of 'Hypertension' and what is the total number of tests performed by each doctor?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n d.doctor_id, \n COUNT(*) AS total_tests \nFROM \n Doctors AS d \nJOIN \n PatientTests AS pt ON d.doctor_id = pt.doctor_id \nJOIN \n MedicalHistory AS mh ON pt.patient_id = mh.patient_id \nWHERE \n mh.diagnosis = 'Hypertension' \nGROUP BY \n d.doctor_id;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the total number of medical tests performed by each doctor, considering only doctors who have performed tests for more than 5 patients, and what is the average number of tests performed per patient?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n d.doctor_id, \n COUNT(*) AS total_tests, \n AVG(test_count) AS avg_tests_per_patient \nFROM \n Doctors AS d \nJOIN \n (SELECT \n doctor_id, \n patient_id, \n COUNT(*) AS test_count \n FROM \n PatientTests \n GROUP BY \n doctor_id, \n patient_id) AS pt \nON \n d.doctor_id = pt.doctor_id \nGROUP BY \n d.doctor_id \nHAVING \n COUNT(DISTINCT pt.patient_id) > 5;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: Which medications have been prescribed to patients with a diagnosis of 'Asthma' and what is the total number of prescriptions for each medication?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n m.medication_name, \n COUNT(*) AS total_prescriptions \nFROM \n Medications AS m \nJOIN \n PrescriptionDetails AS pd ON m.medication_id = pd.medication_id \nJOIN \n Prescriptions AS p ON pd.prescription_id = p.prescription_id \nJOIN \n MedicalHistory AS mh ON p.patient_id = mh.patient_id \nWHERE \n mh.diagnosis = 'Asthma' \nGROUP BY \n m.medication_name;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the total amount billed to patients who have undergone a medical test with a result of 'Positive' and what is the average amount billed per patient?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n SUM(b.total_amount) AS total_billed, \n AVG(b.total_amount) AS avg_billed_per_patient \nFROM \n Billing AS b \nJOIN \n PatientTests AS pt ON b.patient_id = pt.patient_id \nWHERE \n pt.results = 'Positive';"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: Which doctors have performed medical tests for patients with a diagnosis of 'Diabetes' and what is the total number of tests performed by each doctor?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n D.first_name, \n D.last_name, \n COUNT(DISTINCT PT.patient_test_id) AS total_tests\nFROM \n Doctors D\nLEFT JOIN \n PatientTests PT ON D.doctor_id = PT.doctor_id\nLEFT JOIN \n MedicalHistory MH ON PT.patient_id = MH.patient_id\nWHERE \n MH.diagnosis = 'Diabetes'\nGROUP BY \n D.first_name, D.last_name\nORDER BY \n total_tests DESC;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the total number of medical tests performed by each doctor, considering only doctors who have performed tests for more than 5 patients, and what is the average number of tests performed per patient?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n d.doctor_id, \n COUNT(*) AS total_tests, \n AVG(test_count) AS avg_tests_per_patient \nFROM \n Doctors AS d \nJOIN \n (SELECT \n doctor_id, \n patient_id, \n COUNT(*) AS test_count \n FROM \n PatientTests \n GROUP BY \n doctor_id, \n patient_id) AS pt \nON \n d.doctor_id = pt.doctor_id \nGROUP BY \n d.doctor_id \nHAVING \n COUNT(DISTINCT pt.patient_id) > 5;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: Which medications have been prescribed to patients with a diagnosis of 'Hypertension' and what is the total number of prescriptions for each medication?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n m.medication_name, \n COUNT(*) AS total_prescriptions \nFROM \n Medications AS m \nJOIN \n PrescriptionDetails AS pd ON m.medication_id = pd.medication_id \nJOIN \n Prescriptions AS p ON pd.prescription_id = p.prescription_id \nJOIN \n MedicalHistory AS mh ON p.patient_id = mh.patient_id \nWHERE \n mh.diagnosis = 'Hypertension' \nGROUP BY \n m.medication_name;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the total amount billed to patients who have undergone a medical test with a result of 'Negative' and what is the average amount billed per patient?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n SUM(b.total_amount) AS total_billed, \n AVG(b.total_amount) AS avg_billed_per_patient \nFROM \n Billing AS b \nJOIN \n PatientTests AS pt ON b.patient_id = pt.patient_id \nWHERE \n pt.results = 'Negative';"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: Which doctors have performed medical tests for patients with a diagnosis of 'Asthma' and what is the total number of tests performed by each doctor?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n d.doctor_id, \n COUNT(*) AS total_tests \nFROM \n Doctors AS d \nJOIN \n PatientTests AS pt ON d.doctor_id = pt.doctor_id \nJOIN \n MedicalHistory AS mh ON pt.patient_id = mh.patient_id \nWHERE \n mh.diagnosis = 'Asthma' \nGROUP BY \n d.doctor_id;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the total number of medical tests performed by each doctor, considering only doctors who have performed tests for more than 5 patients, and what is the average number of tests performed per patient?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n d.doctor_id, \n COUNT(*) AS total_tests, \n AVG(test_count) AS avg_tests_per_patient \nFROM \n Doctors AS d \nJOIN \n (SELECT \n doctor_id, \n patient_id, \n COUNT(*) AS test_count \n FROM \n PatientTests \n GROUP BY \n doctor_id, \n patient_id) AS pt \nON \n d.doctor_id = pt.doctor_id \nGROUP BY \n d.doctor_id \nHAVING \n COUNT(DISTINCT pt.patient_id) > 5;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of the patients who have undergone a medical test with a result of 'Positive' and have a medical history diagnosis of 'Diabetes', and what are the names of the medications prescribed to these patients?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT DISTINCT p.first_name, p.last_name, m.medication_name\nFROM Patients p\nINNER JOIN MedicalHistory mh ON p.patient_id = mh.patient_id\nINNER JOIN PatientTests pt ON p.patient_id = pt.patient_id\nINNER JOIN Prescriptions pr ON p.patient_id = pr.patient_id\nINNER JOIN PrescriptionDetails pd ON pr.prescription_id = pd.prescription_id\nINNER JOIN Medications m ON pd.medication_id = m.medication_id\nWHERE pt.results = 'Positive' AND mh.diagnosis = 'Diabetes'"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of the doctors who have prescribed medications to patients with a medical history diagnosis of 'Hypertension' and have also conducted medical tests with a result of 'Negative'?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT DISTINCT d.first_name, d.last_name \nFROM Doctors d \nJOIN Prescriptions pr ON d.doctor_id = pr.doctor_id \nJOIN Patients p ON pr.patient_id = p.patient_id \nJOIN MedicalHistory mh ON p.patient_id = mh.patient_id \nJOIN PatientTests pt ON p.patient_id = pt.patient_id AND pt.doctor_id = d.doctor_id\nWHERE mh.diagnosis = 'Hypertension' AND pt.results = 'Negative';"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of the patients who have undergone a medical test with a result of 'Positive' and have a medical history diagnosis of 'Diabetes', and what are the names of the medications prescribed to these patients, along with the dosage and frequency?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT DISTINCT p.first_name, p.last_name, m.medication_name, pd.dosage, pd.frequency\nFROM Patients p\nJOIN Prescriptions pr ON p.patient_id = pr.patient_id\nJOIN PrescriptionDetails pd ON pr.prescription_id = pd.prescription_id\nJOIN Medications m ON pd.medication_id = m.medication_id\nWHERE p.patient_id IN (\n SELECT patient_id FROM MedicalHistory WHERE diagnosis = 'Diabetes'\n) AND p.patient_id IN (\n SELECT patient_id FROM PatientTests WHERE results = 'Positive'\n)"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of the doctors who have prescribed medications to patients with a medical history diagnosis of 'Hypertension' and have also conducted medical tests with a result of 'Negative', along with the names of the medications prescribed?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT DISTINCT d.first_name, d.last_name, m.medication_name\nFROM Doctors d\nJOIN Prescriptions pr ON d.doctor_id = pr.doctor_id\nJOIN Patients p ON pr.patient_id = p.patient_id\nJOIN MedicalHistory mh ON p.patient_id = mh.patient_id\nJOIN PatientTests pt ON p.patient_id = pt.patient_id AND pt.doctor_id = d.doctor_id\nJOIN PrescriptionDetails pd ON pr.prescription_id = pd.prescription_id\nJOIN Medications m ON pd.medication_id = m.medication_id\nWHERE mh.diagnosis = 'Hypertension' AND pt.results = 'Negative'"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of the patients who have undergone a medical test with a result of 'Positive' and have a medical history diagnosis of 'Diabetes', and what are the names of the medications prescribed to these patients, along with the dosage and frequency, and the names of the doctors who prescribed the medications?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n patients.first_name, \n patients.last_name, \n medications.medication_name, \n prescription_details.dosage, \n prescription_details.frequency\nFROM \n Patients patients\n JOIN MedicalHistory medical_history ON patients.patient_id = medical_history.patient_id\n JOIN Prescriptions prescriptions ON patients.patient_id = prescriptions.patient_id\n JOIN PrescriptionDetails prescription_details ON prescriptions.prescription_id = prescription_details.prescription_id\n JOIN Medications medications ON prescription_details.medication_id = medications.medication_id\nWHERE \n medical_history.diagnosis = 'Diabetes'"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of the doctors who have prescribed medications to patients with a medical history diagnosis of 'Hypertension' and have also conducted medical tests with a result of 'Negative', along with the names of the medications prescribed, and the names of the patients who were prescribed the medications?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n d.first_name, \n d.last_name, \n m.medication_name, \n p.first_name, \n p.last_name\nFROM \n Doctors AS d\n JOIN Prescriptions AS pr ON d.doctor_id = pr.doctor_id\n JOIN Patients AS p ON pr.patient_id = p.patient_id\n JOIN PrescriptionDetails AS pd ON pr.prescription_id = pd.prescription_id\n JOIN Medications AS m ON pd.medication_id = m.medication_id\nWHERE \n p.patient_id IN (\n SELECT patient_id \n FROM MedicalHistory \n WHERE diagnosis = 'Hypertension'\n ) \n AND p.patient_id IN (\n SELECT patient_id \n FROM PatientTests \n WHERE results = 'Negative'\n )"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of the patients who have undergone a medical test with a result of 'Positive' and have a medical history diagnosis of 'Diabetes', and what are the names of the medications prescribed to these patients, along with the dosage and frequency, and the names of the doctors who prescribed the medications, and the dates of the prescriptions?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n patients.first_name, \n patients.last_name, \n medications.medication_name, \n prescription_details.dosage, \n prescription_details.frequency, \n doctors.first_name, \n doctors.last_name, \n prescriptions.prescription_date\nFROM \n Patients patients\n JOIN Prescriptions prescriptions ON patients.patient_id = prescriptions.patient_id\n JOIN PrescriptionDetails prescription_details ON prescriptions.prescription_id = prescription_details.prescription_id\n JOIN Medications medications ON prescription_details.medication_id = medications.medication_id\n JOIN Doctors doctors ON prescriptions.doctor_id = doctors.doctor_id\nWHERE \n patients.patient_id IN (\n SELECT patient_id \n FROM MedicalHistory \n WHERE diagnosis = 'Diabetes'\n )\n AND patients.patient_id IN (\n SELECT patient_id \n FROM PatientTests \n WHERE results = 'Positive'\n )"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of the doctors who have prescribed medications to patients with a medical history diagnosis of 'Hypertension' and have also conducted medical tests with a result of 'Negative', along with the names of the medications prescribed, and the names of the patients who were prescribed the medications, and the dates of the prescriptions?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n d.first_name, \n d.last_name, \n m.medication_name, \n p.first_name, \n p.last_name, \n pr.prescription_date\nFROM \n Doctors d\n JOIN Prescriptions pr ON d.doctor_id = pr.doctor_id\n JOIN Patients p ON pr.patient_id = p.patient_id\n JOIN MedicalHistory mh ON p.patient_id = mh.patient_id\n JOIN PatientTests pt ON p.patient_id = pt.patient_id\n JOIN PrescriptionDetails pd ON pr.prescription_id = pd.prescription_id\n JOIN Medications m ON pd.medication_id = m.medication_id\nWHERE \n mh.diagnosis = 'Hypertension' \n AND pt.results = 'Negative'\n AND d.doctor_id = pt.doctor_id;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of the patients who have undergone a medical test with a result of 'Positive' and have a medical history diagnosis of 'Diabetes', and what are the names of the medications prescribed to these patients, along with the dosage and frequency, and the names of the doctors who prescribed the medications, and the dates of the prescriptions, and the medical test dates?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n p.first_name, \n p.last_name, \n m.medication_name, \n pd.dosage, \n pd.frequency, \n d.first_name, \n d.last_name, \n pr.prescription_date, \n pt.test_date\nFROM \n Patients p\n JOIN PatientTests pt ON p.patient_id = pt.patient_id\n JOIN Prescriptions pr ON p.patient_id = pr.patient_id\n JOIN PrescriptionDetails pd ON pr.prescription_id = pd.prescription_id\n JOIN Medications m ON pd.medication_id = m.medication_id\n JOIN Doctors d ON pr.doctor_id = d.doctor_id\nWHERE \n LOWER(pt.results) = 'positive' \n AND p.patient_id IN (SELECT patient_id FROM MedicalHistory WHERE diagnosis = 'Diabetes')"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of the doctors who have prescribed medications to patients with a medical history diagnosis of 'Hypertension' and have also conducted medical tests with a result of 'Negative', along with the names of the medications prescribed, and the names of the patients who were prescribed the medications, and the dates of the prescriptions, and the medical test dates?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n d.first_name, \n d.last_name, \n m.medication_name, \n p.first_name, \n p.last_name, \n pr.prescription_date, \n pt.test_date\nFROM \n Doctors d\n JOIN Prescriptions pr ON d.doctor_id = pr.doctor_id\n JOIN Patients p ON pr.patient_id = p.patient_id\n JOIN PrescriptionDetails pd ON pr.prescription_id = pd.prescription_id\n JOIN Medications m ON pd.medication_id = m.medication_id\n JOIN PatientTests pt ON p.patient_id = pt.patient_id\nWHERE \n p.patient_id IN (SELECT patient_id FROM MedicalHistory WHERE diagnosis = 'Hypertension')\n AND pt.results = 'Negative'"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of the patients who have undergone a medical test with a result of 'Positive' and have a medical history diagnosis of 'Diabetes', and what are the names of the medications prescribed to these patients, along with the dosage and frequency, and the names of the doctors who prescribed the medications, and the dates of the prescriptions, and the medical test dates, and the appointment dates?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n pat.first_name, \n pat.last_name, \n med.medication_name, \n pd.dosage, \n pd.frequency, \n doc.first_name, \n doc.last_name, \n pr.prescription_date, \n pt.test_date, \n a.appointment_date\nFROM \n Patients pat\n JOIN Prescriptions pr ON pat.patient_id = pr.patient_id\n JOIN PrescriptionDetails pd ON pr.prescription_id = pd.prescription_id\n JOIN Medications med ON pd.medication_id = med.medication_id\n JOIN Doctors doc ON pr.doctor_id = doc.doctor_id\n JOIN Appointments a ON pat.patient_id = a.patient_id\n JOIN PatientTests pt ON pat.patient_id = pt.patient_id\nWHERE \n pt.results = 'Positive' \n AND EXISTS (\n SELECT 1 \n FROM MedicalHistory mh \n WHERE mh.patient_id = pat.patient_id \n AND mh.diagnosis = 'Diabetes'\n )"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of the doctors who have prescribed medications to patients with a medical history diagnosis of 'Hypertension' and have also conducted medical tests with a result of 'Negative', along with the names of the medications prescribed, and the names of the patients who were prescribed the medications, and the dates of the prescriptions, and the medical test dates, and the appointment dates?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n d.first_name, \n d.last_name, \n m.medication_name, \n pt.test_date\nFROM \n Doctors d\n JOIN Prescriptions pr ON d.doctor_id = pr.doctor_id\n JOIN PrescriptionDetails pd ON pr.prescription_id = pd.prescription_id\n JOIN Medications m ON pd.medication_id = m.medication_id\n JOIN PatientTests pt ON pr.patient_id = pt.patient_id AND d.doctor_id = pt.doctor_id\nWHERE \n pt.results = 'Negative'\n AND pr.patient_id IN (SELECT patient_id FROM MedicalHistory WHERE diagnosis = 'Hypertension');"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of the patients who have undergone a medical test with a result of 'Positive' and have a medical history diagnosis of 'Diabetes', and what are the names of the medications prescribed to these patients, along with the dosage and frequency, and the names of the doctors who prescribed the medications, and the dates of the prescriptions, and the medical test dates, and the appointment dates, and the billing information?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n patients.first_name, \n patients.last_name, \n medications.medication_name, \n prescription_details.dosage, \n prescription_details.frequency, \n doctors.first_name, \n doctors.last_name, \n prescriptions.prescription_date, \n patient_tests.test_date, \n appointments.appointment_date, \n billing.total_amount, \n billing.billing_date\nFROM \n Patients patients\n JOIN Prescriptions prescriptions ON patients.patient_id = prescriptions.patient_id\n JOIN PrescriptionDetails prescription_details ON prescriptions.prescription_id = prescription_details.prescription_id\n JOIN Medications medications ON prescription_details.medication_id = medications.medication_id\n JOIN Doctors doctors ON prescriptions.doctor_id = doctors.doctor_id\n JOIN Appointments appointments ON patients.patient_id = appointments.patient_id\n JOIN Billing billing ON patients.patient_id = billing.patient_id\n JOIN PatientTests patient_tests ON patients.patient_id = patient_tests.patient_id\nWHERE \n patient_tests.results = 'Positive' \n AND patients.patient_id IN (SELECT patient_id FROM MedicalHistory WHERE diagnosis = 'Diabetes')"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of the doctors who have prescribed medications to patients with a medical history diagnosis of 'Hypertension' and have also conducted medical tests with a result of 'Negative', along with the names of the medications prescribed, and the names of the patients who were prescribed the medications, and the dates of the prescriptions, and the medical test dates, and the appointment dates, and the billing information?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n d.first_name AS doctor_first_name, \n d.last_name AS doctor_last_name, \n m.medication_name, \n p.first_name AS patient_first_name, \n p.last_name AS patient_last_name, \n pr.prescription_date, \n pt.test_date, \n a.appointment_date, \n b.total_amount, \n b.billing_date\nFROM \n Doctors d\n JOIN Prescriptions pr ON d.doctor_id = pr.doctor_id\n JOIN Patients p ON pr.patient_id = p.patient_id\n JOIN MedicalHistory mh ON p.patient_id = mh.patient_id\n JOIN PatientTests pt ON p.patient_id = pt.patient_id AND pt.doctor_id = d.doctor_id\n JOIN PrescriptionDetails pd ON pr.prescription_id = pd.prescription_id\n JOIN Medications m ON pd.medication_id = m.medication_id\n JOIN Appointments a ON p.patient_id = a.patient_id AND a.doctor_id = d.doctor_id\n JOIN Billing b ON p.patient_id = b.patient_id AND b.appointment_id = a.appointment_id\nWHERE \n mh.diagnosis = 'Hypertension' \n AND pt.results = 'Negative'\nORDER BY \n p.last_name, p.first_name, d.last_name, d.first_name"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of the patients who have undergone a medical test with a result of 'Positive' and have a medical history diagnosis of 'Diabetes', and what are the names of the medications prescribed to these patients, along with the dosage and frequency, and the names of the doctors who prescribed the medications, and the dates of the prescriptions, and the medical test dates, and the appointment dates, and the billing information, and the insurance policy information?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "SELECT \n p.first_name, \n p.last_name, \n m.medication_name, \n pd.dosage, \n pd.frequency, \n d.first_name, \n d.last_name, \n pr.prescription_date\nFROM \n Patients p\n JOIN Prescriptions pr ON p.patient_id = pr.patient_id\n JOIN PrescriptionDetails pd ON pr.prescription_id = pd.prescription_id\n JOIN Medications m ON pd.medication_id = m.medication_id\n JOIN Doctors d ON pr.doctor_id = d.doctor_id\n JOIN PatientTests pt ON p.patient_id = pt.patient_id AND pt.results = 'Positive'\nWHERE \n p.patient_id IN (SELECT patient_id FROM MedicalHistory WHERE diagnosis = 'Diabetes')"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What are the names of the doctors who have prescribed medications to patients with a medical history diagnosis of 'Hypertension' and have also conducted medical tests with a result of 'Negative', along with the names of the medications prescribed, and the names of the patients who were prescribed the medications, and the dates of the prescriptions, and the medical test dates, and the appointment dates, and the billing information, and the insurance policy information?, Input schema: CREATE TABLE Patients (\n patient_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Doctors (\n doctor_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n specialization TEXT NOT NULL,\n phone TEXT,\n email TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Appointments (\n appointment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n appointment_date TIMESTAMP NOT NULL,\n reason TEXT,\n status TEXT CHECK(status IN ('Scheduled', 'Completed', 'Canceled')) DEFAULT 'Scheduled',\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE MedicalHistory (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n diagnosis TEXT NOT NULL,\n treatment TEXT,\n diagnosis_date DATE NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);\n\nCREATE TABLE Prescriptions (\n prescription_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n doctor_id INTEGER NOT NULL,\n prescription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n medication_name TEXT NOT NULL,\n dosage TEXT NOT NULL,\n duration_days INTEGER NOT NULL,\n instructions TEXT,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Medications (\n medication_id INTEGER PRIMARY KEY AUTOINCREMENT,\n medication_name TEXT NOT NULL UNIQUE,\n description TEXT,\n side_effects TEXT\n);\n\nCREATE TABLE PrescriptionDetails (\n prescription_detail_id INTEGER PRIMARY KEY AUTOINCREMENT,\n prescription_id INTEGER NOT NULL,\n medication_id INTEGER NOT NULL,\n dosage TEXT NOT NULL,\n frequency TEXT NOT NULL,\n FOREIGN KEY (prescription_id) REFERENCES Prescriptions(prescription_id),\n FOREIGN KEY (medication_id) REFERENCES Medications(medication_id)\n);\n\nCREATE TABLE MedicalTests (\n test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n test_name TEXT NOT NULL,\n description TEXT\n);\n\nCREATE TABLE PatientTests (\n patient_test_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n test_id INTEGER NOT NULL,\n test_date TIMESTAMP NOT NULL,\n results TEXT,\n doctor_id INTEGER,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (test_id) REFERENCES MedicalTests(test_id),\n FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id)\n);\n\nCREATE TABLE Billing (\n billing_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n appointment_id INTEGER,\n total_amount REAL NOT NULL,\n billing_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n paid INTEGER CHECK(paid IN (0, 1)) DEFAULT 0,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id),\n FOREIGN KEY (appointment_id) REFERENCES Appointments(appointment_id)\n);\n\nCREATE TABLE InsurancePolicies (\n policy_id INTEGER PRIMARY KEY AUTOINCREMENT,\n patient_id INTEGER NOT NULL,\n policy_number TEXT NOT NULL,\n provider_name TEXT NOT NULL,\n coverage_amount REAL NOT NULL,\n expiry_date DATE NOT NULL,\n FOREIGN KEY (patient_id) REFERENCES Patients(patient_id)\n);"}, {"from": "gpt", "value": "WITH patients_with_hypertension AS (\n SELECT p.patient_id, p.first_name, p.last_name\n FROM Patients p\n JOIN MedicalHistory mh ON p.patient_id = mh.patient_id\n WHERE mh.diagnosis = 'Hypertension'\n),\npatients_with_negative_test_results AS (\n SELECT p.patient_id, pt.test_date\n FROM Patients p\n JOIN PatientTests pt ON p.patient_id = pt.patient_id\n WHERE pt.results = 'Negative'\n),\nprescriptions_for_hypertension AS (\n SELECT pr.prescription_id, pr.prescription_date, m.medication_name\n FROM Prescriptions pr\n JOIN PrescriptionDetails pd ON pr.prescription_id = pd.prescription_id\n JOIN Medications m ON pd.medication_id = m.medication_id\n WHERE pr.patient_id IN (SELECT patient_id FROM patients_with_hypertension)\n)\nSELECT \n d.first_name, \n d.last_name, \n p.first_name, \n p.last_name, \n pr.prescription_date, \n pt.test_date, \n a.appointment_date, \n b.total_amount, \n b.billing_date, \n i.policy_number, \n i.provider_name\nFROM \n patients_with_hypertension p\nJOIN patients_with_negative_test_results pt ON p.patient_id = pt.patient_id\nJOIN prescriptions_for_hypertension pr ON p.patient_id = pr.patient_id\nJOIN Doctors d ON pr.doctor_id = d.doctor_id\nJOIN Appointments a ON p.patient_id = a.patient_id\nJOIN Billing b ON p.patient_id = b.patient_id\nJOIN InsurancePolicies i ON p.patient_id = i.patient_id;"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the average credit score of applicants who have applied for a loan in the last 6 months and have a credit score greater than 700, grouped by loan type?, Input schema: CREATE TABLE Applicants (\n applicant_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT NOT NULL UNIQUE,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE LoanTypes (\n loan_type_id INTEGER PRIMARY KEY AUTOINCREMENT,\n loan_type_name TEXT NOT NULL,\n description TEXT,\n interest_rate REAL NOT NULL\n);\n\nCREATE TABLE LoanApplications (\n application_id INTEGER PRIMARY KEY AUTOINCREMENT,\n applicant_id INTEGER NOT NULL,\n loan_type_id INTEGER NOT NULL,\n loan_amount REAL NOT NULL,\n application_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Approved', 'Rejected')) DEFAULT 'Pending',\n approval_date TIMESTAMP,\n rejection_reason TEXT,\n FOREIGN KEY (applicant_id) REFERENCES Applicants(applicant_id),\n FOREIGN KEY (loan_type_id) REFERENCES LoanTypes(loan_type_id)\n);\n\nCREATE TABLE LoanOfficers (\n officer_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n employee_id TEXT NOT NULL UNIQUE,\n phone TEXT,\n email TEXT NOT NULL UNIQUE,\n department TEXT NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Approvals (\n approval_id INTEGER PRIMARY KEY AUTOINCREMENT,\n application_id INTEGER NOT NULL,\n officer_id INTEGER NOT NULL,\n approval_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n loan_terms TEXT,\n FOREIGN KEY (application_id) REFERENCES LoanApplications(application_id),\n FOREIGN KEY (officer_id) REFERENCES LoanOfficers(officer_id)\n);\n\nCREATE TABLE LoanRepayments (\n repayment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n application_id INTEGER NOT NULL,\n payment_date TIMESTAMP NOT NULL,\n amount_paid REAL NOT NULL,\n remaining_balance REAL NOT NULL,\n payment_status TEXT CHECK(payment_status IN ('On-Time', 'Late', 'Missed')) DEFAULT 'On-Time',\n FOREIGN KEY (application_id) REFERENCES LoanApplications(application_id)\n);\n\nCREATE TABLE PaymentSchedules (\n schedule_id INTEGER PRIMARY KEY AUTOINCREMENT,\n application_id INTEGER NOT NULL,\n due_date TIMESTAMP NOT NULL,\n amount_due REAL NOT NULL,\n payment_status TEXT CHECK(payment_status IN ('Pending', 'Paid', 'Missed')) DEFAULT 'Pending',\n FOREIGN KEY (application_id) REFERENCES LoanApplications(application_id)\n);\n\nCREATE TABLE LoanCollaterals (\n collateral_id INTEGER PRIMARY KEY AUTOINCREMENT,\n application_id INTEGER NOT NULL,\n collateral_type TEXT NOT NULL,\n description TEXT,\n value REAL NOT NULL,\n FOREIGN KEY (application_id) REFERENCES LoanApplications(application_id)\n);\n\nCREATE TABLE LoanDocuments (\n document_id INTEGER PRIMARY KEY AUTOINCREMENT,\n application_id INTEGER NOT NULL,\n document_name TEXT NOT NULL,\n document_type TEXT,\n file_path TEXT NOT NULL,\n uploaded_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (application_id) REFERENCES LoanApplications(application_id)\n);\n\nCREATE TABLE CreditScores (\n credit_score_id INTEGER PRIMARY KEY AUTOINCREMENT,\n applicant_id INTEGER NOT NULL,\n credit_score INTEGER NOT NULL CHECK(credit_score BETWEEN 300 AND 850),\n score_date TIMESTAMP NOT NULL,\n FOREIGN KEY (applicant_id) REFERENCES Applicants(applicant_id)\n);"}, {"from": "gpt", "value": "SELECT \n LT.loan_type_name, \n AVG(CS.credit_score) AS avg_credit_score\nFROM \n LoanApplications LA\nJOIN \n LoanTypes LT ON LA.loan_type_id = LT.loan_type_id\nJOIN \n CreditScores CS ON LA.applicant_id = CS.applicant_id\nWHERE \n LA.application_date > DATE('now', '-6 months')\n AND CS.credit_score > 700\nGROUP BY \n LT.loan_type_name"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the total loan amount applied for by applicants from each city, where the applicant has a credit score greater than 600 and the loan application is pending?, Input schema: CREATE TABLE Applicants (\n applicant_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT NOT NULL UNIQUE,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE LoanTypes (\n loan_type_id INTEGER PRIMARY KEY AUTOINCREMENT,\n loan_type_name TEXT NOT NULL,\n description TEXT,\n interest_rate REAL NOT NULL\n);\n\nCREATE TABLE LoanApplications (\n application_id INTEGER PRIMARY KEY AUTOINCREMENT,\n applicant_id INTEGER NOT NULL,\n loan_type_id INTEGER NOT NULL,\n loan_amount REAL NOT NULL,\n application_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Approved', 'Rejected')) DEFAULT 'Pending',\n approval_date TIMESTAMP,\n rejection_reason TEXT,\n FOREIGN KEY (applicant_id) REFERENCES Applicants(applicant_id),\n FOREIGN KEY (loan_type_id) REFERENCES LoanTypes(loan_type_id)\n);\n\nCREATE TABLE LoanOfficers (\n officer_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n employee_id TEXT NOT NULL UNIQUE,\n phone TEXT,\n email TEXT NOT NULL UNIQUE,\n department TEXT NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Approvals (\n approval_id INTEGER PRIMARY KEY AUTOINCREMENT,\n application_id INTEGER NOT NULL,\n officer_id INTEGER NOT NULL,\n approval_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n loan_terms TEXT,\n FOREIGN KEY (application_id) REFERENCES LoanApplications(application_id),\n FOREIGN KEY (officer_id) REFERENCES LoanOfficers(officer_id)\n);\n\nCREATE TABLE LoanRepayments (\n repayment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n application_id INTEGER NOT NULL,\n payment_date TIMESTAMP NOT NULL,\n amount_paid REAL NOT NULL,\n remaining_balance REAL NOT NULL,\n payment_status TEXT CHECK(payment_status IN ('On-Time', 'Late', 'Missed')) DEFAULT 'On-Time',\n FOREIGN KEY (application_id) REFERENCES LoanApplications(application_id)\n);\n\nCREATE TABLE PaymentSchedules (\n schedule_id INTEGER PRIMARY KEY AUTOINCREMENT,\n application_id INTEGER NOT NULL,\n due_date TIMESTAMP NOT NULL,\n amount_due REAL NOT NULL,\n payment_status TEXT CHECK(payment_status IN ('Pending', 'Paid', 'Missed')) DEFAULT 'Pending',\n FOREIGN KEY (application_id) REFERENCES LoanApplications(application_id)\n);\n\nCREATE TABLE LoanCollaterals (\n collateral_id INTEGER PRIMARY KEY AUTOINCREMENT,\n application_id INTEGER NOT NULL,\n collateral_type TEXT NOT NULL,\n description TEXT,\n value REAL NOT NULL,\n FOREIGN KEY (application_id) REFERENCES LoanApplications(application_id)\n);\n\nCREATE TABLE LoanDocuments (\n document_id INTEGER PRIMARY KEY AUTOINCREMENT,\n application_id INTEGER NOT NULL,\n document_name TEXT NOT NULL,\n document_type TEXT,\n file_path TEXT NOT NULL,\n uploaded_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (application_id) REFERENCES LoanApplications(application_id)\n);\n\nCREATE TABLE CreditScores (\n credit_score_id INTEGER PRIMARY KEY AUTOINCREMENT,\n applicant_id INTEGER NOT NULL,\n credit_score INTEGER NOT NULL CHECK(credit_score BETWEEN 300 AND 850),\n score_date TIMESTAMP NOT NULL,\n FOREIGN KEY (applicant_id) REFERENCES Applicants(applicant_id)\n);"}, {"from": "gpt", "value": "SELECT \n A.city, \n SUM(LA.loan_amount) AS total_loan_amount\nFROM \n LoanApplications LA\nJOIN \n Applicants A ON LA.applicant_id = A.applicant_id\nJOIN \n CreditScores CS ON A.applicant_id = CS.applicant_id\nWHERE \n CS.credit_score > 600 \n AND LA.status = 'Pending'\nGROUP BY \n A.city"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the number of loan applications submitted by applicants from each state, where the applicant has a credit score greater than 650 and the loan application is approved?, Input schema: CREATE TABLE Applicants (\n applicant_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT NOT NULL UNIQUE,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE LoanTypes (\n loan_type_id INTEGER PRIMARY KEY AUTOINCREMENT,\n loan_type_name TEXT NOT NULL,\n description TEXT,\n interest_rate REAL NOT NULL\n);\n\nCREATE TABLE LoanApplications (\n application_id INTEGER PRIMARY KEY AUTOINCREMENT,\n applicant_id INTEGER NOT NULL,\n loan_type_id INTEGER NOT NULL,\n loan_amount REAL NOT NULL,\n application_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Approved', 'Rejected')) DEFAULT 'Pending',\n approval_date TIMESTAMP,\n rejection_reason TEXT,\n FOREIGN KEY (applicant_id) REFERENCES Applicants(applicant_id),\n FOREIGN KEY (loan_type_id) REFERENCES LoanTypes(loan_type_id)\n);\n\nCREATE TABLE LoanOfficers (\n officer_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n employee_id TEXT NOT NULL UNIQUE,\n phone TEXT,\n email TEXT NOT NULL UNIQUE,\n department TEXT NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Approvals (\n approval_id INTEGER PRIMARY KEY AUTOINCREMENT,\n application_id INTEGER NOT NULL,\n officer_id INTEGER NOT NULL,\n approval_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n loan_terms TEXT,\n FOREIGN KEY (application_id) REFERENCES LoanApplications(application_id),\n FOREIGN KEY (officer_id) REFERENCES LoanOfficers(officer_id)\n);\n\nCREATE TABLE LoanRepayments (\n repayment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n application_id INTEGER NOT NULL,\n payment_date TIMESTAMP NOT NULL,\n amount_paid REAL NOT NULL,\n remaining_balance REAL NOT NULL,\n payment_status TEXT CHECK(payment_status IN ('On-Time', 'Late', 'Missed')) DEFAULT 'On-Time',\n FOREIGN KEY (application_id) REFERENCES LoanApplications(application_id)\n);\n\nCREATE TABLE PaymentSchedules (\n schedule_id INTEGER PRIMARY KEY AUTOINCREMENT,\n application_id INTEGER NOT NULL,\n due_date TIMESTAMP NOT NULL,\n amount_due REAL NOT NULL,\n payment_status TEXT CHECK(payment_status IN ('Pending', 'Paid', 'Missed')) DEFAULT 'Pending',\n FOREIGN KEY (application_id) REFERENCES LoanApplications(application_id)\n);\n\nCREATE TABLE LoanCollaterals (\n collateral_id INTEGER PRIMARY KEY AUTOINCREMENT,\n application_id INTEGER NOT NULL,\n collateral_type TEXT NOT NULL,\n description TEXT,\n value REAL NOT NULL,\n FOREIGN KEY (application_id) REFERENCES LoanApplications(application_id)\n);\n\nCREATE TABLE LoanDocuments (\n document_id INTEGER PRIMARY KEY AUTOINCREMENT,\n application_id INTEGER NOT NULL,\n document_name TEXT NOT NULL,\n document_type TEXT,\n file_path TEXT NOT NULL,\n uploaded_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (application_id) REFERENCES LoanApplications(application_id)\n);\n\nCREATE TABLE CreditScores (\n credit_score_id INTEGER PRIMARY KEY AUTOINCREMENT,\n applicant_id INTEGER NOT NULL,\n credit_score INTEGER NOT NULL CHECK(credit_score BETWEEN 300 AND 850),\n score_date TIMESTAMP NOT NULL,\n FOREIGN KEY (applicant_id) REFERENCES Applicants(applicant_id)\n);"}, {"from": "gpt", "value": "SELECT \n A.state, \n COUNT(LA.application_id) AS num_loan_applications\nFROM \n LoanApplications LA\nJOIN \n Applicants A ON LA.applicant_id = A.applicant_id\nJOIN \n CreditScores CS ON A.applicant_id = CS.applicant_id\nWHERE \n CS.credit_score > 650 \n AND LA.status = 'Approved'\nGROUP BY \n A.state"}]
[{"from": "system", "value": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."}, {"from": "human", "value": "Instruction: What is the average loan amount approved for applicants from each country, where the applicant has a credit score greater than 700 and the loan application is approved?, Input schema: CREATE TABLE Applicants (\n applicant_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n gender TEXT CHECK(gender IN ('Male', 'Female', 'Other')),\n birth_date DATE NOT NULL,\n phone TEXT,\n email TEXT NOT NULL UNIQUE,\n address TEXT,\n city TEXT,\n state TEXT,\n postal_code TEXT,\n country TEXT,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE LoanTypes (\n loan_type_id INTEGER PRIMARY KEY AUTOINCREMENT,\n loan_type_name TEXT NOT NULL,\n description TEXT,\n interest_rate REAL NOT NULL\n);\n\nCREATE TABLE LoanApplications (\n application_id INTEGER PRIMARY KEY AUTOINCREMENT,\n applicant_id INTEGER NOT NULL,\n loan_type_id INTEGER NOT NULL,\n loan_amount REAL NOT NULL,\n application_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Approved', 'Rejected')) DEFAULT 'Pending',\n approval_date TIMESTAMP,\n rejection_reason TEXT,\n FOREIGN KEY (applicant_id) REFERENCES Applicants(applicant_id),\n FOREIGN KEY (loan_type_id) REFERENCES LoanTypes(loan_type_id)\n);\n\nCREATE TABLE LoanOfficers (\n officer_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n employee_id TEXT NOT NULL UNIQUE,\n phone TEXT,\n email TEXT NOT NULL UNIQUE,\n department TEXT NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE Approvals (\n approval_id INTEGER PRIMARY KEY AUTOINCREMENT,\n application_id INTEGER NOT NULL,\n officer_id INTEGER NOT NULL,\n approval_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n loan_terms TEXT,\n FOREIGN KEY (application_id) REFERENCES LoanApplications(application_id),\n FOREIGN KEY (officer_id) REFERENCES LoanOfficers(officer_id)\n);\n\nCREATE TABLE LoanRepayments (\n repayment_id INTEGER PRIMARY KEY AUTOINCREMENT,\n application_id INTEGER NOT NULL,\n payment_date TIMESTAMP NOT NULL,\n amount_paid REAL NOT NULL,\n remaining_balance REAL NOT NULL,\n payment_status TEXT CHECK(payment_status IN ('On-Time', 'Late', 'Missed')) DEFAULT 'On-Time',\n FOREIGN KEY (application_id) REFERENCES LoanApplications(application_id)\n);\n\nCREATE TABLE PaymentSchedules (\n schedule_id INTEGER PRIMARY KEY AUTOINCREMENT,\n application_id INTEGER NOT NULL,\n due_date TIMESTAMP NOT NULL,\n amount_due REAL NOT NULL,\n payment_status TEXT CHECK(payment_status IN ('Pending', 'Paid', 'Missed')) DEFAULT 'Pending',\n FOREIGN KEY (application_id) REFERENCES LoanApplications(application_id)\n);\n\nCREATE TABLE LoanCollaterals (\n collateral_id INTEGER PRIMARY KEY AUTOINCREMENT,\n application_id INTEGER NOT NULL,\n collateral_type TEXT NOT NULL,\n description TEXT,\n value REAL NOT NULL,\n FOREIGN KEY (application_id) REFERENCES LoanApplications(application_id)\n);\n\nCREATE TABLE LoanDocuments (\n document_id INTEGER PRIMARY KEY AUTOINCREMENT,\n application_id INTEGER NOT NULL,\n document_name TEXT NOT NULL,\n document_type TEXT,\n file_path TEXT NOT NULL,\n uploaded_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (application_id) REFERENCES LoanApplications(application_id)\n);\n\nCREATE TABLE CreditScores (\n credit_score_id INTEGER PRIMARY KEY AUTOINCREMENT,\n applicant_id INTEGER NOT NULL,\n credit_score INTEGER NOT NULL CHECK(credit_score BETWEEN 300 AND 850),\n score_date TIMESTAMP NOT NULL,\n FOREIGN KEY (applicant_id) REFERENCES Applicants(applicant_id)\n);"}, {"from": "gpt", "value": "SELECT \n A.country, \n AVG(LA.loan_amount) AS avg_loan_amount\nFROM \n LoanApplications LA\nJOIN \n Applicants A ON LA.applicant_id = A.applicant_id\nJOIN \n CreditScores CS ON A.applicant_id = CS.applicant_id\nWHERE \n CS.credit_score > 700 \n AND LA.status = 'Approved'\nGROUP BY \n A.country"}]