This might be a relatively simple question but I am struggling to work it out. I have three tables all listed below (owners, pets, petTypes) and trying to extract the following data all in one query. The first two are not difficult but the 3rd and 4th is where I am struggling.
Table data if you want to replicate: https://pastebin.com/veXHwcMc
The questions:
- Owner Id 
- Owner name 
- Owners oldest pet age
- Owners oldest pet type name
- Number of other pets
What I have tried
Selecting the oldest age SELECT MAX(age) FROM pets
Joining the tables to show both SELECT pets.ownerId, MAX(pets.age), petTypes.name FROM pets INNER JOIN petTypes ON pets.petTypeId = petTypes.id GROUP BY pets.ownerId;
But this is wrong. Because they are all showing cat when they should be showing the correct name for the oldest cat.
I cam upon this question: How can I SELECT rows with MAX(Column value), DISTINCT by another column in SQL?
So I attempted: SELECT petTypes.id, petTypes.name FROM petTypes INNER JOIN (SELECT MAX(age) FROM pets GROUP BY ownerId) pets ON petTypes.id = pets.petTypeId;
But the error thrown is ERROR 1054 (42S22): Unknown column 'petTypes.id' in 'on clause'
Any help please

 
     
    