SELECT within SELECT
1.哪些國家的GDP比Europe歐洲的全部國家都要高呢? [只需列出 name 。] (有些國家的記錄中,GDP是NULL,沒有填入資料的。)
select name from world where gdp > ALL (select gdp from world where continent='Europe' and gdp>0)
2.在每一個州中找出最大面積的國家,列出洲份 continent, 國家名字 name 及面積 area。 (有些國家的記錄中,AREA是NULL,沒有填入資料的。)
SELECT continent, name, area FROM world x
WHERE area >= ALL
(SELECT area FROM world y
WHERE y.continent=x.continent
AND area >0)
3.列出洲份名稱,和每個洲份中國家名字按子母順序是排首位的國家名。(即每洲只有列一國)
select continent,name from world a where name = (select name from world b where a.continent = b.continent order by name limit 1)
4.找出洲份,當中全部國家都有少於或等於 25000000 人口. 在這些洲份中,列出國家名字name,continent 洲份和population人口。
SELECT name,continent,population FROM world x
WHERE 25000000>=ALL(SELECT population FROM world y WHERE
y.continent=x.continent AND population>0)
5.有些國家的人口是同洲份的所有其他國的3倍或以上。列出 國家名字name 和 洲份 continent。
select name,continent from world a
where population >= ALL (select population*3 from world b where a.continent = b.continent and population>0 and a.name!=b.name)
The JOIN operation
3.語句FROM 表示合拼兩個表格game 和 goal的數據。語句 ON 表示如何找出 game中每一列應該配對goal中的哪一列 – goal的 id 必須配對game的 matchid 。 簡單來說,就是
ON (game.id=goal.matchid)
以下SQL列出每個入球的球員(來自goal表格)和場館名(來自game表格)修改它來顯示每一個德國入球的球員名,隊伍名,場館和日期。
SELECT b.player,b. teamid ,a.stadium,a.mdate
FROM game a JOIN goal b ON a.id=b.matchid
where b.teamid ='GER'