SELECT a.dep_month,
a.dep_day_of_week,
AVG(a.flight_distance) AS average_distance
FROM (
SELECT dep_month,
dep_day_of_week,
dep_date,
sum(distance) AS flight_distance
FROM flights
GROUP BY 1,2,3
) a
GROUP BY 1,2
ORDER BY 1,2;
Im confused as to why there is a little a after the close bracket of the inner query (right above the GROUP BY 1,2)
If anyone could explain this that would be super helpful
thanks!
I was also wondering why my code did not work without that lower case “a”.
I’ve also done the SQL courses on Khan Academy where this “syntax” was used, but worked without typing that letter at the end of the inner query, if I remember correctly.
the lower case ‘a’ is the alias for the table from the inner query. Which is how the outer query is getting their results of a.dep_month etc. Without the ‘a’, you would be getting an error, or you basically wouldn’t get any results due to the fact that those would basically mean nothing to the program.