SqlZoo错题整理

最近复习了一下SQL,在sqlZoo上练手了一些题目,通过练习还是发现了不少问题,这里对错题进行一波记录与整理。


网址: https://sqlzoo.net/wiki/SELECT_from_WORLD_Tutorial

1、SELECT_from_WORLD_Tutorial

10.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.Show per-capita GDP for the trillion dollar countries to the nearest $1000.

显示那些国内生产总值至少为1万亿(10万亿美元)的国家的人均GDP,即12个零。将此值四舍五入到最近的
1000。(即‎显示万亿美元国家的人均GDP到最近的1000美元)‎

# per-capita GDP:人均国内生产总值
SELECT name,ROUND(GDP/population,-3) FROM world 
WHERE GDP>1000000000000

12.The capital of Sweden is Stockholm. Both words start with the letter ‘S’.
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.

Sweden的首都是Stockholm。这两个词都以字母"S"开头。‎显示名称和首都第一个字母是匹配(一样)的名称和首都。不包括名称和首都是同一个词的国家.

You can use the function LEFT to isolate the first character.
You can use <> as the NOT EQUALS operator.

#了解Left函数  https://sqlzoo.net/wiki/LEFT
SELECT name,capital FROM world 
WHERE LEFT(name,1)=LEFT(capital,1) AND name<>capital

13.Equatorial Guinea and Dominican Republic have all of the vowels (a e i o u) in the name. They don’t count because they have more than one word in the name.
Find the country that has all the vowels and no spaces in its name.
You can use the phrase name NOT LIKE ‘%a%’ to exclude characters from your results.The query shown misses countries like Bahamas and Belarus because they contain at least one ‘a’

SELECT name
FROM world
WHERE name LIKE '%a%'
      AND name LIKE '%e%'
      AND name LIKE '%i%'
      AND name LIKE '%o%'
      AND name LIKE '%u%'
      AND name NOT LIKE '% %'

2、SELECT_from_Nobel_Tutorial

网址:https://sqlzoo.net/wiki/SELECT_from_Nobel_Tutorial
12、Find all details of the prize won by EUGENE O’NEILL .

查找 EUGENE O’NEILL 赢得的奖品的所有详细信息.

如果查询语句中查询条件本身包含’,则不能把一个单引号直接的放在字符串中。可连续使用两个单引号在字符串中当做一个单引号。

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

14、The expression subject IN (‘Chemistry’,‘Physics’) can be used as a value - it will be 0 or 1. Show the 1984 winners and subject ordered by subject and winner name; but list Chemistry and Physics last.

查找1984年获奖者和主题,按主题和获胜者名称排序,并把化学奖和物理奖排到最后面显示.

CASE WHEN subject IN (‘Physics’,‘Chemistry’) THEN 1 ELSE 0 END ASC: 通过 case when…then…else … end 将化学奖和物理奖 转化为1 ,其他转化为0 ,便于排序。

SELECT winner, subject FROM nobel
WHERE yr=1984
ORDER BY
CASE WHEN subject IN ('Physics','Chemistry') THEN 1 ELSE 0 END ASC,
subject,
winner

3、Nobel_Quiz

网址:https://sqlzoo.net/wiki/Nobel_Quiz
3、Pick the code that shows the amount of years where no Medicine awards were given.‎

选择显示未颁发给医学奖的年数的代码‎.

#记得最后COUNT()里要去重
SELECT COUNT(DISTINCT yr) FROM nobel
WHERE yr NOT IN (SELECT DISTINCT yr FROM nobel WHERE subject = 'Medicine')

6、Select the code which shows the years when a Medicine award was given but no Peace or Literature award was.

‎选择显示给予医学奖,但没有和平或文学奖的年份的代码‎.

理解:没有和平或文学奖的年份这里指的是二者都没有的年份

SELECT DISTINCT yr
FROM nobel
WHERE subject='Medicine' 
   AND yr NOT IN(SELECT yr FROM nobel 
                  WHERE subject='Literature')
   AND yr NOT IN (SELECT yr FROM nobel
                   WHERE subject='Peace')

4、SELECT_within_SELECT_Tutorial

网址:https://sqlzoo.net/wiki/SELECT_within_SELECT_Tutorial
相关(或同步)子查询

相关子查询的工作原理类似于嵌套循环:子查询只能访问与外部查询中单个记录相关的行。该技术依靠表别名来识别同一表的两种不同用途,一种在外部查询中,另一种在子查询中。

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 y.continent=x.continent
			      AND area >0)

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

列出每个大陆和各个大洲内按字母顺序排列第一的国家名称.

SELECT continent,name FROM world x
WHERE name <= ALL(SELECT name FROM world y
        		  WHERE y.continent=x.continent)

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

查找所有国家人口<= 250000000 的大陆。然后找到与这些大陆相关的国家的名称。显示名称,大陆和人口。

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

Some countries have populations more than three times that of any of their neighbours (in the same continent). Give the countries and continents.

一些国家的人口>任一邻国(处于同一大陆)的三倍。给出国家和大陆.

# x.name!=y.name这个条件不能漏
SELECT name,continent FROM world x 
WHERE population>=ALL(SELECT population*3 FROM world y 
				      WHERE x.continent=y.continent
				      AND population>0 
				      AND x.name!=y.name)				 

5、Nested_SELECT_Quiz

网址:https://sqlzoo.net/wiki/Nested_SELECT_Quiz
5、Select the code that would show the countries with a greater GDP than any country in Africa (some countries may have NULL gdp values).

‎选择显示国内生产总值高于非洲任何国家的国家的代码(有些国家可能具有 NULL gdp 值).

# 错误答案 NULL 不能用<>或!=判断,而是用is not 
SELECT name FROM bbc
WHERE gdp > ALL (SELECT gdp FROM bbc 
 			      WHERE region = 'Africa' AND gdp<>NULL)
# 这里可以直接使用函数max()
SELECT name FROM bbc
WHERE gdp > (SELECT MAX(gdp) FROM bbc 
			 WHERE region = 'Africa')

6、SUM_and_COUNT

网址:https://sqlzoo.net/wiki/SUM_and_COUNT
7.For each continent show the continent and number of countries with populations of at least 10 million.

每个‎‎大陆‎‎都显示‎‎非洲大陆‎‎和人口至少为1 000万的国家数目.‎

#分组后的一组数据看作是一个整体,having是对整体进行进一步限制筛选
#【having后的条件是筛选符合条件的组,不能对这个组内某一个字段进行限制】,
# 下面这样就是错的
SELECT continent,COUNT(name) FROM world 
GROUP BY continent 
HAVING population >= 10000000
# 这里应该先使用where对population进行限制,注意where子句在group by子句前面
SELECT continent,COUNT(name) FROM world 
WHERE population>=10000000 
GROUP BY continent 

8.For each continent show the continent and number of countries with populations of at least 10 million.

‎列出总人口至少为‎‎1亿的大陆.

# 这里就是对分组后的一组这个整体进行限制,而不是对组内单个数据进行限制,所以可以用having
SELECT continent FROM world 
GROUP BY continent 
HAVING SUM(population)>100000000

7、The_nobel_table_can_be_used_to_practice_more_SUM_and_COUNT_functions

网址:https://sqlzoo.net/wiki/The_nobel_table_can_be_used_to_practice_more_SUM_and_COUNT_functions.

9.Show the years in which three prizes were given for Physics.

‎显示物理奖获得三个奖项的年数.

# 首先是显示年数,必定要以年分组,where限制subject= 'Physics',最后用having筛选组
SELECT yr FROM nobel 
WHERE subject='Physics' 
GROUP BY yr 
HAVING COUNT(subject)=3

10.Show winners who have won more than once.‎

展示赢得不止一次的优胜者.

SELECT winner FROM nobel 
GROUP BY winner 
HAVING COUNT(winner)>1

11.Show winners who have won more than one subject.

‎显示赢得多个主题的优胜者.

# distinct 不能漏
SELECT winner FROM nobel 
GROUP BY winner 
HAVING COUNT(winner)>1 AND COUNT(distinct subject)>1

12.Show the year and subject where 3 prizes were given. Show only years 2000 onwards.‎

显示颁发 3 个奖项的年度和主题。只显示 2000 年以后的.

# 分组字段为yr和subject,这样结果就是按每一年的情况显示
SELECT yr,subject FROM nobel 
WHERE yr>=2000 
GROUP BY yr,subject 
HAVING COUNT(winner)=3 

后续篇:SqlZoo错题整理2

猜你喜欢

转载自blog.csdn.net/weixin_45041745/article/details/120032194
今日推荐