I have three tables:
houses
id | square_feet
------------------
1 | 2000
2 | 1600
3 | 1000
energies
id | house_id | kwh
-------------------------
6 | 1 | 10
7 | 2 | 100
8 | 3 | 200
cars
id | energy_id | gallons
--------------------------
11 | 6 | 20
12 | 6 | 40
13 | 7 | 200
14 | 8 | 77
15 | 8 | 88
I need to get the average square feet and average kwh for either all houses or just the houses with a certain number of cars.
So for two cars, I want values for houses 1 and 3. For one car, I want values only for house 2. For all houses, I want 1, 2 and 3.
For any all houses I am using:
SELECT AVG(houses.square_feet) AS avg_sf, AVG(energies.kwh) AS avg_kwh
FROM (houses LEFT JOIN energies ON houses.id = energies.house_id)
How do I find the average square feet and average kwh for houses that have two cars?