SQLzoo 习题记录03-SELECT from Nobel Tutorial & Nobel Quiz

本文汇总了1950年至1962年间的诺贝尔奖得主,包括化学家Willard F. Libby、文学家Saint-John Perse和科学家Albert Einstein等,详细展示了这些领域的获奖情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

SELECT from Nobel Tutorial

  1. Nobel Laureates
  2. Winners from 1950
  3. 1962 Literature
  4. Albert Einstein
  5. Recent Peace Prizes
  6. Literature in the 1980's
  7. Only Presidents
  8. John
  9. Chemistry and Physics from different years
  10. Exclude Chemists and Medics
  11. Early Medicine, Late Literature
  12. Harder Questions
  13. Umlaut
  14. Apostrophe
  15. Knights of the realm
  16. Chemistry and Physics last

Nobel Quiz


SELECT from Nobel Tutorial

网址:SELECT from Nobel Tutorial - SQLZOO

nobel
yrsubjectwinner
1960ChemistryWillard F. Libby
1960LiteratureSaint-John Perse
1960MedicineSir Frank Macfarlane Burnet
1960MedicinePeter Madawar
...

Nobel Laureates

We continue practicing simple SQL queries on a single table.

This tutorial is concerned with a table of Nobel prize winners:

nobel(yr, subject, winner)

Using the SELECT statement.

Winners from 1950

1.

Change the query shown so that it displays Nobel prizes for 1950.

  • SELECT yr, subject, winner FROM nobel
  • WHERE yr = 1950;

1962 Literature

2.

Show who won the 1962 prize for Literature.

  • SELECT winner FROM nobel
  •  WHERE yr = 1962
  •        AND subject = 'Literature';

Albert Einstein

3.

Show the year and subject that won 'Albert Einstein' his prize.

  • Select yr,subject From nobel
  • Where winner = 'Albert Einstein';

Recent Peace Prizes

4.

Give the name of the 'Peace' winners since the year 2000, including 2000.

  • Select winner From nobel
  • Where yr>=2000
  •    And subject= 'Peace' ;

Literature in the 1980's

5.

Show all details (yrsubjectwinner) of the Literature prize winners for 1980 to 1989 inclusive.

  • Select yr,subject,winner From nobel
  • Where subject='Literature'
  •     And yr between 1980 and 1989;

Only Presidents

6.

Show all details of the presidential winners:

  • Theodore Roosevelt
  • Woodrow Wilson
  • Jimmy Carter
  • Barack Obama
  • SELECT * FROM nobel
  •  WHERE winner in ('Theodore Roosevelt',
  • 'Woodrow Wilson',
  • 'Jimmy Carter',
  • 'Barack Obama');

John

7.

Show the winners with first name John

  • Select winner from nobel
  • Where winner like 'John%';

Chemistry and Physics from different years

8.

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

  • Select * from nobel
  • Where subject = 'Physics' and yr=1980
  •       Or subject='Chemistry' and yr=1984;

Exclude Chemists and Medics

9.

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

  • Select * from nobel
  • Where yr = 1980 and subject not in ('Chemistry','Medicine');

Early Medicine, Late Literature

10.

Show year, subject, and name of people who won a 'Medicine' prize in an early year (before 1910, not including 1910) together with winners of a 'Literature' prize in a later year (after 2004, including 2004)

  • Select * from nobel
  • Where subject = 'Medicine' and yr <1910
  •       Or subject = 'Literature' and yr >=2004;

Harder Questions

Umlaut

11.

Find all details of the prize won by PETER GRÜNBERG

Non-ASCII characters

  • Select * from nobel
  • Where winner ='PETER GRÜNBERG';

Apostrophe

12.

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

Escaping single quotes, 查询含有单引号的字符串时要改为双单引号。

  • Select * from nobel
  • Where winner ='EUGENE O''NEILL';

Knights of the realm

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;

Chemistry and Physics last

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.

做不出来QAQ,求教。

 

Nobel Quiz

网址:Nobel Quiz - SQLZOO

nobel
yrsubjectwinner
1960ChemistryWillard F. Libby
1960LiteratureSaint-John Perse
1960MedicineSir Frank Macfarlane Burnet
1960MedicinePeter Medawar
1960PhysicsDonald A. Glaser
1960PeaceAlbert Lutuli
...

1. Pick the code which shows the name of winner's names beginning with C and ending in n

  • SELECT winner FROM nobel
  • WHERE winner LIKE 'C%' AND winner LIKE '%n'

2. Select the code that shows how many Chemistry awards were given between 1950 and 1960

  • SELECT COUNT(subject) FROM nobel
  • WHERE subject = 'Chemistry' AND yr BETWEEN 1950 and 1960

3. Pick the code that shows the amount of years where no Medicine awards were given

  • SELECT COUNT(DISTINCT yr) FROM nobel
  • WHERE yr NOT IN (SELECT DISTINCT yr FROM nobel WHERE subject = 'Medicine')

4. Select the result that would be obtained from the following code:

  • SELECT subject, winner FROM nobel
  • WHERE winner LIKE 'Sir%' AND yr LIKE '196%'
MedicineSir John Eccles
MedicineSir Frank Macfarlane Burnet

5. Select the code which would show the year when neither a Physics or Chemistry award was given

  • SELECT yr FROM nobel
  • WHERE yr NOT IN (SELECT yr FROM nobel WHERE subject IN ('Chemistry','Physics'))

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')

7. Pick the result that would be obtained from the following code:

  • SELECT subject, COUNT(subject)
  • FROM nobel
  • WHERE yr ='1960'
  • GROUP BY subject
Chemistry1
Literature1
Medicine2
Peace1
Physics1

 

&lt;think&gt;首先,用户的问题是:理解在SQL查询中为何需要使用DISTINCT修饰列yr,并将其转换为COUNT(DISTINCT yr)来统计唯一年份的数量。同时,用户问COUNT(DISTINCT column)的使用场景及作用。 根据系统级指令: - 所有行内数学表达式必须使用$...$格式,但这里没有数学表达式,所以可能不需要。 - 独立公式必须使用$$...$$格式并单独成段,但问题中没有公式。 - LaTeX语法正确:确保任何数学表达正确。 - 使用中文回答:整个响应必须用中文。 - 生成相关问题:在回答结束时,生成相关问题。 - 回答中引用的段落末尾自然地添加引用标识:如[^1]。 用户可见层要求: - 回答结构清晰,帮助用户逐步解决问题。 - 保证回答真实可靠。 参考引用提供了信息: - 引用[1]:COUNT(DISTINCT column_name) 返回指定列的不同值的数目,适用于Oracle和SQL Server,但不用于Access。 - 引用[2]:在MySQL中,COUNT(DISTINCT id) 计算不同id的记录数。 - 引用[3]:在Hive中,count(distinct) 的优化,数据量大的时候可能不如group by快。 - 引用[4]:COUNT函数计算行数,COUNT(DISTINCT column) 计算不同值的行数。 关键点:用户想知道为什么用DISTINCT修饰yr,并用COUNT(DISTINCT yr)统计唯一年份。 核心概念: - COUNT(column) 计算该列非空值的数量。 - DISTINCT 用于去除重复值。 - COUNT(DISTINCT column) 结合两者,计算该列中不同值的数量。 使用场景: - 当需要统计某一列的唯一值数量时,比如统计不同年份、不同用户ID等。 - 例如,在表中,yr列可能有重复年份,COUNT(yr) 会返回所有非空yr值的计数,包括重复;而COUNT(DISTINCT yr) 只计算唯一年份的数量。 作用: - 避免重复计数,提供准确的唯一值统计。 - 在数据分析中,常用于去重统计。 现在,构建回答: 1. **结构清晰,逐步解决问题**: - 先解释问题:为什么需要DISTINCT修饰yr。 - 然后解释COUNT(DISTINCT yr)的作用。 - 最后讨论使用场景和好处。 2. **使用中文**。 3. **添加引用标识**:在相关段落末尾添加,如[^1]。 4. **生成相关问题**:在结束时,基于主题生成几个问题。 回答大纲: - 引言:重述问题。 - 解释基础概念: - COUNT函数的作用。 - DISTINCT关键字的作用。 - 结合使用COUNT(DISTINCT column)。 - 具体到yr列:为什么需要它来统计唯一年份。 - 使用场景:列举例子。 - 注意事项:如性能问题(引用[3]提到数据量大时优化)。 - 总结。 - 相关问题。 添加引用: - 引用[1]:COUNT(DISTINCT) 的基本用法。 - 引用[2]:MySQL中的使用。 - 引用[3]:性能考虑。 - 引用[4]:COUNT函数的详细解释。 最终响应:&lt;/think&gt;### SQL中COUNT(DISTINCT column)的使用场景及作用 在SQL查询中,使用`DISTINCT`修饰列`yr`(例如`COUNT(DISTINCT yr)`)是为了确保统计`yr`列的唯一值数量,避免重复计数。下面我将逐步解释其必要性、作用机制、使用场景及注意事项,帮助您全面理解这一功能。 #### 1. **为什么需要DISTINCT修饰列yr?** - **基础概念回顾**: - `COUNT(column)` 函数用于计算指定列的非空值数量,但它会包含重复值。例如,如果`yr`列有值 `[2020, 2020, 2021]`,则`COUNT(yr)` 返回 `3`(所有非空值的总数)。 - `DISTINCT` 关键字用于去除重复值,只保留唯一值。例如,`SELECT DISTINCT yr FROM table` 会返回 `[2020, 2021]`。 - 结合使用时,`COUNT(DISTINCT yr)` 先对`yr`列去重,再计算唯一值的数量。在上述例子中,它返回 `2`(唯一年份的数量)。 - **必要性**: 如果直接使用`COUNT(yr)`,统计结果会包含重复年份,无法反映真实的&ldquo;唯一年份数量&rdquo;。这在数据分析中会导致误导,例如统计年度报告中的独立年份时,必须确保每个年份只计数一次。`COUNT(DISTINCT yr)` 通过去重机制解决了这一问题,提供精确的唯一值统计[^1][^4]。 #### 2. **COUNT(DISTINCT column)的作用机制** - **语法结构**:`COUNT(DISTINCT column_name)` 接受一个列名作为参数,返回该列不同值的数目。 - 示例:`SELECT COUNT(DISTINCT yr) FROM sales;` 会计算`sales`表中`yr`列的唯一年份数。 - **核心作用**: - **去重统计**:高效计算列中不重复值的数量,适用于需要唯一性计数的场景。 - **兼容性**:在主流数据库如Oracle、MySQLSQL Server中支持,但部分系统如Microsoft Access不支持此语法(需改用其他方法,如子查询)[^1][^2]。 - **性能特点**: 在数据量小或列值重复率高时,`COUNT(DISTINCT)` 通常高效;但在大数据量(如百万行)下,它可能比`GROUP BY` + `COUNT`慢,因为`DISTINCT`需要额外排序和去重操作。优化时可考虑数据量或使用索引[^3][^4]。 #### 3. **使用场景及实际示例** `COUNT(DISTINCT column)` 主要用于需要统计唯一值的业务场景,以下是一些常见例子: - **场景1:统计唯一年份或日期** 例如,在销售表中统计有销售记录的唯一年份: ```sql -- 返回yr列的不同年份数量,如结果为5表示有5个不同年份 SELECT COUNT(DISTINCT yr) FROM sales; ``` 如果不加`DISTINCT`,`COUNT(yr)` 可能返回重复年份的总数(如100条记录中有10个重复年份),无法反映实际年份跨度[^4]。 - **场景2:计算独立用户数** 在用户行为表中,统计访问过网站的不同用户ID: ```sql -- 返回user_id列的唯一用户数量 SELECT COUNT(DISTINCT user_id) FROM access_log; ``` 这比先`GROUP BY user_id`再计数更简洁[^2]。 - **场景3:数据质量检查** 例如,检查产品表中分类列的唯一值数量,确保无重复分类: ```sql -- 如果结果小于总行数,说明有重复分类 SELECT COUNT(DISTINCT category) FROM products; ``` - **场景4:多列去重统计** 在支持多列的数据库中(如MySQL),可统计组合唯一值: ```sql -- 返回yr和region列组合的不同值数量 SELECT COUNT(DISTINCT yr, region) FROM sales; ``` 这在分析区域年度分布时很有用[^4]。 #### 4. **注意事项** - **性能优化**: 大数据量下,`COUNT(DISTINCT)` 可能较慢。可改用`GROUP BY` + 子查询(如`SELECT COUNT(*) FROM (SELECT yr FROM sales GROUP BY yr) tmp;`),或在列上创建索引加速去重[^3]。 - **空值处理**: `COUNT(DISTINCT column)` 忽略NULL值,只计算非空唯一值。这与`COUNT(*)`(统计所有行)不同[^4]。 - **数据库差异**: - Oracle和SQL Server支持`COUNT(DISTINCT)`。 - MySQL中效率较高,但超大数据集需谨慎。 - Hive等大数据工具中,数据量小时`COUNT(DISTINCT)`快,但数据量大时可能不如`GROUP BY`优化[^3]。 - Microsoft Access不支持,需用替代方案如`SELECT COUNT(*) FROM (SELECT DISTINCT yr FROM sales);`[^1]。 #### 总结 使用`COUNT(DISTINCT yr)` 是统计唯一年份数量的标准方法,它通过去重确保结果准确,适用于需要唯一性计数的分析场景。核心优势是简洁高效,但需注意数据量和数据库兼容性。在实际查询中,优先评估数据规模以选择最优方案[^1][^3][^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值