一、SELECT basics/zh
-
以顯示德國 Germany 的人口。
select population from world where name = 'Germany';
-
查詢面積為 5,000,000 以上平方公里的國家,對每個國家顯示她的名字和人均國內生產總值(
gdp/population
)。select name, gdp/population from world where area > 5000000;
-
顯示“Ireland 愛爾蘭”,“Iceland 冰島”,“Denmark 丹麥”的國家名稱和人口。
select name, population from world where name IN ('Ireland ', 'Iceland ', 'Denmark ');
-
以顯示面積為 200,000 及 250,000 之間的國家名稱和該國面積。
select name, area from world where area BETWEEN 200000 AND 250000;
二、SELECT names/zh
-
找出以 Y 為開首的國家。
select name from world where name LIKE 'Y%';
-
找出以 Y 為結尾的國家。
SELECT name FROM world WHERE name LIKE '%Y'
-
找出所有國家,其名字包括字母x。
SELECT name FROM world WHERE name LIKE '%x%'
-
找出所有國家,其名字以 land 作結尾。
SELECT name FROM world WHERE name LIKE '%land'
-
找出所有國家,其名字以 C 作開始,ia 作結尾。
SELECT name FROM world WHERE name LIKE 'C%ia'
-
找出所有國家,其名字包括字母oo。
SELECT name FROM world WHERE name LIKE '%oo%'
-
找出所有國家,其名字包括三個或以上的a。
SELECT name FROM world WHERE name LIKE '%a%a%a%'
-
找出所有國家,其名字以t作第二個字母。
SELECT name FROM world WHERE name LIKE '_t%' ORDER BY name
-
找出所有國家,其名字都有兩個字母 o,被另外兩個字母相隔着。
SELECT name FROM world WHERE name LIKE '%o__o%'
-
找出所有國家,其名字都是 4 個字母的。
SELECT name FROM world WHERE name LIKE '____'
-
顯示所有國家名字,其首都和國家名字是相同的。
select name from world where name = capital;
-
顯示所有國家名字,其首都是國家名字加上”City”。
select name from world where capital = concat(name,' City');
-
找出所有首都和其國家名字,而首都要有國家名字中出現。
select capital,name from world where capital like concat('%',name,'%');
-
找出所有首都和其國家名字,而首都是國家名字的延伸你應顯示 Mexico City,因它比其國家名字 Mexico 長。
你不應顯示 Luxembourg,因它的首都和國家名相是相同的。select name,capital from world where capital like concat('%',name,'%') and capital != name;
-
顯示國家名字,及其延伸詞,如首都是國家名字的延伸。你可以使用SQL函數 REPLACE 或 MID.
SELECT name,REPLACE(capital, name, '') FROM world WHERE capital LIKE CONCAT('%',name,'%') AND REPLACE(capital, name, '') != '';
三、SELECT from WORLD Tutorial/zh
-
觀察運行一個簡單的SQL命令的結果。
SELECT name, continent, population FROM world
-
顯示具有至少2億人口的國家名稱。 2億是200000000,有八個零。
SELECT name FROM world WHERE population>200000000;
-
找出有至少200百萬(2億)人口的國家名稱,及人均國內生產總值。求助:如何人均國內生產總值計算
select name,gdp/population from world where population>200000000;
-
顯示'South America'南美洲大陸的國家名字和以百萬為單位人口數。 將人口population 除以一百萬(1000000得可得到以百萬為單位人口數。
select name,population/1000000 from world where continent = 'South America';
-
顯示法國,德國,意大利(France, Germany, Italy)的國家名稱和人口。
select name,population from world where name in ('France','Germany','Italy');
-
顯示包含單詞“United”為名稱的國家。
select name from world where name like '%United%';
-
展示大國的名稱,人口和面積。
select name,population,area from world where area>3000000 or population>250000000
-
顯示以人口或面積為大國的國家,但不能同時兩者。顯示國家名稱,人口和面積。
select name,population,area from world where (area>3000000 and population<250000000) or (area<3000000 and population>250000000) and name not in ('USA', 'India', 'China')
-
對於南美顯示以百萬計人口,以十億計2位小數GDP。
select name,round(population/1000000,2),round(gdp/1000000000,2) from world where continent ='South America';
-
顯示萬億元國家的人均國內生產總值,四捨五入到最近的$ 1000。
select name,round(gdp/population,-3) from world where gdp>1000000000000;
-
Show the name - but substitute Australasia for Oceania - for countries beginning with N.
select name,case when continent='Oceania' then 'Australasia' else continent end from world where name like 'N%';
-
Show the name and the continent - but substitute Eurasia for Europe and Asia; substitute America - for each country in North America or South America or Caribbean*. Show countries beginning with A or B
select name,case when continent in ('Europe','Asia') then 'Eurasia' when continent in('North America','South America','Caribbean') then 'America' else continent end from world where name like 'A%' or name like 'B%';
-
Show the name, the original continent and the new continent of all countries.
三、SELECT from Nobel Tutorial/zh
-
更改查詢以顯示1950年諾貝爾獎的獎項資料。
SELECT yr,subject,winner FROM nobel WHERE yr = 1950;
-
顯示誰贏得了1962年文學獎(Literature)。
SELECT winner FROM nobel WHERE yr = 1962 AND subject = 'Literature';
-
顯示“愛因斯坦”('Albert Einstein') 的獲獎年份和獎項。
select yr,subject from nobel where winner = 'Albert Einstein';
- 顯示2000年及以後的和平獎(‘Peace’)得獎者。
select winner from nobel where yr >=2000 and subject ='Peace';
- 顯示1980年至1989年(包含首尾)的文學獎(Literature)獲獎者所有細節(年,主題,獲獎者)。
select * from nobel where yr between 1980 and 1989 and subject='Literature';
- 顯示總統獲勝者的所有細節:
- 西奧多•羅斯福 Theodore Roosevelt
- 伍德羅•威爾遜 Woodrow Wilson
- 吉米•卡特 Jimmy Carter
select * from nobel where winner in ('Theodore Roosevelt','Woodrow Wilson','Jimmy Carter');
- 顯示名字為John 的得獎者。 (注意:外國人名字(First name)在前,姓氏(Last name)在後)
select winner from nobel where winner like 'John%';
- 顯示1980年物理學(physics)獲獎者,及1984年化學獎(chemistry)獲得者。
select * from nobel where (yr=1980 and subject='physics') or (yr=1984 and subject ='chemistry');
- 查看1980年獲獎者,但不包括化學獎(Chemistry)和醫學獎(Medicine)。
select * from nobel where subject not in ('Chemistry','Medicine') and yr=1980;
- 顯示早期的醫學獎(Medicine)得獎者(1910之前,不包括1910),及近年文學獎(Literature)得獎者(2004年以後,包括2004年)。
select * from nobel where (yr<1910 and subject = 'Medicine') or (yr>=2004 and subject = 'Literature');
- Find all details of the prize won by PETER GRÜNBERG
select * from nobel where winner ='PETER GRÜNBERG';
- 查找尤金•奧尼爾EUGENE O'NEILL得獎的所有細節 Find all details of the prize won by EUGENE O'NEILL
select * from nobel where winner = 'EUGENE O\'NEILL';
- 列出爵士的獲獎者、年份、獎頁(爵士的名字以Sir開始)。先顯示最新獲獎者,然後同年再按名稱順序排列。
select winner,yr,subject from nobel where winner like 'Sir%' order by yr desc,winner asc;
- Show the 1984 winners and subject ordered by subject and winner name; but list Chemistry and Physics last.
select winner,subject from nobel where yr=1984 order by subject in ('Chemistry','Physics'),subject,winner;
四、SELECT within SELECT Tutorial/zh
-
列出每個國家的名字 name****,當中人口 population 是高於俄羅斯'Russia'的人口。
select name from world where population >(select population from world where name = 'Russia');
-
列出歐州每國家的人均GDP,當中人均GDP要高於英國'United Kingdom'的數值。
select name from world where continent = 'Europe' and gdp/population >(select gdp/population from world where name = 'United Kingdom');
-
在阿根廷****Argentina 及 澳大利亞 Australia****所在的洲份中,列出當中的國家名字 name 及洲分 continent 。按國字名字順序排序
select name,continent from world where continent in ( select continent from world where name in ('Argentina ','Australia'))order by name;
-
哪一個國家的人口比加拿大Canada的多,但比波蘭Poland的少?列出國家名字name和人口population 。
select name,population from world where population >(select population from world where name='Canada') and population < (select population from world where name='Poland')
-
顯示歐洲的國家名稱name和每個國家的人口population。以德國的人口的百分比作人口顯示。
select name,concat(round(100*population/(select population from world where name = 'Germany'),0),'%') from world where continent ='Europe';
-
哪些國家的GDP比Europe歐洲的全部國家都要高呢? [只需列出 name 。] (有些國家的記錄中,GDP是NULL,沒有填入資料的。)
select name from world where gdp>all(select gdp from world where continent ='Europe' and gdp>0);
-
在每一個州中找出最大面積的國家,列出洲份 continent, 國家名字 name **及面積 **area。 (有些國家的錄中,AREA是NULL,沒有填入資料的。)
select continent,name,area from world x where area >= all( select area from world y where x.continent=y.continent and area >0);
-
列出洲份名稱,和每個洲份中國家名字按子母順序是排首位的國家名。(即每洲只有列一國)
select continent,name from world x where x.name = ( select y.name from world y where x.continent=y.continent order by y.name limit 1);
-
找出洲份,當中全部國家都有少於或等於 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);
-
有些國家的人口是同洲份的所有其他國的3倍或以上。列出 國家名字name 和 洲份 continent。
select name,continent from world x where population >=all( select 3*population from world y where x.continent = y.continent and y.population>0 and x.name != y.name);
五、SUM and COUNT/zh
-
展示世界的總人口。
SELECT sum(population) FROM world;
-
列出所有的洲份, 每個只有一次。
select distinct continent from world;
-
找出非洲(Africa)的GDP總和。
select sum(gdp) from world where continent = 'Africa';
-
有多少個國家具有至少百萬(1000000)的面積。
select count(name) from world where area >= 1000000;
-
('France','Germany','Spain')(“法國”,“德國”,“西班牙”)的總人口是多少?
select sum(population) from world where name in ('France','Germany','Spain');
-
對於每一個洲份,顯示洲份和國家的數量。
select continent,count(name) from world group by continent;
-
對於每一個洲份,顯示洲份和至少有1000萬人(10,000,000)口國家的數目。
select continent,count(name) from world where population >= 10000000 group by continent;
-
列出有至少100百萬(1億)(100,000,000)人口的洲份。
select continent from world group by continent having sum(population) >= 100000000;
六、The nobel table can be used to practice more SUM and COUNT functions./zh
-
找出總共有多少個獎頒發了。
select count(subject) from nobel;
-
列出每一個獎項(subject), 只列一次
select distinct subject from nobel
-
找出物理獎的總頒發次數。
select count(subject) from nobel where subject = 'Physics'
-
對每一個獎項(Subject),列出頒發數目。
select subject,count(1) from nobel group by subject
-
對每一個獎項(Subject),列出首次頒發的年份。
select subject,min(yr) from nobel group by subject
-
對每一個獎項(Subject),列出2000年頒發的數目。
select subject,count(*) from nobel where yr=2000 group by subject
-
對每一個獎項(Subject),列出有多少個不同的得獎者。
select subject,count(distinct winner) from nobel group by subject
-
對每一個獎項(Subject),列出有多少年曾頒發過。
select subject,count(distinct yr) from nobel group by subject
-
列出哪年曾同年有3個物理獎Physics得獎者。
select yr from nobel where subject = 'Physics' group by yr having count(*)=3
-
列出誰得獎多於一次。
select winner from nobel group by winner having count(*)>1
-
列出誰獲得多於一個獎項(Subject)
select winner from nobel group by winner having count(distinct subject) > 1
-
哪年哪獎項,是同一獎項(subject)頒發給3個人。只列出2000年及之後的資料。
select yr,subject from nobel where yr>=2000 group by yr,subject having count(*) = 3
七、The JOIN operation/zh
-
賽事編號matchid 和球員名 player ,該球員代表德國隊Germany入球的。要找出德國隊球員,要檢查:
teamid = 'GER'
select matchid,player from goal left join eteam on goal.teamid=eteam.id where teamid = 'GER'
-
只顯示賽事1012的 id, stadium, team1, team2
select id,stadium,team1,team2 from game where id = 1012
-
修改它來顯示每一個德國入球的球員名,隊伍名,場館和日期。
select player,teamid,stadium,mdate from game,goal where goal.matchid=game.id and goal.teamid = 'GER'
-
列出球員名字叫Mario (
player LIKE 'Mario%'
)有入球的 隊伍1 team1, 隊伍2 team2 和 球員名 playerselect team1,team2,player from goal left join game on goal.matchid=game.id where player like 'Mario%'
-
列出每場球賽中首10分鐘
gtime<=10
有入球的球員player
, 隊伍teamid
, 教練coach
, 入球時間gtime
select player,teamid,coach,gtime from goal,eteam where goal.teamid = eteam.id and gtime <= 10
-
列出'Fernando Santos'作為隊伍1 team1 的教練的賽事日期,和隊伍名。
select mdate,teamname from game,eteam where game.team1 = eteam.id and coach = 'Fernando Santos'
-
列出場館 'National Stadium, Warsaw'的入球球員。
select player from goal go,game ga where go.matchid = ga.id and ga.stadium = 'National Stadium, Warsaw'
-
只列出全部賽事,射入德國龍門的球員名字。
select distinct player from goal go left join game ga on go.matchid = ga.id where (ga.team1 = 'GER' or ga.team2 = 'GER') and go.teamid != 'GER'
-
列出隊伍名稱 teamname 和該隊入球總數
select teamname,count(1) from eteam e left join goal on e.id = goal.teamid group by e.id order by teamname
-
列出場館名和在該場館的入球數字。
select stadium,count(1) from game inner join goal on game.id = goal.matchid group by stadium order by stadium
-
每一場波蘭'POL'有參與的賽事中,列出賽事編號 matchid, 日期date 和入球數字。
select matchid,mdate,count(1) from game inner join goal on game.id = goal.matchid where team1 ='POL'or team2='POL' group by matchid
-
每一場德國'GER'有參與的賽事中,列出賽事編號 matchid, 日期date 和德國的入球數字。
select matchid,mdate,count(1) from game right join goal on game.id = goal.matchid where teamid = 'GER' group by matchid
-
List every match with the goals scored by each team as shown. This will use "CASE WHEN" which has not been explained in any previous exercises.
select mdate,team1,sum(case team1 when teamid then 1 else 0 end) score1,team2,sum(case team2 when teamid then 1 else 0 end) score2 from game left join goal on game.id = goal.matchid group by id order by mdate,id
八、More JOIN operations/zh
-
列出1962年首影的電影, [顯示 id, title]
select id,title from movie where yr = 1962
-
電影大國民 'Citizen Kane' 的首影年份。
select yr from movie where title = 'Citizen Kane'
-
列出全部Star Trek星空奇遇記系列的電影,包括id, title 和 yr(此系統電影都以Star Trek為電影名稱的開首)。按年份順序排列。
select id,title,yr from movie where title like 'Star Trek%' order by yr
-
id是 11768, 11955, 21191 的電影是什麼名稱?
select title from movie where id in (11768, 11955, 21191)
-
女演員'Glenn Close'的編號 id是什麼?
select id from actor where name = 'Glenn Close'
-
電影北非諜影'Casablanca' 的編號 id是什麼?
select id from movie where title = 'Casablanca'
-
使用 movieid=11768, 這是你上一題得到的結果。
select name from actor,casting,movie where actor.id = casting.actorid and movie.id = casting.movieid and title = 'Casablanca'
-
顯示電影異型'Alien' 的演員清單。
select name from actor,casting where actor.id = casting.actorid and movieid = (select id from movie where title = 'Alien')
-
列出演員夏里遜福 'Harrison Ford' 曾演出的電影。
select title from movie,casting where movie.id = casting.movieid and actorid = (select id from actor where name = 'Harrison Ford')
-
列出演員夏里遜福 'Harrison Ford' 曾演出的電影,但他不是第1主角。
select title from movie,casting where movie.id = casting.movieid and actorid = (select id from actor where name = 'Harrison Ford') and ord <> 1
-
列出1962年首影的電影及它的第1主角。
select title,name from movie join casting on movie.id = casting.movieid join actor on casting.actorid = actor.id where yr = 1962 and ord = 1
-
尊·特拉華達'John Travolta'最忙是哪一年? 顯示年份和該年的電影數目。
select yr,count(*) c from movie where id in( select movieid from casting join actor on casting.actorid = actor.id where name = 'John Travolta') group by yr order by c desc limit 1
-
列出演員茱莉·安德絲'Julie Andrews'曾參與的電影名稱及其第1主角。
select title,name from movie join casting on movie.id = casting.movieid join actor on actor.id = casting.actorid where ord =1 and movie.id in ( select movieid from casting join actor on casting.actorid = actor.id where name = 'Julie Andrews')
-
列出按字母順序,列出哪一演員曾作30次第1主角。
select name from actor join casting on actor.id = casting.actorid and ord = 1 group by actor.id having count(*) >= 30 order by name
-
列出1978年首影的電影名稱及角色數目,按此數目由多至少排列。
select title,count(actorid) c from movie,casting where movie.id = casting.movieid and yr = 1978 group by movieid order by c desc
-
列出曾與演員亞特·葛芬柯'Art Garfunkel'合作過的演員姓名。
select name from actor join casting on actor.id = casting.actorid where movieid in ( select movieid from casting where actorid = ( select id from actor where name = 'Art Garfunkel') ) and name != 'Art Garfunkel'
九、Using Null
-
列出學系department是NULL值的老師。
select name from teacher where dept is null;
-
注意INNER JOIN 不理會沒有學系的老師及沒有老師的學系。
SELECT teacher.name, dept.name FROM teacher INNER JOIN dept ON (teacher.dept=dept.id)
-
使用不同的JOIN(外連接),來列出全部老師。
SELECT teacher.name, dept.name FROM teacher left join dept ON (teacher.dept=dept.id)
-
使用不同的JOIN(外連接),來列出全部學系。
SELECT teacher.name, dept.name FROM teacher right join dept ON (teacher.dept=dept.id)
-
name and mobile number or '07986 444 2266'
select name,coalesce(mobile,'07986 444 2266') from teacher
-
Use the COALESCE function and a LEFT JOIN to print the teacher name and department name. Use the string 'None' where there is no department.
select t.name,coalesce(d.name,'None') from teacher t left join dept d on t.dept = d.id
-
使用COUNT來數算老師和流動電話數目。
select count(name),count(mobile) from teacher
-
使用COUNT 和 GROUP BY dept.name來顯示每一學系的老師數目。 使用 RIGHT JOIN 以確保工程系Engineering 是在當中。
select d.name,count(t.name) from teacher t right join dept d on t.dept = d.id group by d.name
-
Use CASE to show the name of each teacher followed by 'Sci' if the teacher is in dept 1 or 2 and 'Art' otherwise.
select name,case when dept in (1,2) then 'Sci' else 'Art' end from teacher
-
Use CASE to show the name of each teacher followed by 'Sci' if the teacher is in dept 1 or 2, show 'Art' if the teacher's dept is 3 and 'None' otherwise.
select name,case when dept in (1,2) then 'Sci' when dept = 3 then 'Art' else 'None' end from teacher