Posts

Showing posts with the label sql

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 ...

SQL ZOO 試答:SELECT names

SELECT names Find the country that start with Y SELECT name FROM world WHERE name LIKE 'Y%' Find the countries that end with y SELECT name FROM world WHERE name LIKE '%y' Find the countries that contain the letter x SELECT name FROM world WHERE name LIKE '%x%' Find the countries that end with land SELECT name FROM world WHERE name LIKE '%land' Find the countries that start with C and end with ia SELECT name FROM world WHERE name LIKE 'C%ia' Find the country that has oo in the name SELECT name FROM world WHERE name LIKE '%oo%' Find the countries that have three or more a in the name SELECT name FROM world WHERE name LIKE '%a%a%a%' Find the countries that have "t" as the se...