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

文章目录

  • 1、SELECT_from_WORLD_Tutorial
  • 2、SELECT_from_Nobel_Tutorial
  • 3、Nobel_Quiz
  • 4、SELECT_within_SELECT_Tutorial
  • 5、Nested_SELECT_Quiz
  • 6、SUM_and_COUNT
  • 7、The_nobel_table_can_be_used_to_practice_more_SUM_and_COUNT_functions

网址: 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 nobelWHERE 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 yWHERE y.continent=x.continentAND 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 yWHERE 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.continentAND 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

SqlZoo错题整理相关推荐

  1. 加速度测试什么软件,错题整理神器,喵喵错题APP实现高效学习的第一步

    错题整理神器,喵喵错题APP实现高效学习的第一步 "如何高效的提升学习效率?",关于这一问题,近期我是做了不少功课.因为家里的孩子已经幼升小,但经过一年级上学期半年的学习,孩子的成 ...

  2. PMP错题整理6.6

    PMP错题整理6.6 102. [单选] 项目经理发现没有关于需要哪些部门资源的信息,应该在哪一份文件中更新该信息? A project manager discovers that there is ...

  3. 证券从业资格考试——金融市场基础知识关键点和错题整理

    证券从业资格考试--金融市场基础知识错题及关键知识点整理 金融市场基础知识错题整理 一.各种申请条件(只记录关键的数字) 二.与数字相关的点(时间,百分比,人数等) 三.其他 金融市场基础知识错题整理 ...

  4. 牛客网错题整理--C++篇1

    牛客网错题整理--C++篇1 1.下列程序的运行结果是PP 10003,请为横线处选择合适的程序(): #include<stdio.h>#include<string.h>s ...

  5. 小明用计算机整理30个数,六年级上册数学期末复习易错题整理_(8)[1]

    六年级上册数学易错题整理(2) 圆和百分数部分 一.填空题 1.圆是平面上的( )线图形,( )决定圆的位置,用字母( )表示: ( )决定圆的大小,用字母( )表示. 2.通过( )并且两端都在( ...

  6. 判断小数是否相等_四年级上册数学填空+计算+判断易错题整理练习,收藏练一练!...

    四年级数学易错题练习 一.填空题 1.1.25×0.8表示(              ). 2.去掉0.25的小数点,就是把这个数扩大(      ):把50.4的小数点向左移动两位,就是把它缩小到 ...

  7. sqlzoo错题总结-1

    目录 说明 题目1 正解 说明 最近在练习sqlzoo,遇到错题就总结到这里,让自己回顾一下 题目1 数据表nobel如下 题目如下:(属于sqlzoo第三个quiz的第5个选择题) 链接在此:点我到 ...

  8. 【C语言】(错题整理) 寻找完数、字符串中各类字符数的统计、最大公约数和最小公倍数、回文数计算 (循环、函数相关内容)

    目录 一.循环 1.寻找完数(计算因子例题) 2.字符串中各类字符数的统计 3.最大公约数和最小公倍数 求最大公约数: 最小公倍数:最小公倍数=两整数的乘积÷最大公约数 二.函数 1.回文数计算 本篇 ...

  9. 计算机系统原理错题整理

    计算机系统原理 想要理解计算机的工作原理,那就来学计算机系统原理鸭! 知识点和错题解析 1.假定"int buf[2]={10,50};"所定义的buf被分配在静态数据区,其首地址 ...

最新文章

  1. golang中struct字段
  2. 微软确认5月2日召开新品发布会 8天后就是Build 2017大会
  3. python的collection系列-默认字典(defaultdict)
  4. mapgis转arcgis数据后发现属性表内没有数据
  5. vue process.env获取不到_从文档开始,重学vue(下)源码级别
  6. c# 多线程实现ping 多线程控制控件
  7. CSITOOL安装接收CSI数据
  8. leetcode 559 N叉树的最大深度
  9. 华为网络拒绝接入_网络拒绝接入什么意思
  10. mysql之查询用户名
  11. Spring Boot的MyBatis注解:@MapperScan和@Mapper
  12. ZK(7.0.1)将zul页面引入作为组件标签的简单示例
  13. android手机远程控制电脑源代码,手把手教你用安卓手机实现远程控制电脑-网络教程与技术 -亦是美网络...
  14. 企业销售统计管理页面ui模板
  15. vb改动microsip让microsip隐藏然后命令拨打电话
  16. SVN服务端与客户端安装(汉化包)以及简单使用
  17. log4j从1.x平滑升级至2.x
  18. Samsung SENS R60plus
  19. python查询46级成绩
  20. Apache Calcite介绍

热门文章

  1. Android 9.0 10.0 沉浸式状态栏导致导航栏状态栏灰色蒙层的解决方案
  2. 第五届传智杯-初赛【A组-题解】
  3. 从新手小白到氪金大佬的“建站指南”
  4. 手把手教你搭建自己的git+gerrit代码评审服务器
  5. 有史以来实现Excel外接程序最简单的教程
  6. 硬核浪漫!用python在圣诞节给对象送礼的5个最没用小技巧
  7. Python+大数据-知行教育(七)-学生出勤主题看板
  8. PLC PID控制优化系列之死区控制
  9. 【CNN削减阅读笔记】【简化网络设计】【低秩分解】
  10. 恒压板框过滤实验数据处理_比价:宝鸡小型板框实验压滤机销售厂家,压滤机价格...