SQLZOO筆記(二)

DATA

JOIN

SELECT * FROM game JOIN goal ON (game.id=goal.matchid)

The FROM clause says to merge data from the goal table with that from the game table. The ON says how to figure out which rows in game go with which rows in goal - the matchid from goal must match id from game.

https://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins

mysql不支持full join蹈胡,可以通過左連接union右連接實現
Oracle支持full join


Show teamname and the total number of goals scored.

SELECT teamname, COUNT(teamid) FROM eteam
JOIN goal ON id = teamid
GROUP BY teamname

For every match where 'GER' scored, show matchid, match date and the number of goals scored by 'GER'

SELECT matchid, mdate, COUNT(teamid) FROM game
JOIN goal ON id = matchid
WHERE teamid = 'GER'
GROUP BY matchid

List every match with the goals scored by each team as shown. Sort your result by mdate, matchid, team1 and team2.

SELECT mdate,
  team1,
  SUM(CASE WHEN teamid = team1 THEN 1 ELSE 0 END) score1,
  team2,
  SUM(CASE WHEN teamid = team2 THEN 1 ELSE 0 END) score2
FROM game LEFT JOIN goal ON matchid = id  # 左連接
GROUP BY mdate, matchid, team1 and team2

Select the code which shows players, their team and the amount of goals they scored against Greece(GRE).

SELECT player, teamid, COUNT(*) FROM game 
JOIN goal ON matchid = id
WHERE (team1 = "GRE" OR team2 = "GRE")
AND teamid != 'GRE'
GROUP BY player, teamid

Movie Database

More JOIN

Obtain the cast list for the film 'Alien'.

SELECT name FROM actor
JOIN casting ON actor.id = actorid
JOIN movie ON movie.id = movieid
WHERE title = 'Alien'

List the films where 'Harrison Ford' has appeared, but not in the starring role.

SELECT title FROM movie
JOIN casting ON movie.id = movieid
JOIN actor ON actor.id = actorid
WHERE name = 'Harrison Ford'
AND ord != 1

List the films together with the leading star for all 1962 films.

SELECT title,name FROM movie  # name字段不屬于movie表堤框,但是可以被索引,因為JOIN的關系昆汹,三個表被看作是一個表
JOIN casting ON movie.id = movieid
JOIN actor ON actor.id = actorid
WHERE yr = 1962
AND ord = 1

Which were the busiest years for 'Rock Hudson', show the year and the number of movies he made each year for any year in which he made more than 2 movies.

SELECT yr,COUNT(title) FROM
  movie JOIN casting ON movie.id = movieid
        JOIN actor   ON actorid = actor.id
WHERE name = 'Rock Hudson'
GROUP BY yr  # 找each
HAVING COUNT(title) > 2

List the film title and the leading actor for all of the films 'Julie Andrews' played in.

SELECT title,name FROM movie
JOIN casting ON movie.id = movieid
JOIN actor ON actor.id = actorid
WHERE movieid IN (SELECT movieid FROM casting
                  JOIN actor ON actorid = id
                  WHERE name = 'Julie Andrews')
AND ord = 1

Obtain a list, in alphabetical order, of actors who've had at least 15 starring roles.

SELECT name FROM actor
JOIN casting ON actorid = id
WHERE ord = 1
GROUP BY actorid
HAVING COUNT(ord) >= 15  # count里面可以填ord/actorid/name,因為前面的where已經限制了ord=1
ORDER BY name  # 這個注釋的顏色是隨機的??

List the films released in the year 1978 ordered by the number of actors in the cast, then by title.

SELECT title,COUNT(actorid) FROM movie
JOIN casting ON movie.id = movieid
JOIN actor ON actor.id = actorid
WHERE yr = 1978
GROUP BY movieid
ORDER BY COUNT(actorid) DESC, title

GROUP的邏輯:ordered by the number of actors (each movie)


List all the people who have worked with 'Art Garfunkel'.
索引邏輯:1. 找到'Art Garfunkel'參演過的電影; 2. 以電影id為條件索引參演人員name

SELECT name FROM actor
JOIN casting ON actorid = actor.id
JOIN movie ON movieid = movie.id
WHERE movieid IN (SELECT movieid from casting
                  JOIN actor ON actorid = id
                  WHERE name = 'Art Garfunkel')
AND name != 'Art Garfunkel'

INNER JOIN = JOIN

SELECT name
FROM actor INNER JOIN movie ON actor.id = director
WHERE gross < budget

存疑

SELECT name, COUNT(movieid)
FROM casting JOIN actor ON actorid = actor.id
WHERE name LIKE 'John %'
GROUP BY name ORDER BY 2 DESC  # 這個order by 2指的是按照第二列進行排序

???

SELECT title, yr
FROM movie, casting, actor 
WHERE name = 'Robert De Niro' AND movieid = movie.id AND actorid = actor.id AND ord = 3

NULL

  1. IS NULL/IS NOT NULL 關鍵字(var = NULL為錯誤用法)
  2. COALESCE(x,y,z...): COALESCE takes any number of arguments and returns the first value that is not null.
teacher.id dept name phone mobile
101 1 Shrivell 2753 07986 555 1234
102 1 Throd 2754 07122 555 1920
103 1 Splint 2293
104 Spiregrain 3287
105 2 Cutflower 3212 07996 555 6574
106 Deadyawn 3345
dept.id name
1 Computing
2 Design
3 Engineering

Show the teacher name and department name. Use the string 'None' where there is no department.

# 空值以null顯示被替換為了'None'字符串
SELECT teacher.name, COALESCE(dept.name,'None') FROM teacher
LEFT JOIN dept ON teacher.dept = dept.id

Use COUNT to show the number of teachers and the number of mobile phones.

# 空值不參與計數
SELECT COUNT(name), COUNT(mobile) FROM teacher
COUNT(name) COUNT(mobile)
6 3

Edinburgh_Buses

SELF JOIN

Show the services from Craiglockhart to London Road without changing routes

SELECT a.company, a.num, a.stop, b.stop
FROM route a JOIN route b ON (a.company = b.company AND a.num = b.num)
WHERE a.stop = 53
AND b.stop IN (SELECT id FROM stops
               JOIN route ON stop = id
               WHERE name = 'London Road')
company num stop stop
LRT 4 53 149
LRT 45 53 149

By joining two copies of the stops table we can refer to stops by name rather than by number.

SELECT a.company, a.num, stopa.name, stopb.name
FROM route a JOIN route b ON (a.company = b.company AND a.num = b.num)
JOIN stops stopa ON (a.stop = stopa.id)
JOIN stops stopb ON (b.stop = stopb.id)
WHERE stopa.name = 'Craiglockhart'
AND stopb.name = 'London Road'
company num name name
LRT 4 Craiglockhart London Road
LRT 45 Craiglockhart London Road

Give a list of all the services which connect stops 115 and 137.

SELECT DISTINCT R1.company, R1.num FROM route R1
JOIN route R2 ON (R1.num = R2.num AND R1.company = R2.company)  # all pairs of stops that share the same service
WHERE R1.stop = '115'
AND R2.stop = '137'

Give a list of the services which connect the stops 'Craiglockhart' and 'Tollcross'.

SELECT DISTINCT R1.company, R1.num FROM route R1
JOIN route R2 ON (R1.num = R2.num AND R1.company = R2.company)
JOIN stops stops1 ON R1.stop = stops1.id
JOIN stops stops2 ON R2.stop = stops2.id
WHERE stops1.name = 'Craiglockhart'
AND stops2.name = 'Tollcross'

Give a distinct list of the stops which may be reached from 'Craiglockhart' by taking one bus, including 'Craiglockhart' itself, offered by the LRT company. Include the company and bus no. of the relevant services.

SELECT DISTINCT stops2.name, R1.company, R1.num FROM route R1
JOIN route R2 ON (R1.num = R2.num AND R1.company = R2.company)
JOIN stops stops1 ON R1.stop = stops1.id
JOIN stops stops2 ON R2.stop = stops2.id
WHERE stops1.name = 'Craiglockhart'

Find the routes involving two buses that can go from Craiglockhart to Lochend.
Show the bus no. and company for the first bus, the name of the stop for the transfer, and the bus no. and company for the second bus.

從Craiglockhart出發(fā)經過一個中轉站到達Lochend的公車信息
拆解為三步:

  1. 列出以Craiglockhart作為起始點婴栽,任意站點為終止點的公車信息
SELECT R1.num, R1.company, R1.stop AS start, R2.stop AS end 
FROM route R1 JOIN route R2 ON 
(R1.num = R2.num AND R1.company = R2.company AND R1.stop != R2.stop)
WHERE R1.stop = (SELECT id 
                 FROM stops WHERE name = 'Craiglockhart')
num company start end
10 LRT 53 215
10 LRT 53 162
10 LRT 53 167
10 LRT 53 137
... ... ... ...
  1. 列出以Lochend為終止點满粗,任意站點為起始點的公車信息
SELECT R1.num, R1.company, R1.stop AS start, R2.stop AS end
FROM route R1 JOIN route R2 ON
(R1.num = R2.num AND R1.company = R2.company AND R1.stop != R2.stop)
WHERE R2.stop = (SELECT id
                 FROM stops WHERE name = 'Lochend')
num company start end
20 LRT 105 147
20 LRT 59 147
20 LRT 225 147
20 LRT 95 147
... ... ... ...
  1. 將兩表連接(排序和答案有點出入)
SELECT DISTINCT Start.num, Start.company, name, End.num, End.company FROM(
SELECT R1.num, R1.company, R1.stop AS start, R2.stop AS end 
FROM route R1 JOIN route R2 ON 
(R1.num = R2.num AND R1.company = R2.company AND R1.stop != R2.stop)
WHERE R1.stop = (SELECT id 
                 FROM stops WHERE name = 'Craiglockhart')
) AS Start
JOIN (
SELECT R1.num, R1.company, R1.stop AS start, R2.stop AS end
FROM route R1 JOIN route R2 ON
(R1.num = R2.num AND R1.company = R2.company AND R1.stop != R2.stop)
WHERE R2.stop = (SELECT id
                 FROM stops WHERE name = 'Lochend')
) AS End
ON Start.end = End.start
JOIN stops ON Start.end = stops.id
num company name num company
10 LRT Leith 34 LRT
10 LRT Leith 35 LRT
10 LRT Princes Street 65 LRT
10 LRT Leith 87 LRT
10 LRT Leith C5 SMT
10 LRT Princes Street C5 SMT
... ... ... ... ...
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
禁止轉載,如需轉載請通過簡信或評論聯系作者愚争。
  • 序言:七十年代末映皆,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子轰枝,更是在濱河造成了極大的恐慌捅彻,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,718評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件鞍陨,死亡現場離奇詭異步淹,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現死者居然都...
    沈念sama閱讀 90,683評論 3 385
  • 文/潘曉璐 我一進店門缭裆,熙熙樓的掌柜王于貴愁眉苦臉地迎上來键闺,“玉大人,你說我怎么就攤上這事澈驼“樱” “怎么了?”我有些...
    開封第一講書人閱讀 158,207評論 0 348
  • 文/不壞的土叔 我叫張陵盅藻,是天一觀的道長购桑。 經常有香客問我,道長氏淑,這世上最難降的妖魔是什么勃蜘? 我笑而不...
    開封第一講書人閱讀 56,755評論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮假残,結果婚禮上缭贡,老公的妹妹穿的比我還像新娘。我一直安慰自己辉懒,他們只是感情好阳惹,可當我...
    茶點故事閱讀 65,862評論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著眶俩,像睡著了一般莹汤。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上颠印,一...
    開封第一講書人閱讀 50,050評論 1 291
  • 那天纲岭,我揣著相機與錄音,去河邊找鬼线罕。 笑死止潮,一個胖子當著我的面吹牛,可吹牛的內容都是我干的钞楼。 我是一名探鬼主播喇闸,決...
    沈念sama閱讀 39,136評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼询件!你這毒婦竟也來了燃乍?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 37,882評論 0 268
  • 序言:老撾萬榮一對情侶失蹤雳殊,失蹤者是張志新(化名)和其女友劉穎橘沥,沒想到半個月后,有當地人在樹林里發(fā)現了一具尸體夯秃,經...
    沈念sama閱讀 44,330評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡座咆,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 36,651評論 2 327
  • 正文 我和宋清朗相戀三年痢艺,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片介陶。...
    茶點故事閱讀 38,789評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡堤舒,死狀恐怖,靈堂內的尸體忽然破棺而出哺呜,到底是詐尸還是另有隱情舌缤,我是刑警寧澤,帶...
    沈念sama閱讀 34,477評論 4 333
  • 正文 年R本政府宣布某残,位于F島的核電站国撵,受9級特大地震影響,放射性物質發(fā)生泄漏玻墅。R本人自食惡果不足惜介牙,卻給世界環(huán)境...
    茶點故事閱讀 40,135評論 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望澳厢。 院中可真熱鬧环础,春花似錦、人聲如沸剩拢。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,864評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽徐伐。三九已至贯钩,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間呵晨,已是汗流浹背魏保。 一陣腳步聲響...
    開封第一講書人閱讀 32,099評論 1 267
  • 我被黑心中介騙來泰國打工熬尺, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留摸屠,地道東北人。 一個月前我還...
    沈念sama閱讀 46,598評論 2 362
  • 正文 我出身青樓粱哼,卻偏偏與公主長得像季二,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子揭措,可洞房花燭夜當晚...
    茶點故事閱讀 43,697評論 2 351