SELECT c.name, cy.name,cy.population FROM country c, city cy where c.capital=cy.id order by cy.population desc limit 5; 2. 查詢國家語言(Countrylanguage)中國碼(countrycode)為”SGP”的所有欄位資料。
SELECT * FROM countrylanguage where countrycode = "sgp"; 3. 查詢亞洲(asia)及歐洲(europe)GNP的總額,請顯示洲名及將GNP的總額別名為TotalGNP。
SELECT continent, sum(gnp) as TotalGNP FROM country where continent = "asia" or continent = "europe" group by continent; 4. 查詢城市(City)資料中人口(Population)超過9000000的所有欄位資料。
SELECT * FROM city where population >9000000; 5. 單一地顯示出全世界所有的官方語言(isofficial為T)名稱,語言名稱(language)請勿重覆。
…等等 SELECT distinct language FROM countrylanguage where isofficial = "T"; 6. 查詢各洲中(continent)的國家的個數, 請顯示洲名及國家的個數。
SELECT continent, count(*) FROM country group by continent; 7. 查詢國碼(countrycode)為USA城市名稱(name),人口數(population)。
…等等 SELECT name, population FROM city where countrycode = "usa"; 8. 查詢全世界最晚獨立(IndepYear)的國家名稱(Name)及獨立年份(IndepYear)。
SELECT name , indepyear FROM country order by indepyear desc limit 1; 9. 查詢中東(Middle East)地區(Region)的國家名稱及國家型態(GovernmentForm)。
…等等 SELECT name, governmentform FROM country where region ="middle east" 10. 查詢中東(Middle East)地區(Region)的地區名稱,及表面積(Surfacearea)之總合(請將其別名為TotalArea)。
SELECT region, sum(surfacearea) as totalarea FROM country where region ="middle east" group by region;
10 種inner join 之多表格結合。 Ex. 所有歐洲的城市名稱, 及城市人口數。 SELECT c.name, c.population FROM city c, country cn where c.countrycode = cn.code and cn.continent = "europe"; Ex. 顯示出語言中有包含"Chinese"字串的國名(country name), 語言(Language), 是否為官方語(IsOffical), 百分比(Percentage); 依百分比由大 到小排序 。 SELECT c2.name, c.language, c.IsOfficial, c.Percentage FROM countrylanguage c, country c2 where c.countrycode = c2.code and c.language like "%chinese%" order by percentage desc;
8 種聚合函數使用範例。 Ex. 各洲人口統計,顯示出洲名及人口數,依洲名排序。 SELECT continent, sum(population) FROM country group by continent order by continent;
5 種子查詢之運用。 Ex. 單一的顯示出在北美洲(North America) 所說的官方(IsOfficial= true)語言名稱(Language)。 SELECT distinct language FROM countrylanguage WHERE isofficial = true and countrycode in (SELECT Code FROM country c WHERE Continent = "North America");