SQL ZOO
記錄一下我跟別人不同的寫法,以後對 SQL 語法的效能更有感之後,再回頭來看看有什麼差別。 目前還不太確定 SQL 語法的 coding style,例如縮排要空幾格、怎麼分行之類的。 SELECT in SELECT 8. List each continent and the name of the country that comes first alphabetically. 我的 SELECT continent, name FROM world x WHERE name = (SELECT name FROM world y WHERE y.continent = x.continent ORDER BY name LIMIT 1); 別人的 SELECT continent, name FROM world x WHERE name <= ALL(SELECT name FROM world y WHERE y.continent = x.continent) 不過我用 ORDER 應該會比較慢,最快的 merge sort,時間複雜度是 O(n log n)。別人用 <= ALL,時間複雜度應該是 O(n)。 9. Find the continents where all countries have a population <= 25000000. Then find the names of the countries associated with these continents. Show name, continent and population. 我的 SELECT name, continent, population FROM world WHERE continent NOT IN (SELECT continent FROM world WHERE population > 25000000); 別人的 SELECT name, continent, population FROM world WHERE continent IN (SELECT continent FROM world x WHERE 25000000 ...