There are 3 tables:
- medics:
- IDM (id_medic) (primary key)
- 1st name
- 2nd name
- specialty
- patients:
- IDP (id_patient) (primary key)
- name
- DOB (date of birth)
- visits:
- id
- id_medic
- id_patient
I would like to find out the average age of patients for each specialty.
SELECT specialty, AVG(year(curdate()) - year(patients.DOB))
FROM medics, patients, visits
WHERE medics.IDM = visits.medics GROUP by specialty;
The query above shows me on each line the average of all patients.