SQL-SQLZOO學習筆記

Tutorials: Learn SQL in stages

雖然日常工作中經常用到SQL,不過沒有系統性的訓練就難以有效的提升查詢效率。本文記錄每一節的常用函數及應用方法,以備以後查找。
https://sqlzoo.net/wiki/SQL_Tutorial

SELECT names

函數 用法
like(’’) 模糊匹配,可以用’%‘和’_'佔位符,前者可表白空值或多個任意字符,後者代表一個字符且不代表空字符,以及not like()用法
concat(str1,str2,…) 可將多個字符串拼接在一起,和"兩個豎槓"符號作用相同
locate(a,b) 判斷a是否在b中,返回0或1,用法類似於instr()
replace(‘vessel’,‘e’,‘a’) 將字符串中’vessel’的’e’用’a’代替,最終輸出’vassal’

SELECT from World

函數 用法
xor 區別於or,or是指滿足一個條件即可,包括同時滿足條件的;xor是指只需滿足一個條件即可,但不能同時滿足
round(,n) 四捨五入將值保留指定n位數,n可以爲負值
<> 和!=效果相同
left(str,1) 從左邊第一個字符開始,取出現的前幾個字符,相似的有right(str,1),substr(str,i,j) ,在ORACLE中無此項用法

SELECT from Nobel Tutorial

函數 用法
str IN (‘str1’,‘str2’,…) The expression str IN (‘str1’,‘str2’) can be used as a value - it will be 0 or 1,即in函數可以作爲一個整體來用

注意“與”和“或”的邏輯關係
eg.
3. Pick the code that shows the amount of years where no Medicine awards were given.
案例圖片
eg.
6. Select the code which shows the years when a Medicine award was given but no Peace or Literature award was.
案例圖

SELECT within SELECT Tutorial

本節主要練習嵌套查詢,嵌套查詢中子查詢的結果既可以在父查詢的條件中使用也可以在結果中使用,更復雜的用法需將查詢表命別名(將內外表連起來,相當於創建了一個查詢條件)。

函數 用法
ALL >ALL:代表父查詢中的結果集大於子查詢中每一個結果集中的值,則爲真
ANY >ANY:代表父查詢中的結果集大於子查詢中任意一個結果集中的值,則爲真;=ANY:與子查詢IN相同;<>ANY:類NOT IN

eg.
7.Find the largest country (by area) in each continent, show the continent, the name and the area:

select continent,name,area from world x
where area >=all(
select area from world y
where x.continent=y.continent)

eg.
10.Some countries have populations more than three times that of any of their neighbours (in the same continent). Give the countries and continents.
#第一次編的有些複雜,也附上改進版

select name,continent from(select name,population,continent from world x
where population >=all(select max(population) from world y where  x.continent=y.continent))z1
where round(population/3,1) >all(select population from 
(select name,continent,population from world where name not in
(select name from world x
where population >=all(select max(population) from world y where  x.continent=y.continent))) z2
where z1.continent=z2.continent)
>>>改進
select name,continent from world x
where population>all(select population*3 from world y where x.continent=y.continent and x.name!=y.name)

SUM and COUNT

函數 用法
HAVING 和GROUP BY一起用,在GROUP BY之後。The HAVING clause allows use to filter the groups which are displayed. The WHERE clause filters rows before the aggregation, the HAVING clause filters after the aggregation

eg.8.List the continents that have a total population of at least 100 million.

select continent from world
group by continent
having sum(population)  >=100000000

The JOIN operation
full join/ left join/ right join/ inner join(join)

在這裏插入圖片描述
eg.
13.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.
在這裏插入圖片描述
Notice in the query given every goal is listed. If it was a team1 goal then a 1 appears in score1, otherwise there is a 0. You could SUM this column to get a count of the goals scored by team1. Sort your result by mdate, matchid, team1 and team2.

#原代碼
with temp as(
select matchid,teamid,count(1) goals from goal group by matchid,teamid
)
select  mdate,team1,scores1,team2, 
case when goals is null then 0 else goals end score2 from(
select id,mdate,team1,
case when goals is null then 0 else goals end
scores1,team2 from game left join temp on game.id||game.team1=temp.matchid||temp.teamid) game2 left join temp on  
game2.id||game2.team2=temp.matchid||temp.teamid

#改進後
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,team2

More JOIN operations

在這裏插入圖片描述
eg.
12.List the film title and the leading actor for all of the films ‘Julie Andrews’ played in.

select title,name from movie join casting join actor
on movie.id=casting.movieid and casting.actorid=actor.id
where ord=1 and movie.id||title in(
select movie.id||title from movie join casting join actor
on movie.id=casting.movieid and casting.actorid=actor.id
where name='Julie Andrews') 

13.Obtain a list, in alphabetical order, of actors who’ve had at least 30 starring roles.

select name from(
select name,title from movie join casting join actor
on movie.id=casting.movieid and casting.actorid=actor.id
where ord=1) a
group by name
having count(title)>=30
order by name
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章