Lowercase "a" after inner query

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 :smiley:
thanks!

1 Like

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.

Curious for an answer as well.

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.

Hi,

I have been Ckeck your Query in my sql Server. If you dont Write small ‘a’ then result should not be the change. So, Dont be confused.

THANK YOU,
Divyesh Padamani

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