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.
SQL-SQLZOO学习笔记_第1张图片
eg.
6. Select the code which shows the years when a Medicine award was given but no Peace or Literature award was.
SQL-SQLZOO学习笔记_第2张图片

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)

SQL-SQLZOO学习笔记_第3张图片
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.
SQL-SQLZOO学习笔记_第4张图片
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

SQL-SQLZOO学习笔记_第5张图片
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

你可能感兴趣的:(SQL,学习档案)