SQLZOO练习笔记

SQLZOO练习笔记

  • SELECT from WORLD Tutorial
  • SELECT from Nobel
  • nested SELECT
  • SELECT .. SELECT
  • SUM and COUNT
  • SUM and COUNT from Nobel
  • The JOIN operation
  • JOIN from Music Tutorial
  • More JOIN operations from Movie Tutorial
  • Using NULL
  • Self JOIN

MySQL的自学主要来源是《MySQL必知必会》这本书,最近想通过SQLZOO进行练习巩固,本文主要记录一下在练习过程中遇到的不熟练的题目和在《MySQL必知必会》中没有学习到的知识点。

SELECT from WORLD Tutorial

  • 操作符XOR
    满足条件1或条件2,但是不同时满足条件1和条件2
第8题:
SELECT name, population, area FROM world 
WHERE area >= 3000000 XOR population >= 250000000
  • 近似函数ROUND,FLOOR,CEIL
    https://sqlzoo.net/wiki/ROUND
    ROUND(f,p) 用来取近似值,p表示保留小数点后几位数字,如果p是负数,则表示近似到10的几次方,负数也可以理解为保留到小数点前几位数。
    FLOOR(f) 向下取整数
    CEIL(f) 向上取整数
第9题:
SELECT name, ROUND(population/1000000,2), ROUND(gdp/1000000000, 2) FROM world
WHERE continent IN ('South America')
第10题:
SELECT name, ROUND(gdp/population, -3) FROM world
WHERE gdp >= 1000000000000
  • 运算符 ‘=’, 容易和编程语言里的’=='混淆
第11题:
SELECT name, capital FROM world 
WHERE LENGTH(name) = LENGTH(capital)

SELECT from Nobel

  • BETWEEN包含开始值和结束值
第5题:
SELECT yr, subject, winner FROM nobel
WHERE subject = 'Literature' AND yr BETWEEN 1980 AND 1989
  • 排序可用表达式的值(0或1)作为排序依据
第14题:
SELECT winner, subject
  FROM nobel
 WHERE yr=1984
 ORDER BY subject IN ('Physics','Chemistry'),subject,winner

nested SELECT

  • ALL (SELECT子句) 用于比较操作,子句返回的每一个结果都要用来进行比较
第4题:
SELECT name, population FROM world
 WHERE population > ALL
      (SELECT population FROM world
        WHERE continent='Europe')

SELECT … SELECT

  • 子查询,derived table X
第1题:
SELECT name, ROUND(gdp_per_capita)
FROM  (SELECT name,  gdp/population AS gdp_per_capita FROM bbc) X
WHERE gdp_per_capita>20000

SUM and COUNT

  • 使用HAVING过滤分组
第8题:
SELECT continent FROMworld
GROUP BY continent
HAVING SUM(population) >= 100000000

SUM and COUNT from Nobel

  • 使用DISTINCT检索不同的值
第11题
SELECT winner FROM nobel
GROUP BY winner
HAVING COUNT(DISTINCT subject) > 1

The JOIN operation

  • 使用WHERE+完全限定列名创建联结,FROM后面要加上所有表名
第1题:
SELECT matchid,player FROM goal, eteam
 WHERE goal.teamid = eteam.id AND eteam.teamname = 'Germany'
  • 使用INNER JOIN … ON创建联结
第1题的另一种写法:
SELECT matchid,player FROM goal INNER JOIN eteam
 ON goal.teamid = eteam.id 
 WHERE eteam.teamname = 'Germany'
第3题:
SELECT player, teamid, stadium, mdate
FROM game JOIN goal ON (game.id = goal.matchid)
JOIN eteam ON goal.teamid = eteam.id WHERE eteam.teamname = 'Germany'
  • 使用CASE WHEN…THEN…语句在不同条件下返回不同值
第13题
SELECT id,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 

JOIN from Music Tutorial

  • 使用HAVING 和DISTINCT
第8题:
SELECT track.song, COUNT(DISTINCT album.title)
FROM album JOIN track
ON album.asin = track.album
GROUP BY track.song
HAVING COUNT(DISTINCT album.title) > 2

More JOIN operations from Movie Tutorial

  • 查询结果中列出最大值
第11题:
SELECT yr,COUNT(title) FROM
  movie JOIN casting ON movie.id=movieid
         JOIN actor   ON actorid=actor.id
where name='John Travolta'
GROUP BY yr
HAVING COUNT(title)=(SELECT MAX(c) FROM
(SELECT yr,COUNT(title) AS c FROM
   movie JOIN casting ON movie.id=movieid
         JOIN actor   ON actorid=actor.id
 where name='John Travolta'
 GROUP BY yr) AS t
)
  • 用子查询结果作为判断条件
第12题:
SELECT title, name FROM movie JOIN casting ON movie.id = casting.movieid 
JOIN actor ON casting.actorid = actor.id
WHERE casting.ord = 1 
AND movieid IN
(SELECT movieid FROM casting 
 WHERE actorid IN 
              (SELECT id FROM actor WHERE name='Julie Andrews'))
第15题:
SELECT name FROM actor JOIN casting ON actor.id = casting.actorid 
WHERE casting.movieid IN
    (SELECT movieid FROM casting WHERE actorid = (SELECT id FROM actor WHERE name = 'Art Garfunkel'))
 AND casting.actorid <>(SELECT id FROM actor WHERE name = 'Art Garfunkel') 

Using NULL

  • NVL

NVL(x,y) = x if x is not NULL
NVL(x,y) = y if x is NULL

  • COALESCE

COALESCE(x,y,z) = x if x is not NULL
COALESCE(x,y,z) = y if x is NULL and y is not NULL
COALESCE(x,y,z) = z if x and y are NULL but z is not NULL
COALESCE(x,y,z) = NULL if x and y and z are all NULL

第6题:
SELECT teacher.name, COALESCE(dept.name, 'None')
 FROM teacher **LEFT** JOIN dept  ON (teacher.dept=dept.id)
  • NULLIF

NULLIF(x,y) = NULL if x=y
NULLIF(x,y) = x if x != y

Self JOIN

  • 使用不同的表别名对同一个表进行多条件查询
第5题:
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 = 149
第6题:
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' 
  • 多次使用自联结
第10题:
SELECT DISTINCT a.num, a.company, stopc.name, b.num, b.company
FROM route a JOIN route c ON
  (a.company=c.company AND a.num=c.num)
  JOIN route d ON 
  (c.stop = d.stop)
  JOIN route b ON
  (d.company=b.company AND d.num=b.num)
  JOIN stops stopa ON (a.stop=stopa.id) 
  JOIN stops stopb ON (b.stop=stopb.id)
  JOIN stops stopc ON (c.stop=stopc.id)
WHERE stopa.name='Craiglockhart' AND stopb.name = 'Lochend'

你可能感兴趣的:(MySql学习)