【随手练】SQLZOO部分习题

"Monaco-Ville"是合併國家名字 "Monaco" 和延伸詞"-Ville".

顯示國家名字,及其延伸詞,如首都是國家名字的延伸。

你可以使用SQL函數 REPLACE 或 MID.

SELECT name,REPLACE(capital,name,'')
FROM world
WHERE capital LIKE CONCAT(name,'_%')

相关函数:

  1. REPLACE(f, s1, s2) ,对f,用字符串s2替代s1,如果替代成'',相当于删除s1字符  REPLACE('vessel','e','a') -> 'vassal'
  2. CONCAT 连接两个字符串成一个字符

 

Exclusive OR (XOR). Show the countries that are big by area (more than 3 million) or big by population (more than 250 million) but not both. Show name, population and area.

SELECT name,population,area
FROM world
WHERE area>=3000000 XOR population>=250000000

XOR: a XOR b 不同时满足a,b 满足a或者满足b

 

Show the name and per-capita GDP for those countries with a GDP of at least one trillion (1000000000000; that is 12 zeros). Round this value to the nearest 1000.

SELECT name,ROUND(gdp/population,-3)
FROM world
WHERE gdp>=1000000000000

ROUND(f,p) returns f rounded to p decimal places ,p是正数返回p位小数,p是负数为标准化个位、十位、百位…

 

Show the name and the capital where the first letters of each match. Don't include countries where the name and the capital are the same word.

SELECT name,capital
FROM world
WHERE LEFT(name,1)=LEFT(capital,1) AND name<>capital

LEFT: LEFT(s,n) allows you to extract n characters from the start of the string s.提取字符串s的第n个字符

 

Find all details of the prize won by EUGENE O'NEILL

SELECT * FROM nobel 
WHERE winner='EUGENE O\'NEILL'

在字符串中遇到'这种字符应用\进行转义

 

List the name and continent of countries in the continents containing either Argentina or Australia. Order by name of the country.

SELECT name,continent
FROM world
WHERE continent IN (SELECT continent FROM world 
                    WHERE name IN ('Argentina','Australia'))
ORDER BY name

因为子查询中返回的continent不止一行,所以第一个SELECT的判断应该用IN 而不能等于

 

Which countries have a GDP greater than every country in Europe? [Give the name only.] (Some countries may have NULL gdp values)

SELECT name FROM world
WHERE gdp> ALL(SELECT gdp FROM world WHERE continent='Europe' AND gdp IS NOT NULL)

IS NOT NULL判断不为NULL ,IS NULL 判断为NULL

ALL可以和比较符号连用,用以比较单个数值和列表中所有的数值,不能a>3 all(b) 只能 a/3 >all(b)

 

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 x.area >=
    ALL(SELECT y.area FROM world y
        WHERE y.continent=x.continent
          AND area>0)

 

List each continent and the name of the country that comes first alphabetically.

SELECT continent,name FROM world x
WHERE  x.name=(SELECT y.name FROM world y
               WHERE y.continent=x.continent
               ORDER BY name LIMIT 1)

利用重命名,外层SELECT和内层SELECT 实现自己和自己比较

ORDER BY name LIMIT 1按照name排序,返回一个 。

LIMIT n等价于LIMIT 0,n    LIMIT i, n为索引i开始(第一条是0),返回n条

 

Find the continents where all countries have a population <= 25000000. Then find the names of the countries associated with these continents. Show namecontinent and population.

SELECT name, continent, population FROM world x 
WHERE 25000000>= ALL
(SELECT y.population FROM world y
WHERE y.continent=x.continent)

 

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 world x
WHERE population>=ALL (SELECT 3*y.population FROM world y
                     WHERE y.continent=x.continent
                     AND y.name<>x.name)

【随手练】SQLZOO部分习题_第1张图片

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

CASE用法:

CASE WHEN condition1 THEN value1

           WHEN condition 2 THEN value2 

           ELSE value3

END

 

Obtain the cast list for 'Casablanca'.

SELECT name FROM actor JOIN casting ON actorid=id
WHERE movieid=(SELECT id FROM movie
WHERE title='Casablanca')

 

Use COUNT and GROUP BY dept.name to show each department and the number of staff. Use a RIGHT JOIN to ensure that the Engineering department is listed

SELECT dept.name,COUNT(teacher.name) FROM teacher 
RIGHT JOIN dept ON teacher.dept=dept.id
GROUP BY dept.name

COUNT(*) 统计包含NULL行,COUNT(字段名)不包含NULL,left join保证所有的部门都被列出来。

 

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

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(SQL)