how sql join works on 3 tables with example

I hope it will full fill your requirement

SELECT R.AREA_ID,
       COUNT(A.DISEASEID) ATTACKED_PEOPLE
  FROM AREA R
  LEFT JOIN ATTACK A
    ON A.AREAID=R.AREA_ID
  LEFT JOIN DISEASE D
    ON D.SISEASE_ID=A.DISEASEID
  GROUP BY R.AREA_ID;

Here we have used joining of three tables using LEFT JOIN concept. So the left side table (AREA) retrieves all the records and the right side table (ATTACK) retrieves only matched records with left side table. As well as second LEFT JOIN condition is for retrieving the count of diseases from ATTACK table. So that we can get count of people who were attacked and who were not using the above query.

Leave a Comment