explain analyze
select seat_no
, passenger_name
, contact_data
from
(
select p.seat_no
, t.passenger_name
, t.contact_data
, max(case when t.passenger_name = 'MIKHAIL VOROBEV' then 1 end ) over(partition by substring(p.seat_no,'\d+')) m
from boarding_passes p
join tickets t on t.ticket_no = p.ticket_no
where p.flight_id =
( select f.flight_id
from flights f
where f.flight_no = 'PG0510'
and f.scheduled_departure >= '2017-07-16'
and f.scheduled_departure < '2017-07-17'
)
) t
where m = 1
and t.passenger_name <> 'MIKHAIL VOROBEV'
order by seat_no
;