Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>How does the following query perform? It works by first finding the northmost Y cordinate in the set of airports visited. Then an identical query is performed which is filtered by the Y coordinate in the previous query. The final step is to find the airport.</p> <pre><code>drop table airports; drop table flights; create table airports( apid int not null ,apname varchar(50) not null ,x int not null ,y int not null ,primary key(apid) ,unique(apname) ); create table flights( flight_id int not null auto_increment ,src_apid int not null ,dst_apid int not null ,user_id varchar(20) not null ,foreign key(src_apid) references airports(apid) ,foreign key(dst_apid) references airports(apid) ,primary key(flight_id) ,index(user_id) ); insert into airports(apid, apname, x, y) values(1, 'Northpole Civilian', 50, 100); insert into airports(apid, apname, x, y) values(2, 'Northpole Military', 50, 100); insert into airports(apid, apname, x, y) values(3, 'Transit point', 50, 50); insert into airports(apid, apname, x, y) values(4, 'Southpole Civilian', 50, 0); insert into airports(apid, apname, x, y) values(5, 'Southpole Military', 50, 0); insert into flights(src_apid, dst_apid, user_id) values(4, 3, 'Family guy'); insert into flights(src_apid, dst_apid, user_id) values(3, 1, 'Family guy'); insert into flights(src_apid, dst_apid, user_id) values(5, 3, 'Mr Bazooka'); insert into flights(src_apid, dst_apid, user_id) values(3, 2, 'Mr Bazooka'); select airports.apid ,airports.apname ,airports.x ,airports.y from (select max(a.y) as y from flights f join airports a on (a.apid = f.src_apid or a.apid = f.dst_apid) where f.user_id = 'Family guy' ) as northmost join (select a.apid ,a.y from flights f join airports a on (a.apid = f.src_apid or a.apid = f.dst_apid) where f.user_id = 'Family guy' ) as userflights on(northmost.y = userflights.y) join airports on(userflights.apid = airports.apid); </code></pre> <p><strong>Edit. Alternative query that may be less confusing to the optimizer</strong> </p> <pre><code>select airports.* from (select case when s.y &gt; d.y then s.apid else d.apid end as apid ,case when s.y &gt; d.y then s.y else d.y end as northmost from flights f join airports s on(f.src_apid = s.apid) join airports d on(f.dst_apid = d.apid) where f.user_id = 'Family guy' order by northmost desc limit 1 ) as user_flights join airports on(airports.apid = user_flights.apid); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload