Subqueries - equivalence


#1

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

Here is my code:



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.


#2

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


#3

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