title: SQL - UNION, UNION ALL, INTERSECT, EXCEPT
body: <p>I am learning SQL with GalaXQL and can't figure out the following question:</p>
  
  <blockquote>
    <p>Build a query which returns starids from planets. The starids should be selected so that for each starid (x) in the list:
    - there should exist a planet with a starid that's three times x but
    - there should not exist a planet with starid two times x.
    Only use starid:s from the planets table.</p>
  </blockquote>
  
  <p>What would be a query that would accomplish this using UNION, UNION ALL, INTERSECT, EXCEPT?
  Please digest this in your answer as I am a beginner.
  Thank you in advance!</p>
  
  <p>Here is the database schema:</p>
  
  <pre><code>CREATE TABLE stars (starid INTEGER PRIMARY KEY,
                      name TEXT,
                      x DOUBLE NOT NULL,
                      y DOUBLE NOT NULL,
                      z DOUBLE NOT NULL,
                      class INTEGER NOT NULL,
                      intensity DOUBLE NOT NULL);
  
  CREATE TABLE hilight (starid INTEGER UNIQUE);
  
  CREATE TABLE planets (planetid INTEGER PRIMARY KEY,
                        starid INTEGER NOT NULL,
                        orbitdistance DOUBLE NOT NULL,
                        name TEXT,
                        color INTEGER NOT NULL,
                        radius DOUBLE NOT NULL);
  
  CREATE TABLE moons (moonid INTEGER PRIMARY KEY,
                      planetid INTEGER NOT NULL,
                      orbitdistance DOUBLE NOT NULL,
                      name TEXT,
                      color INTEGER NOT NULL,
                      radius DOUBLE NOT NULL);
  
  CREATE INDEX planets_starid ON planets (starid);
  CREATE INDEX moons_planetid ON moons (planetid);
  </code></pre>
  
  <p>Here is my query:</p>
  
  <pre><code>SELECT planets.starid
  FROM planets
  UNION
  SELECT starid*3 FROM planets
  EXCEPT
  SELECT starid*2 FROM planets
  </code></pre>