Correlated Subqueries II #6

<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.>
https://www.codecademy.com/courses/sql-table-transformation/lessons/subqueries/exercises/correlated-ii?action=lesson_resume

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>
Disclaimer: Squeaky clean newb here. I believe I may be over thinking this. The code is fine, I got the answer from another post but I still want to know why and/or what is going on.

Specifically:
WHERE f.id < flights.id
AND f.origin=flights.origin

So f.id is calling f (“flights” table) and the column “id” from it, right? But then we’re trying to find where that is less than “flights” table and… column id (…again??)

How are we able to find an id column less than another id column from the same table?
Same question for the f.origin line.

```

SELECT origin, id,
(
SELECT COUNT(*)
FROM flights f
WHERE f.id < flights.id
AND f.origin=flights.origin
) + 1
AS flight_sequence_number
FROM flights;

<do not remove the three backticks above>
3 Likes

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