select x.nr_line as Pierwsza, y.nr_line as druga from Course x, Course y,
(
select top 1 A.course_id as startt, B.course_id as endd, A.stop_id, A._time as Atime, B._time as Btime, DATEDIFF(minute, A._time, B._time) as Diff from
(select course_id, stop_id, _time from TimeTable where TimeTable.course_id in
(select course_id from TimeTable join Stops on TimeTable.stop_id = Stops.stop_id where Stops.stop_name = 'Arkadia') ) A
join
(select course_id, stop_id, _time from TimeTable where TimeTable.course_id in
(select course_id from TimeTable join Stops on TimeTable.stop_id = Stops.stop_id where Stops.stop_name = 'Sadyba') ) B
on
A.stop_id = B.stop_id AND A.course_id != B.course_id
where
A._time < B._time
order by Diff
) z
where
x.course_id = z.startt AND y.course_id = z.endd
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}