https://sqlzoo.net

8.

美國、印度和中國(USA, India, China)是人口又大,同時面積又大的國家。排除這些國家。

顯示以人口或面積為大國的國家,但不能同時兩者。顯示國家名稱,人口和面積。

(成為大國的兩種方式:如果它有3百萬平方公里以上的面積,或擁有250百萬(2.5億)以上人口)

SELECT
`name`,
`population`,
`area`
FROM
`world`
WHERE
(area>3000000 AND population<250000000)
OR
(area<3000000 AND population>250000000)

 9.

除以為1000000(6個零)是以百萬計。除以1000000000(9個零)是以十億計。使用 ROUND 函數來顯示的數值到小數點後兩位。

對於南美顯示以百萬計人口,以十億計2位小數GDP。
SELECT
`name`,
ROUND(population/1000000,2) AS population,
ROUND(gdp/1000000000,2) AS gdp
FROM
`world`
WHERE
`continent`='South America'

 10.

顯示國家有至少一個萬億元國內生產總值(萬億,也就是12個零)的人均國內生產總值。四捨五入這個值到最接近1000。

顯示萬億元國家的人均國內生產總值,四捨五入到最近的$ 1000。

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

  

11.

The CASE statement shown is used to substitute North America for Caribbean in the third column.

Show the name - but substitute Australasia for Oceania - for countries beginning with N.
/*SELECT name,
CASE WHEN continent='Oceania' THEN 'Australia'
ELSE continent END
FROM world
WHERE name LIKE 'N%'*/ SELECT name,
CASE WHEN continent='Oceania' THEN 'Australasia'
ELSE continent END
FROM world
WHERE name LIKE 'N%'

  

12.

Show the name and the continent - but substitute Eurasia for Europe and Asia; substitute America - for each country in North America or South America or Caribbean. Show countries beginning with A or B

# 错误样例
/*SELECT
`name`,
CASE
WHEN continent=('Europe' OR 'Asia') THEN 'Eurasia'
WHEN continent=('North America' OR 'South America' OR 'Caribbean') THEN 'America'
ELSE continent
END
FROM
`world'
WHERE
`name` LIKE '[AB]%';*/ # 正确 SELECT name,
       CASE WHEN continent='Europe' or continent='Asia' THEN 'Eurasia'
WHEN continent in ('North America','South America','Caribbean') THEN 'America'
ELSE continent END
FROM world
WHERE name LIKE 'A%' or name LIKE 'B%' # name LIKE '[AB]%' 其他SQL语言中允许这种形式,MySQL似乎不允许

13.

Put the continents right...

  • Oceania becomes Australasia
  • Countries in Eurasia and Turkey go to Europe/Asia
  • Caribbean islands starting with 'B' go to North America, other Caribbean islands go to South America
Show the name, the original continent and the new continent of all countries.

/*SELECT
`name',
`continent`
CASE
WHEN continent='Eurasia' THEN 'Europe/Asia'
WHEN continent='Oceania' THEN 'Australasia'
WHEN name='Turkey' THEN 'Europe/Asia'
WHEN continent='Caribbean' AND name LIKE 'B%' THEN 'North America'
WHEN continent='Caribbean' THEN 'South America'
ELSE continent
END
FROM
world*/

SELECT name, continent, CASE
WHEN continent = 'Oceania' THEN 'Australasia'
WHEN continent = 'Eurasia' THEN 'Europe/Asia'
WHEN name = 'Turkey' THEN 'Europe/Asia'
WHEN continent = 'Caribbean' AND name LIKE 'B%' then 'North America'
WHEN continent = 'Caribbean' THEN 'South America'
ELSE continent END
FROM world ORDER BY name

  

SELECT_from_Nobel_Tutorial

8.

Show the year, subject, and name of Physics winners for 1980 together with the Chemistry winners for 1984.

select
yr,
subject,
winner
from
nobel
where
(subject='Physics' AND yr='1980' or (subject='Chemistry' and yr='1984');

9.

Show the year, subject, and name of winners for 1980 excluding Chemistry and Medicine

select
yr,
subject,
winner
from
nobel
where yr=''and not subject in ('Chemistry','Medicine')

12.

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

Escaping single quotes
You can't put a single quote in a quote string directly. You can use two single quotes within a quoted string.
select
*
from
nobel
where
winner='EUGENE O\'NEILL'

13

Knights in order

List the winners, year and subject where the winner starts with Sir. Show the the most recent first, then by name order

select
winner,
yr,
subject
from
nobel
where
winner like 'Sir%'
order by
yr DESC,winner;

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.

SELECT winner, subject
FROM nobel
WHERE yr=1984
ORDER BY subject IN ('Physics','Chemistry'),subject,winner

SELECT within SELECT Tutorial/zh

2.

列出歐州每國家的人均GDP,當中人均GDP要高於英國'United Kingdom'的數值。

SELECT
name
from
world
where
continent='Europe'
and
gdp/population>
(
select
gdp/population
from
world
where
name='United Kingdom');

4.

哪一個國家的人口比加拿大Canada的多,但比波蘭Poland的少?列出國家名字name和人口population 。

SELECT
name,
population
from
world
where
population>
(select
population
from
world
where name='Canada')
and
population<
(select
population
from
world
where name='Poland')

5.

Germany德國(人口8000萬),在Europe歐洲國家的人口最多。Austria奧地利(人口850萬)擁有德國總人口的11%。

顯示歐洲的國家名稱name和每個國家的人口population。以德國的人口的百分比作人口顯示。

小數位數
您可以使用函數ROUND 刪除小數。
百分號 %
您可以使用函數 CONCAT 增加的百分比符號。
SELECT
name,
concat
  (round(population/
    (select
      population
    from
      world
    where
      name='Germany')*100,0),'%')
from
   world
where
   continent='Europe'

6.

哪些國家的GDP比Europe歐洲的全部國家都要高呢? [只需列出 name 。] (有些國家的記錄中,GDP是NULL,沒有填入資料的。)

SELECT
  name
FROM
  world
WHERE
  gdp>ALL
  (SELECT
    gdp
  FROM
    world
  WHERE
    continent='Europe' AND gdp>0);

我們可以在子查詢,參閱外部查詢的數值。我們為表格再命名,便可以分別內外兩個不同的表格。

7.

在每一個州中找出最大面積的國家,列出洲份 continent, 國家名字 name 及面積 area。 (有些國家的記錄中,AREA是NULL,沒有填入資料的。)

SELECT continent, name, area FROM world x
WHERE area >= ALL
(SELECT area FROM world y
WHERE y.continent=x.continent
AND area>0)

8.

列出洲份名稱,和每個洲份中國家名字按子母順序是排首位的國家名。(即每洲只有列一國)

select
continent,
name
from
world as a
where name<=all
(select
name from world as b
where a.continent=b.continent)

9.

找出洲份,當中全部國家都有少於或等於 25000000 人口. 在這些洲份中,列出國家名字namecontinent 洲份和population人口。

##################错误###################
select
name,
continent,
population
from
world as A
where
continent in
(select
continent
from
world
where
population>=ALL
(SELECT population from world
where population<=25000000));
####################正确#########################
SELECT name, continent, population FROM world x
WHERE 25000000 >= ALL(SELECT population
FROM world y
WHERE x.continent = y.continent
AND y.population>0);

10.

有些國家的人口是同洲份的所有其他國的3倍或以上。列出 國家名字name 和 洲份 continent。

select
name,
continent
from
world as a
where
population>=all(
select population*3 from world as b
where a.continent=b.continent
and b.population>0)
###################################

select
name,
continent
from
world as a
where
population>=all(
select population*3 from world as b
where a.continent=b.continent
and b.name != a.name)

SUM and COUNT/zh

2

列出所有的洲份, 每個只有一次。

SELECT
distinct continent
FROM
world

8.

列出有至少100百萬(1億)(100,000,000)人口的洲份。

SELECT
continent
FROM
world
GROUP BY
continent
HAVING
SUM(population)>'';

The JOIN operation

3.

我們可以利用JOIN來同時進行两个表格的查询操作。

SELECT *
FROM game JOIN goal ON (id=matchid)

語句FROM 表示合拼兩個表格game 和 goal的數據。語句 ON 表示如何找出 game中每一列應該配對goal中的哪一列 -- goal的 id 必須配對game的 matchid。 簡單來說,就是 
ON (game.id=goal.matchid)

以下SQL列出每個入球的球員(來自goal表格)和場館名(來自game表格)

修改它來顯示每一個德國入球的球員名,隊伍名,場館和日期。

SELECT player, teamid, stadium, mdate
FROM game JOIN goal ON (id=matchid)
WHERE teamid = 'GER'

4.

列出球員名字叫Mario (player LIKE 'Mario%')有入球的 隊伍1 team1, 隊伍2 team2 和 球員名 player

SELECT
team1,team2,player
FROM game JOIN goal ON (id=matchid)
WHERE
player LIKE 'Mario%';

6.

要合拼JOIN 表格game 和表格 eteam,你可以使用
game JOIN eteam ON (team1=eteam.id)

game JOIN eteam ON (team2=eteam.id)

注意欄位id同時是表格game 和表格 eteam的欄位,你要清楚指出eteam.id而不是只用id

列出'Fernando Santos'作為隊伍1 team1 的教練的賽事日期,和隊伍名。

SELECT
mdate,teamname
FROM game JOIN eteam ON (team1=eteam.id)
WHERE
coach='Fernando Santos'

8.

以下例子找出德國-希臘Germany-Greece 的八強賽事的入球

修改它,只列出全部賽事,射入德國龍門的球員名字。

找非德國球員的入球,德國可以在賽事中作team1 隊伍1(主)或team2隊伍2(客)。 你可以用teamid!='GER' 來防止列出德國球員。 你可以用DISTINCT來防止球員出現兩次以上。

# 错误:
/*SELECT DISTINCT player
FROM game JOIN goal ON (matchid = game.id)
WHERE (team1='GER' AND team2='GRE' OR (team1='GRE' AND team2='GER'))
AND teamid!='GER'*/
# 正确:
SELECT DISTINCT player
FROM game JOIN goal ON matchid = id
WHERE (team1= 'GER' OR team2='GER')
AND teamid != 'GER'

9.

列出隊伍名稱 teamname 和該隊入球總數
SELECT
teamname,
COUNT(player)
FROM
goal JOIN eteam ON (teamid=eteam.id)
GROUP BY
teamname

10.

列出場館名和在該場館的入球數字。
SELECT
stadium,
COUNT(player)
FROM
game JOIN goal ON (matchid=game.id)
GROUP BY
stadium;

11.

每一場波蘭'POL'有參與的賽事中,列出賽事編號 matchid, 日期date 和入球數字

# 不知是否理解
SELECT matchid,mdate,COUNT(*)
FROM game JOIN goal ON matchid = id
WHERE (team1 = 'POL' OR team2 = 'POL')
GROUP BY mdate,matchid # 被查询对象分散在两个表中,用group by排序也应该对两个对象排序,两个对象的顺序似乎不重要。

12.

每一場德國'GER'有參與的賽事中,列出賽事編號 matchid, 日期date 和德國的入球數字。

#错误:
/*SELECT
matchid,mdate,COUNT(*)
FROM
game JOIN goal ON (game.id = matchid)
WHERE
(team1='GER' OR team2='GER')
GROUP BY
matchid,mdate */
# 正确
SELECT matchid, mdate, COUNT(*) FROM goal
JOIN game ON (matchid=id)
WHERE teamid = 'GER'
GROUP BY matchid, mdate # 本题与11题,有差异! 本题求的是某一个队的入球,要限定条件(teamid = 'GER');11题不限定条件,任何队入球数字都算。

13.

List every match with the goals scored by each team as shown. This will use "CASE WHEN" which has not been explained in any previous exercises.

# 错误:
/* SELECT mdate,

DISTINCT team1,
SUM(CASE
WHEN teamid=team1 THEN 1
ELSE 0
END score1),
DISTINCT team2,
SUM(CASE
WHEN teamid=team2 THEN 1
ELSE 0
END score2)
FROM

game JOIN goal ON 

game.id = goal.matchid


GROUP BY
mdate, teamid,team1, team2;*/


## 正确:
SELECT DISTINCT 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 game.id = goal.matchid
GROUP BY id, mdate, team1, team2
ORDER BY mdate, matchid, team1, team2;

  

More JOIN operations

8.

顯示電影異型'Alien' 的演員清單。

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

9.

列出演員夏里遜福 'Harrison Ford' 曾演出的電影。

# 解法1
SELECT
title
FROM
movie
JOIN casting ON (casting.movieid=movie.id)
JOIN actor ON (actor.id=casting.actorid)
WHERE
name='Harrison Ford'
#解法2
SELECT title FROM casting
JOIN movie ON (movie.id = movieid)
JOIN actor ON (actor.id = actorid)
WHERE name = 'Harrison Ford'
# 表之间的连接顺序可以有差异

10.

列出演員夏里遜福 'Harrison Ford' 曾演出的電影,但他不是第1主角。

SELECT
title
FROM
movie
JOIN casting ON(id=movieid)
JOIN actor ON(actorid=actor.id)
WHERE
name='Harrison Ford'
AND ord>1;

11.

列出1962年首影的電影及它的第1主角。

SELECT
title,
name
FROM
movie
JOIN casting ON(id=movieid)
JOIN actor ON(actorid=actor.id)
WHERE
yr=1962
AND ord=1;

12.

尊·特拉華達'John Travolta'最忙是哪一年? 顯示年份和該年的電影數目。

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
)# 从HAVING 到最后只为求出 MAX(X)=3

13.

列出演員茱莉·安德絲'Julie Andrews'曾參與的電影名稱及其第1主角。

是否列了電影 "Little Miss Marker"兩次?

她於1980再參與此電影Little Miss Marker. 原作於1934年,她也有參與。 電影名稱不是獨一的。在子查詢中使用電影編號

## 错误
SELECT
title,
name
from
movie
JOIN casting ON(id=movieid)
JOIN actor ON(actorid=actor.id)
WHERE ord=1 AND
title
IN (
SELECT
title,
FROM movie
JOIN casting ON(movie.id=movieid)
JOIN actor ON(casting.actorid=actor.id)
WHERE
name='Julie Andrews'))
## 正确:
SELECT title, name FROM casting
JOIN movie ON movie.id = movieid
JOIN actor ON actor.id = actorid
WHERE ord = 1
AND movie.id IN
(SELECT movie.id FROM movie
JOIN casting ON movie.id = movieid
JOIN actor ON actor.id = actorid
WHERE actor.name = 'Julie Andrews')
# 这里应该用主键作为关联的条件,错误例中以title作为查询条件没有意义,而且过于功利。

14

列出按字母順序,列出哪一演員曾作30次第1主角。

# 错误:
SELECT
DISTINCT name
FROM casting
JOIN actor ON(actor.id=actorid)
JOIN movie ON(movie.id=movieid)
WHERE
actorid IN(
SELECT actorid
FROM casting
WHERE ord =1
GROUP BY
actorid
HAVING COUNT(actorid)>30)
ORDER BY name
# 正确:
SELECT DISTINCT name FROM casting
JOIN movie ON movie.id = movieid
JOIN actor ON actor.id = actorid
WHERE actorid IN (
SELECT actorid FROM casting
WHERE ord = 1
GROUP BY actorid
HAVING COUNT(actorid) >= 30)
ORDER BY name

……

Using Null

1.

List the teachers who have NULL for their department.

You might think that the phrase dept=NULL would work here but it doesn't

SELECT
name
FROM
teacher
WHERE
dept IS NULL

2.&3.

左右连接的区别

# 左连接 即以左边为标准,新添加的列如果条目少了,对不上,以 NULL 补充
SELECT
teacher.name,dept.name
FROM
teacher LEFT JOIN dept
ON (teacher.dept=dept.id); # 右连接 即以右边为标准,新添加的列如果条目少了,对不上,以 NULL 补充
SELECT teacher.name, dept.name 
FROM teacher RIGHT JOIN dept
ON (teacher.dept=dept.id)

5.

Use COALESCE to print the mobile number. Use the number '07986 444 2266' if there is no number given. Show teacher name and mobile number or '07986 444 2266'

SELECT
name,
COALESCE(mobile,'07986 444 2266')
FROM
teacher

6.

Use the COALESCE function and a LEFT JOIN to print the teacher name and department name. Use the string 'None' where there is no department.

SELECT
teacher.name,
COALESCE(dept.name,'None')
FROM teacher
LEFT JOIN dept
ON(teacher.dept=dept.id)

8.

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.dept)
FROM
teacher
RIGHT JOIN dept
ON(dept.id=teacher.dept)
GROUP BY dept.name;

SQLZOO 习题的更多相关文章

  1. Sharepoint学习笔记—习题系列--70-576习题解析 --索引目录

        Sharepoint学习笔记—习题系列--70-576习题解析  为便于查阅,这里整理并列出了70-576习题解析系列的所有问题,有些内容可能会在以后更新. 需要事先申明的是:     1. ...

  2. 《python核心编》程课后习题——第三章

    核心编程课后习题——第三章 3-1 由于Python是动态的,解释性的语言,对象的类型和内存都是运行时确定的,所以无需再使用之前对变量名和变量类型进行申明 3-2原因同上,Python的类型检查是在运 ...

  3. 习题 5: 更多的变量和打印 | 笨办法学 Python

    一. 简述 “格式化字符串(format string)” -  每一次你使用 ' ’ 或 " " 把一些文本引用起来,你就建立了一个字符串. 字符串是程序将信息展示给人的方式. ...

  4. 【WebGoat习题解析】Parameter Tampering->Bypass HTML Field Restrictions

    The form below uses HTML form field restrictions. In order to pass this lesson, submit the form with ...

  5. 练习sql语句的好去处——http://www.sqlzoo.cn/

    sql语句的编写需要按照实际的例子来练习. 如果自己来做准备,需要你自己搭好数据库,建好库和表,还要填入数据,最后自己想出题目和正确答案. 不过,现在我发现了一个好去处,http://www.sqlz ...

  6. python核心编程(第二版)习题

    重新再看一遍python核心编程,把后面的习题都做一下.

  7. SQL简单语句总结习题

    创建一个表记员工个人信息: --创建一个表 create table plspl_company_info( empno ) not null, ename ) not null, job ), ma ...

  8. 《Python核心编程》部分代码习题实践(持续更新)

    第三章 3-10 交换异常处理方式 代码: #makeTextFile.py #!/usr/bin/env python 'makeTextFile.py' import os ls = os.lin ...

  9. web实验指导书和课后习题参考答案

    实验指导书 :http://course.baidu.com/view/daf55bd026fff705cc170add.html 课后习题参考答案:http://wenku.baidu.com/li ...

随机推荐

  1. R Seurat 单细胞处理pipline 代码

    options(stringsAsFactors = F ) rm(list = ls()) library(Seurat) library(dplyr) library(ggplot2) libra ...

  2. Internet History, Technology, and Security(week5)——Technology: Internets and Packets

    前言: 之前都在学习Internet的历史,从这周开始,进入到了Internet技术的学习. Layer1: Link Introduction / The Link Layer 80年代之前,主流网 ...

  3. getAttribute和getParameter

    getAttribute表示从request范围取得设置的属性,必须要先setAttribute设置属性,才能通过getAttribute来取得,设置与取得的为Object对象类型 getParame ...

  4. android 后台运行service实现和后台的持续交互

    在项目中有这么一种需求 需要后台开启服务,时刻记录用户和软件的交互行为,一旦交互发生,就向服务器测发送一条消息 解决方案: 一.创建一个service服务类 在service中开启一个线程,servi ...

  5. Spring Boot 中使用 spring-boot-devtools (使用 Gradle 作为构建工具)

    Spring Boot 中使用 spring-boot-devtools (使用 Gradle 作为构建工具) 本文使用 Gradle 作为构建工具,关于 Gradle 构建工具,可以理解为是 Mav ...

  6. 编写灵活、稳定、高质量的HTML代码的规范

    一.唯一定律 无论有多少人共同参与同一项目,一定要确保每一行代码都像是唯一个人编写的. 二.HTML 2.1 语法 (1)用两个空格来代替制表符(tab) -- 这是唯一能保证在所有环境下获得一致展现 ...

  7. HDU 5172 GTY's gay friends (线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5172 题意: 给你一个n个数的数组,m次询问,询问在[L, R] 这个区间里面有没有 [1, R-L+ ...

  8. Sublime如何设置背景透明

    Sublime如何设置背景透明 下载sublime 透明背景插件 我用的是git下载插件: git clone https://github.com/vhanla/SublimeTextTrans.g ...

  9. 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第5节 String类_1_字符串概述和特点

    在api中查找 java.lang包里面的不用引用

  10. jmeter之关联操作

    测试接口过程中,常常会遇到这样的一个情况:上一个请求返回的数据,另外一个接口需要要使用.那么,使用Jmeter操作时我们常常可以用“关联”来实现. 以接口“登录”和“金币充值”为例:即在做“金币充值” ...