Subqueries - equivalence

<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.>

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

My code works ok, I just have a general question:

Here is my code:
<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 origin, id, (SELECT COUNT(*) FROM flights f WHERE f.id < flights.id AND f.origin = flights.origin) +1 AS flight_sequence_number FROM flights; ```

My question: is this equivalent to the code below?

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

Thanks.

1 Like

No. They are not the same. The second will give you the same answer only if f.origin = flights.origin WHERE f.id = flights.id. Else flight_sequence_number will be equal to the first one -1

1 Like

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