第8题:
SELECT name, population, area FROM world
WHERE area >= 3000000 XOR population >= 250000000
第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)
第5题:
SELECT yr, subject, winner FROM nobel
WHERE subject = 'Literature' AND yr BETWEEN 1980 AND 1989
第14题:
SELECT winner, subject
FROM nobel
WHERE yr=1984
ORDER BY subject IN ('Physics','Chemistry'),subject,winner
第4题:
SELECT name, population FROM world
WHERE population > ALL
(SELECT population FROM world
WHERE continent='Europe')
第1题:
SELECT name, ROUND(gdp_per_capita)
FROM (SELECT name, gdp/population AS gdp_per_capita FROM bbc) X
WHERE gdp_per_capita>20000
第8题:
SELECT continent FROMworld
GROUP BY continent
HAVING SUM(population) >= 100000000
第11题
SELECT winner FROM nobel
GROUP BY winner
HAVING COUNT(DISTINCT subject) > 1
第1题:
SELECT matchid,player FROM goal, eteam
WHERE goal.teamid = eteam.id AND eteam.teamname = 'Germany'
第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'
第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
第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
第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')
NVL(x,y) = x if x is not NULL
NVL(x,y) = y if x is NULL
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(x,y) = NULL if x=y
NULLIF(x,y) = x if x != y
第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'