Working with MTA API.
stop_times table looks like this:
trip_id, stop_id
trip_id is repeated as stop_ids are listed per row. Example:
1111, 1
1111, 2
1111, 3
2222, 1
2222, 3
3333, 1
3333, 2
Goal is to select trip_id of a train that we know will definitely stop at two specific stations. If we want trains that will stop at 1 and 3, we will get trips 1111, and 2222. Or if 1 and 2, then 1111 and 3333.
Here's what I wrote quickly, and of course it runs rather slow:
SELECT trip_id
FROM stop_times
WHERE stop_id=##
AND trip_id IN (SELECT trip_id FROM stop_times WHERE stop_id=##)
Basically, I am trying to do the equivalent of MS SQL INTERSECT.
How can I optimize this to run better?