Need some explanation on the significance of 'a'


#1

SELECT a.dep_month,
a.dep_day_of_week,
AVG(a.flight_count) AS average_flights
FROM (
SELECT dep_month,
dep_day_of_week,
dep_date,
COUNT(*) AS flight_count
FROM flights
GROUP BY 1,2,3
) a
GROUP BY 1,2
ORDER BY 1,2;

Hello everyone, someone help explain to what ‘a’ represent in this query. I have replace it with other variable it work correctly but I don’t have any logical explanation.
I am not very verse with sql
Thanks for the help


#2
SELECT dep_month,
dep_day_of_week,
dep_date,
COUNT(*) AS flight_count
FROM flights
GROUP BY 1,2,3
) a

In the above, a is the alias given to the subquery, and referred to by,

SELECT a.dep_month,
a.dep_day_of_week,
AVG(a.flight_count) AS average_flights

If we treat a as a context, the columns are attributes of that context.

a.dep_month
a.dep_day_of_week
a.flight_count

#3

Thanks very much for the explanation. It very helpful


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.