Q5 - Why do I need to specify "flight AS f" or carrier as "f.carrier"?

<PLEASE USE THIS TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>

I’m stuck here
https://www.codecademy.com/courses/sql-table-transformation/lessons/subqueries/exercises/correlated-i?action=lesson_resume

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>

I got the code to work and I have the correct answer. I just don’t know why I have to have an alias for flights As f in line 2. But in line 5, I say “from flights,” not “from f.” And why do I have to specify that it’s coming from carrier = f.carrier?

<In this course, it often helps to include a screenshot of your whole web browser – that lets everyone see what you see. If you wish to include a screenshot, add it below this line.>

<If you wish to copy/paste in your code, you can use this next section. This will allow others to copy/paste your code for testing – something that they won’t be able to do with just a screenshot.>

```

SELECT id
FROM flights AS f
WHERE distance < (
SELECT AVG(distance)
FROM flights
WHERE carrier = f.carrier
);

<do not remove the three backticks above>

I think f is an indicator of the id you are working on, so that the code could calculate the average distance of the carrier of the id (indicated by that f)

You should always use aliases in SQL.
Over time Your Queries will get complexer. Adding aliases for all tables improves the readability immensely and is generally best practice. Also you wanna have the one and same alias for one table in all selects. That also makes things easier to read/understand.

SELECT id
FROM flights a
WHERE distance < (
 SELECT AVG(distance)
 FROM flights b
 WHERE a.carrier = b.carrier);
2 Likes

Why must write WHERE a.carrier=b.carrier?

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