-1

In the given code, we give the subquery the alias of station_num_trips - I'm unable to understand why this is given since it isn't referenced in any other part of the query or visible in the result. The tables used in this are available on bigquery under the dataset "new_york_citibike"

SELECT
    station_id,
    name,
    number_of_rides AS number_of_rides_starting_at_station 
FROM 
    (
        SELECT
            start_station_id,
            COUNT(*) number_of_rides
        FROM bigquery-public-data.new_york_citibike.citibike_trips
        GROUP BY start_station_id
    )
**AS station_num_trips**
INNER JOIN bigquery-public-data.new_york_citibike.citibike_stations 
ON station_id = start_station_id
ORDER BY number_of_rides DESC 
Mikhail Berlyant
  • 165,386
  • 8
  • 154
  • 230
  • Because that's how the language is defined. You may as well ask why an alias is ever allowed not to be given. Or why there are any joins but NATURAL JOINs since they don't need aliases. Asking "why" a language has or doesn't have a feature is seldom helpful or answerable by other than its designers. It's also not really re practical programming--[help/on-topic]. Moreover "why" questions generally are problematically vague. [Is asking "why" on language specifications still considered as "primarily opinion-based" if it can have official answers?](https://meta.stackoverflow.com/q/323334/3404097) – philipxy Nov 01 '21 at 09:50
  • This is a faq. Please before considering posting read the manual & google any error message & many clear, concise & precise phrasings of your question/problem/goal, with & without your particular names/strings/numbers, 'site:stackoverflow.com' & tags; read many answers. Reflect your research. See [ask], [Help] & the voting arrow mouseover texts. If you post a question, use one phrasing as title. – philipxy Nov 01 '21 at 10:04

1 Answers1

0

Some RDBMS require such subqueries to have an alias, so adding an alias can make your query more portable. An alias can also serve as documentation of what purpose the subquery serves or what data it has.

ysth
  • 96,171
  • 6
  • 121
  • 214