File size: 12,553 Bytes
e7cf806
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
[
  {
    "id": "q1",
    "title": "List female patients",
    "difficulty": "Beginner",
    "description": "Retrieve the names and contact details of all female patients.",
    "hint": "Use a WHERE clause to filter by gender.",
    "expected_sql": "SELECT name, contact FROM patients WHERE gender = 'Female';"
  },
  {
    "id": "q2",
    "title": "Find appointments on specific date",
    "difficulty": "Beginner",
    "description": "Show all appointments scheduled on July 10, 2023.",
    "hint": "Use a WHERE clause with date comparison.",
    "expected_sql": "SELECT appointment_id, patient_id, doctor_id, appointment_date FROM appointments WHERE appointment_date = '2023-07-10';"
  },
  {
    "id": "q3",
    "title": "List cardiologists",
    "difficulty": "Beginner",
    "description": "Retrieve the names and contact details of all doctors with the Cardiologist specialty.",
    "hint": "Use a WHERE clause to filter by specialty.",
    "expected_sql": "SELECT name, contact FROM doctors WHERE specialty = 'Cardiologist';"
  },
  {
    "id": "q4",
    "title": "Patients born before 1995",
    "difficulty": "Beginner",
    "description": "Show the names and dates of birth of patients born before January 1, 1995.",
    "hint": "Use a WHERE clause with date comparison on dob.",
    "expected_sql": "SELECT name, dob FROM patients WHERE dob < '1995-01-01';"
  },
  {
    "id": "q5",
    "title": "List unique diagnoses",
    "difficulty": "Beginner",
    "description": "Retrieve all unique diagnoses from the appointments table.",
    "hint": "Use DISTINCT to avoid duplicate diagnoses.",
    "expected_sql": "SELECT DISTINCT diagnosis FROM appointments;"
  },
  {
    "id": "q6",
    "title": "Appointments for doctor ID 1",
    "difficulty": "Beginner",
    "description": "Show all appointments for the doctor with ID 1.",
    "hint": "Use a WHERE clause to filter by doctor_id.",
    "expected_sql": "SELECT appointment_id, patient_id, appointment_date FROM appointments WHERE doctor_id = 1;"
  },
  {
    "id": "q7",
    "title": "List patient names and genders",
    "difficulty": "Beginner",
    "description": "Show all patient names and their genders.",
    "hint": "Select name and gender from the patients table.",
    "expected_sql": "SELECT name, gender FROM patients;"
  },
  {
    "id": "q8",
    "title": "Appointments in August 2023",
    "difficulty": "Beginner",
    "description": "Retrieve all appointments scheduled in August 2023.",
    "hint": "Use a WHERE clause with LIKE for appointment_date.",
    "expected_sql": "SELECT appointment_id, patient_id, doctor_id, appointment_date FROM appointments WHERE appointment_date LIKE '2023-08%';"
  },
  {
    "id": "q9",
    "title": "List doctors and specialties",
    "difficulty": "Beginner",
    "description": "Show all doctor names and their specialties.",
    "hint": "Select name and specialty from the doctors table.",
    "expected_sql": "SELECT name, specialty FROM doctors;"
  },
  {
    "id": "q10",
    "title": "Patients with Routine Checkup",
    "difficulty": "Beginner",
    "description": "Retrieve the names of patients with a diagnosis of 'Routine Checkup'.",
    "hint": "Join patients and appointments, filter by diagnosis.",
    "expected_sql": "SELECT p.name FROM patients p JOIN appointments a ON p.patient_id = a.patient_id WHERE a.diagnosis = 'Routine Checkup';"
  },
  {
    "id": "q11",
    "title": "Appointments per doctor",
    "difficulty": "Intermediate",
    "description": "Show the number of appointments for each doctor, including those with zero appointments.",
    "hint": "Use a LEFT JOIN and GROUP BY doctor name.",
    "expected_sql": "SELECT d.name, COUNT(a.appointment_id) AS appointment_count FROM doctors d LEFT JOIN appointments a ON d.doctor_id = a.doctor_id GROUP BY d.name;"
  },
  {
    "id": "q12",
    "title": "Patients with multiple appointments",
    "difficulty": "Intermediate",
    "description": "Find patients who have more than one appointment.",
    "hint": "Join patients and appointments, use GROUP BY and HAVING clause.",
    "expected_sql": "SELECT p.name, COUNT(a.appointment_id) AS appointment_count FROM patients p JOIN appointments a ON p.patient_id = a.patient_id GROUP BY p.name HAVING COUNT(a.appointment_id) > 1;"
  },
  {
    "id": "q13",
    "title": "Appointments by doctor specialty",
    "difficulty": "Intermediate",
    "description": "Count the number of appointments for each doctor specialty.",
    "hint": "Join doctors and appointments, then GROUP BY specialty.",
    "expected_sql": "SELECT d.specialty, COUNT(a.appointment_id) AS total_appointments FROM doctors d JOIN appointments a ON d.doctor_id = a.doctor_id GROUP BY d.specialty;"
  },
  {
    "id": "q14",
    "title": "Patient and doctor appointment details",
    "difficulty": "Intermediate",
    "description": "Show patient names, doctor names, and appointment dates for all appointments.",
    "hint": "Join patients, doctors, and appointments tables.",
    "expected_sql": "SELECT p.name AS patient_name, d.name AS doctor_name, a.appointment_date FROM appointments a JOIN patients p ON a.patient_id = p.patient_id JOIN doctors d ON a.doctor_id = d.doctor_id;"
  },
  {
    "id": "q15",
    "title": "Diagnoses by patient gender",
    "difficulty": "Intermediate",
    "description": "Count the number of appointments for each diagnosis, grouped by patient gender.",
    "hint": "Join patients and appointments, GROUP BY gender and diagnosis.",
    "expected_sql": "SELECT p.gender, a.diagnosis, COUNT(a.appointment_id) AS appointment_count FROM patients p JOIN appointments a ON p.patient_id = a.patient_id GROUP BY p.gender, a.diagnosis;"
  },
  {
    "id": "q16",
    "title": "Doctors with no appointments",
    "difficulty": "Intermediate",
    "description": "List doctors who have not had any appointments.",
    "hint": "Use a LEFT JOIN and check for NULL in the appointments table.",
    "expected_sql": "SELECT d.name FROM doctors d LEFT JOIN appointments a ON d.doctor_id = a.doctor_id WHERE a.appointment_id IS NULL;"
  },
  {
    "id": "q17",
    "title": "Patients seen by multiple doctors",
    "difficulty": "Intermediate",
    "description": "Find patients who have appointments with more than one distinct doctor.",
    "hint": "Join patients and appointments, group by patient, and use HAVING clause.",
    "expected_sql": "SELECT p.name, COUNT(DISTINCT a.doctor_id) AS doctor_count FROM patients p JOIN appointments a ON p.patient_id = a.patient_id GROUP BY p.name HAVING COUNT(DISTINCT a.doctor_id) > 1;"
  },
  {
    "id": "q18",
    "title": "Appointments in July 2023",
    "difficulty": "Intermediate",
    "description": "List all appointments between July 1, 2023, and July 31, 2023.",
    "hint": "Use a BETWEEN clause for the date range.",
    "expected_sql": "SELECT appointment_id, patient_id, doctor_id, appointment_date FROM appointments WHERE appointment_date BETWEEN '2023-07-01' AND '2023-07-31';"
  },
  {
    "id": "q19",
    "title": "Most common diagnosis",
    "difficulty": "Intermediate",
    "description": "Find the diagnosis with the highest number of appointments.",
    "hint": "Group by diagnosis, count appointments, and use LIMIT.",
    "expected_sql": "SELECT diagnosis, COUNT(appointment_id) AS diagnosis_count FROM appointments GROUP BY diagnosis ORDER BY diagnosis_count DESC LIMIT 1;"
  },
  {
    "id": "q20",
    "title": "Unique patients by specialty",
    "difficulty": "Intermediate",
    "description": "Show the number of unique patients seen by each doctor specialty.",
    "hint": "Join doctors and appointments, use COUNT and DISTINCT, group by specialty.",
    "expected_sql": "SELECT d.specialty, COUNT(DISTINCT a.patient_id) AS unique_patients FROM doctors d JOIN appointments a ON d.doctor_id = a.doctor_id GROUP BY d.specialty;"
  },
  {
    "id": "q21",
    "title": "Patient age at appointment",
    "difficulty": "Advanced",
    "description": "Calculate the age of each patient at the time of their appointment.",
    "hint": "Join patients and appointments, use JULIANDAY for age calculation.",
    "expected_sql": "SELECT p.name, a.appointment_date, ROUND((JULIANDAY(a.appointment_date) - JULIANDAY(p.dob)) / 365.25, 1) AS age FROM patients p JOIN appointments a ON p.patient_id = a.patient_id;"
  },
  {
    "id": "q22",
    "title": "Most active doctor",
    "difficulty": "Advanced",
    "description": "Find the doctor with the highest number of appointments.",
    "hint": "Join doctors and appointments, group by doctor name, and use LIMIT.",
    "expected_sql": "SELECT d.name, COUNT(a.appointment_id) AS appointment_count FROM doctors d JOIN appointments a ON d.doctor_id = a.doctor_id GROUP BY d.name ORDER BY appointment_count DESC LIMIT 1;"
  },
  {
    "id": "q23",
    "title": "Patients seen by Cardiologist",
    "difficulty": "Advanced",
    "description": "List all patients who have seen a Cardiologist, including their diagnoses.",
    "hint": "Join all tables and filter by doctor specialty.",
    "expected_sql": "SELECT DISTINCT p.name, a.diagnosis FROM patients p JOIN appointments a ON p.patient_id = a.patient_id JOIN doctors d ON a.doctor_id = d.doctor_id WHERE d.specialty = 'Cardiologist';"
  },
  {
    "id": "q24",
    "title": "Diagnosis frequency by doctor",
    "difficulty": "Advanced",
    "description": "Show the number of times each diagnosis was made by each doctor.",
    "hint": "Join doctors and appointments, group by doctor name and diagnosis.",
    "expected_sql": "SELECT d.name, a.diagnosis, COUNT(a.appointment_id) AS diagnosis_count FROM doctors d JOIN appointments a ON d.doctor_id = a.doctor_id GROUP BY d.name, a.diagnosis;"
  },
  {
    "id": "q25",
    "title": "Appointments per month",
    "difficulty": "Advanced",
    "description": "Show the number of appointments per month in 2023.",
    "hint": "Use STRFTIME to extract the month and GROUP BY.",
    "expected_sql": "SELECT STRFTIME('%Y-%m', appointment_date) AS month, COUNT(appointment_id) AS appointment_count FROM appointments GROUP BY month;"
  },
  {
    "id": "q26",
    "title": "Youngest patient per doctor",
    "difficulty": "Advanced",
    "description": "Find the youngest patient seen by each doctor.",
    "hint": "Join tables, calculate age using JULIANDAY, and group by doctor.",
    "expected_sql": "SELECT d.name, p.name AS patient_name, MIN(ROUND((JULIANDAY(a.appointment_date) - JULIANDAY(p.dob)) / 365.25, 1)) AS age FROM doctors d JOIN appointments a ON d.doctor_id = a.doctor_id JOIN patients p ON a.patient_id = p.patient_id GROUP BY d.name;"
  },
  {
    "id": "q27",
    "title": "Patients with multiple specialties",
    "difficulty": "Advanced",
    "description": "List patients who have seen doctors from more than one specialty.",
    "hint": "Join tables, count distinct specialties, and use HAVING clause.",
    "expected_sql": "SELECT p.name, COUNT(DISTINCT d.specialty) AS specialty_count FROM patients p JOIN appointments a ON p.patient_id = a.patient_id JOIN doctors d ON a.doctor_id = d.doctor_id GROUP BY p.name HAVING COUNT(DISTINCT d.specialty) > 1;"
  },
  {
    "id": "q28",
    "title": "Unique diagnoses per doctor",
    "difficulty": "Advanced",
    "description": "Show the number of unique diagnoses made by each doctor.",
    "hint": "Join doctors and appointments, use COUNT and DISTINCT, group by doctor name.",
    "expected_sql": "SELECT d.name, COUNT(DISTINCT a.diagnosis) AS unique_diagnoses FROM doctors d JOIN appointments a ON d.doctor_id = a.doctor_id GROUP BY d.name;"
  },
  {
    "id": "q29",
    "title": "Patients not seen by Neurologist",
    "difficulty": "Advanced",
    "description": "List patients who have never been seen by a Neurologist.",
    "hint": "Use NOT IN or LEFT JOIN to exclude patients with Neurologist appointments.",
    "expected_sql": "SELECT p.name FROM patients p WHERE p.patient_id NOT IN (SELECT a.patient_id FROM appointments a JOIN doctors d ON a.doctor_id = d.doctor_id WHERE d.specialty = 'Neurologist');"
  },
  {
    "id": "q30",
    "title": "Earliest and latest appointment per patient",
    "difficulty": "Advanced",
    "description": "Show the earliest and latest appointment dates for each patient with appointments.",
    "hint": "Join patients and appointments, use MIN and MAX on appointment_date.",
    "expected_sql": "SELECT p.name, MIN(a.appointment_date) AS earliest_appointment, MAX(a.appointment_date) AS latest_appointment FROM patients p JOIN appointments a ON p.patient_id = a.patient_id GROUP BY p.name;"
  }
]