Middle
Write an SQL query: select patients who have visited more than four different doctors
sobes.tech AI
Answer from AI
Suppose there is a table visits with columns patient_id and doctor_id. To select patients who have visited more than four different doctors, you can use the following SQL query:
SELECT patient_id
FROM visits
GROUP BY patient_id
HAVING COUNT(DISTINCT doctor_id) > 4;
This query groups records by patients and selects only those with a number of unique doctors greater than four.