I'm learning SQL and have been going through the chapters of the GalaXQL app.
I've written the following query in answer to the question "Hilight the star (or stars) which has the planet with the highest orbit distance in the galaxy. Remember to clear the old hilights before beginning."
This query returns the result starid:23334, which when inserted into the hilight table, allows me to progress. However, the program hangs for a long, long time before handing this result back to me, so I suspect there's a far more efficient way to pull this information from the database.
This query works, but takes a long time to process, how can I improve this query?
INSERT INTO hilight
SELECT DISTINCT s.starid
FROM planets AS p, stars AS s, moons AS m
WHERE s.starid=(SELECT starid
FROM planets
WHERE orbitdistance=(SELECT MAX(orbitdistance)
FROM planets));
My logic behind this query structure
First find the planet with the greatest orbit distance from the table "planets".
Second to compare the value of "highest orbit distance" to the "orbitdistance" field of the table "planets" and to return the "starid" that relates to that field.
Third to compare the value of the field "starid" in the table "planets" to the field "starid" in the table "stars" and then insert that "starid" to the table "hilight".
Data structure: