SQLZOO习题(我的错题)

2023-10-27

SQLZOO习题

一、All the vowels

(SELECT from WORLD Tutorial中的第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 (len(name) - len(replace(name,'a','')) = 1)
AND name LIKE '%e%' AND (len(name) - len(replace(name,'e','')) = 1)
AND name LIKE '%i%' AND (len(name) - len(replace(name,'i','')) = 1)
AND name LIKE '%o%' AND (len(name) - len(replace(name,'o','')) = 1)
AND name LIKE '%u%' AND (len(name) - len(replace(name,'u','')) = 1)
AND name NOT LIKE '% %'

备注

%a% 包含首字母是a的情况,且大小写均有

二、Nobel_Quiz

Nobel_Quiz的第十三题

Pick the code that shows the amount of years where no Medicine awards were given
十三题易错的两个选项

解释

第一个选项中,如果有年份又有‘Medicine’又有其他奖项,那么这个年份也会被选中,不符合题意。

三、Knights of the realm

SELECT_from_Nobel_Tutorial的第十三题 排序ORDER BY、DESC、ASC的应用

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 ASC;

备注

ORDER BY后面什么都没有,则默认升序排列。

四、Chemistry and Physics last

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, case when subject IN ('physics','chemistry') then 1
else 0 end from nobel
WHERE yr='1984'
ORDER BY 
case when subject IN ('physics','chemistry') then 1
else 0 end,
subject,winner

备注

其中case when subject IN ('physics','chemistry') then 1 else 0 end,
表示subject('physics','chemistry') 时赋值1,否则赋值0。

展示最后一列赋值情况,则结果如下:
结果
先按照最后一列0/1排,然后按照subject顺序排,最后按照winner顺序排。

五、The JOIN operation

The_JOIN_operation中的第八题
The example query shows all goals scored in the Germany-Greece quarterfinal.
Instead show the name of all players who scored a goal against Germany.

SELECT DISTINCT player
  FROM game JOIN goal ON matchid = id 
    WHERE ((team1='GER' OR team2='GER') AND teamid != 'GER')
    #最后一个WHERE语句仔细体会

六、The JOIN operation

The_JOIN_operation中的第八题

For every match involving ‘POL’, show the matchid, date and the number of goals scored.

SELECT matchid,mdate,COUNT(gtime)
  FROM game JOIN goal ON matchid = id 
 WHERE (team1 = 'POL' OR team2 = 'POL')
GROUP BY matchid,mdate#注意这里写两个,因为SELECT了两个,不然只GROUP BY其中一个的话,另一个怎么显示呢?

七、The JOIN operation

The_JOIN_operation中的第十三题
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.

mdate team1 score1 team2 score2
1 Jul 2012 ESP 4 ITA
10 June 2012 ESP 1 ITA 1
10 June 2012 IRL 1 CRO 3

Notice in the query given every goal is listed. If it was a team1 goal then a 1 appears in score1, otherwise there is a 0. You could SUM this column to get a count of the goals scored by team1. Sort your result by mdate, matchid, team1 and team2.

SELECT mdate,
       team1,
       SUM(CASE WHEN teamid = team1 THEN 1 ELSE 0 END) AS score1,
       team2,
       SUM(CASE WHEN teamid = team2 THEN 1 ELSE 0 END) AS score2 FROM
    game JOIN goal ON (id = matchid)
    GROUP BY mdate,team1,team2

八、Star Trek movies

More_JOIN_operations的第三题

List all of the Star Trek movies, include the id, title and yr (all of these movies include the words Star Trek in the title). Order results by year.

SELECT id, title, yr FROM  movie
 WHERE title LIKE '%Star Trek%'  ##注意这里是单引号
ORDER BY yr

注意:LIKE 字段,或者其他使用引号的地方,均为单引号!!!

九、Lead actor in Julie Andrews movies

More_JOIN_operations的第十二题

List the film title and the leading actor for all of the films ‘Julie Andrews’ played in.
列出“‘Julie Andrews’”出演的所有电影的片名和主角

SELECT title, name FROM movie
 JOIN casting ON movie.id=movieid
 JOIN actor   ON actorid=actor.id
WHERE movieid IN
(SELECT movieid FROM casting
WHERE actorid IN (
  SELECT id FROM actor
  WHERE name='Julie Andrews'))  AND ord = 1

备注

嵌套了很多子查询,最内层子查询查找出了’Julie Andrews’的actorid,次内层子查询,找出了’Julie Andrews’出演电影的movieid,最外层查询查找出这些电影的名字和主演。

十、Actors with 15 leading roles

More_JOIN_operations的第十三题

Obtain a list, in alphabetical order, of actors who’ve had at least 15 starring roles.

SELECT name FROM actor
JOIN casting ON actor.id=casting.actorid 
WHERE ord=1
GROUP BY name HAVING COUNT(*)>=15
ORDER BY name

十一、JOIN_Quiz_2

JOIN_Quiz_2的第三题第六题

  1. Select the statement that shows the list of actors called ‘John’ by order of number of movies in which they acted
SELECT name, COUNT(movieid)
  FROM casting JOIN actor ON actorid=actor.id
 WHERE name LIKE 'John %'
 GROUP BY name ORDER BY 2 DESC

注:ORDER BY 2 DESC,以第2列降序排列

  1. There are two sensible ways to connect movie and actor. They are:
  • link the director column in movies with the primary key in actor
  • connect the primary keys of movie and actor via the casting table

十二、不能用 “= NULL”,只能用“IS NULL”

在这里插入图片描述

十三、Using_Null_Quiz

  1. Using_Null_Quiz错题
    第四题注意:展示(display)和设置(set)的区别

十四、SQLZOO的最后一题

ind the routes involving two buses that can go from Craiglockhart to Lochend.
Show the bus no. and company for the first bus, the name of the stop for the transfer,
and the bus no. and company for the second bus.

Hint
Self-join twice to find buses that visit Craiglockhart and Lochend, then join those on matching stops.

答案

Select c.num,c.company,d.name,d.num,d.company from
(Select distinct a.num,a.company,name,a.stop from
(Select num,company,stop from route where (num,company) in(Select num,company from route
join stops on stops.id=route.stop
where stops.name='Craiglockhart') )a
join stops on stops.id=a.stop) c
join
(Select distinct b.num,b.company,name,b.stop from
(Select num,company,stop from route where (num,company) in(Select num,company from route
join stops on stops.id=route.stop
where stops.name='Lochend') )b
join stops on stops.id=b.stop) d
on c.name=d.name
order by c.num

详细解析

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SQLZOO习题(我的错题) 的相关文章

  • SQLAlchemy - 批量插入忽略:“重复条目”

    我有一个名为user data 列id and user id作为唯一的密钥 我想将一些历史数据导入到该表中 我用批量插入映射 http docs sqlalchemy org en rel 1 0 orm session api html
  • 如何将 javax.persistence.Column 定义为 Unsigned TINYINT?

    我正在基于 MySQL 数据库中的现有表创建 Java 持久性实体 Bean 使用 NetBeans IDE 8 0 1 我在这个表中遇到了一个字段 其类型为 无符号 TINYINT 3 我发现可以执行以下操作将列的类型定义为 unsign
  • 在 Mysql 上使用 EntityManager JPA 运行脚本

    我正在尝试运行脚本 sql 文件 但由于我尝试了多种方法 因此出现多个错误 这是我的主要 sql 脚本 INSERT INTO Unity VALUES 11 paq 0 2013 04 15 11 41 37 Admin Paquete
  • 将 MySQL 结果作为 PHP 数组

    mysql 表 config name config value allow autologin 1 allow md5 0 当前的 php 代码 sth mysql query SELECT rows array while r mysq
  • MySQL - 选择一行 - 然后相对于所选行的下一个和上一个

    我会尽力澄清这一点 我需要在不使用 id 的情况下选择特定行和该选定行的前一个相对行以及该选定行的下一个相对行 这可能吗 简而言之 上一篇和下一篇 我不能 也许我只是不知道如何 使用 id 的原因是因为它们不是按顺序排列的 正如您从这个相当
  • 在一个后台为MYSQL的网站上集成搜索

    我有一个位置搜索website http www jammulinks com对于一个城市 我们首先收集该城市所有可能类别的数据 如学校 学院 百货商店等 并将其信息存储在单独的表中 因为每个条目除了名称 地址和电话号码外都有不同的详细信息
  • 如何将行变成列?

    我有一个数据库 其中存储分组到项目中的关键字以及与每个关键字相关的数据 然后我显示每个项目的数据网格 每个关键字一行和几列 全部从同一个表 数据 中检索 我有 4 个表 关键字 项目 group keywords 和数据 keywords
  • 加载数据infile,Windows和Linux的区别

    我有一个需要导入到 MySQL 表的文件 这是我的命令 LOAD DATA LOCAL INFILE C test csv INTO TABLE logs fields terminated by LINES terminated BY n
  • 日期时间与时间戳字段

    我是 MySQL 数据库的新手 您是否建议在表创建中使用日期时间或时间戳字段以及原因 我正在使用 MySQL 5 7 和 innodb 引擎 Thanks 我会用TIMESTAMP对于任何需要自动管理的事情 因为它支持诸如ON UPDATE
  • 使用连接池后如何处理过多的并发连接?

    Scenario 假设您有一个拥有大量流量的网站或应用程序 即使使用数据库连接池 性能也会受到真正的打击 站点 应用程序甚至可能崩溃 因为并发连接太多 Question 人们有什么选择来处理这个问题 我的想法 我在想有这个问题的人可以创建多
  • 使用来自另一个数据库的选择查询更新 mysql 表

    我有两个数据库 我想用另一个数据库表中的值更新一个表 我正在使用以下查询 但它不起作用 UPDATE database1 table1 SET field2 database2 table1 field2 WHERE database1 t
  • 使用“INSERT ... ON DUPLICATE KEY UPDATE”插入多条记录

    我的表结构 table marks 我的目标 我想用条件插入或更新多条记录 我目前正在通过此查询进行检查 第一步 SELECT FROM marks WHERE student 115 AND param 1 第二步 if records
  • 从 call_log 中获取最大并发通话数

    我需要帮助在 MySQL 5 0 77 中编写一个查询 根据下面所示的数据 办公室一天的通话量 返回并发电话呼叫的峰值数量 我只是想知道一天中任何特定时间同时打电话的人数最多是多少 首先 这是 MySQL 表 CREATE TABLE ca
  • MySQL 和 PHP 参数 1 作为资源

    好吧 当我运行下面提到的代码时 PHP 向我抛出此错误 在日志中 Error mysql num rows 期望参数 1 为资源 第 10 行 place 中给出的字符串 9 11号线 queryFP SELECT FROM db coun
  • 无法在 Zend Framework 中回滚事务

    我在 Zend Framework 中使用以下代码进行事务 但回滚功能不起作用 数据通过 insertSome data 插入数据库 怎么了 db gt beginTransaction try model gt insertSome da
  • MySQL正则表达式:如何将字符串中的数字与\d匹配?

    我有一个专栏release date它以字符串格式存储日期 不是 DATETIME 格式 因为它们有时可以是任何其他字符串文字 我想根据给定的月份和年份查找任意日期的所有记录 尝试遵循但对我不起作用 gt Post find all con
  • JDBC 错误:在结果集开始之前[重复]

    这个问题在这里已经有答案了 我在 Java Eclipse 中收到错误消息 我在 MySql 中有一个数据库 它有列 String user name int id time int id desk int user password 我想
  • 使用 MYSQL 将 h:mm pm/am 时间格式插入数据库

    我正在尝试将以 h mm am pm 格式写入的时间插入到存储为标准 DATETIME 格式 hh mm ss 的数据库中 但我不知道如何将发布的时间转换为标准格式所以数据库会接受它 这是我到目前为止一直在尝试的 title POST in
  • 使用用户定义函数 MySql 时出错

    您好 请帮我解决这个问题 提前致谢 我在数据库中定义了这些函数 CREATE FUNCTION levenshtein s1 VARCHAR 255 s2 VARCHAR 255 RETURNS INT DETERMINISTIC BEGI
  • 休眠以持久保存日期

    有没有办法告诉 Hibernate java util Date 应该持久保存 我需要这个来解决 MySQL 中缺少的毫秒分辨率问题 您能想到这种方法有什么缺点吗 您可以自己创建字段long 或者使用自定义的UserType 实施后User

随机推荐

  • 一键磨皮插件:DR5白金版(支持ps 2022)中文版

    Delicious Retouch 5简称DR5 这里为大家分享最新激活的DR5白金版 for mac 这是非常受欢迎的一款PS一键磨皮插件 dr5插件提供了人像磨皮 平滑皮肤 去除瑕疵 美白牙齿 美白皮肤 修饰眼部等功能 一键点击即可使用
  • win10+CPU+Anaconda3 环境下pytorch安装

    本文主要对win10环境下 仅CPU运行 Anaconda3中安装pytorch的步骤进行了记录 主要包括以下内容 1 conda 创建虚拟环境 2 conda 添加镜像源 3 pytorch 安装 4 pytorch 成功安装验证 con
  • Unity官网打不开,试试新地址吧!

    今年6月份发现unity官网进不去了 unity3d com unity com cn 下载历史版本的地址也打不开 网上也有很多人求助 新域名如下 不需要挂vpn 新地址 https unity cn 历史版本下载 https unity
  • Python多线程的理解和使用(一)Threading中join()函数的理解

    转载自 https blog csdn net zhuzuwei article details 80927554 多线程的概念 多线程类似于同时执行多个不同程序 多线程运行有如下优点 使用线程可以把占据长时间的程序中的任务放到后台去处理
  • python操作redis

    目录 python操作redis 安装redis模块 基本链接 连接池连接 redis字符串操作 redis hash操作 redis 列表操作 redis 其它操作 redis管道 django中集成redis python操作redis
  • 为本地项目配置git地址,并推送到远程仓库

    1 进入该项目文件夹 cd Users kk Desktop project k demo 将上面项目路径换成你自己的项目路径 2 初始化git 使用git init 将该项目变成一个可以通过git管理的项目 git init 3 通过gi
  • vue-cli使用指南

    目录 vue全家桶 技术栈 使用vue cli搭建Vue项目 单页的编写 axios的使用 vuex的使用 vuex中数据的保留时间 axios的全局拦截 axios的跨域问题 全局常量 使用Mock模拟后端接口返回数据 嵌套路由 路由守卫
  • 95-38-030-Buffer-Java NIO中-关于DirectBuffer,HeapBuffer的疑问

    文章目录 1 说明 2 疑问 3 RednaxelaFX 1 说明 本文摘要 https www zhihu com question 57374068 2 疑问 Java NIO中 关于DirectBuffer HeapBuffer的疑问
  • CUDA的几种Synchronize

    首先对这三个函数做一下解释 cudaDeviceSynchronize 等待所有线程都处理完成 kernel function处理完成 用在cpu的c code中 cudaThreadSynchronize 功能和cudaDeviceSyn
  • Pycharm的使用技巧与效率提升

    总第010篇 本文主要梳理了pycharm在使用过程中的一些技巧 便于提升工作效率 pycharm主要分为两个版本 一个是专业版本 此版本功能强大 主要是为python和web开发者准备的 需要付费 另一个是社区版本 比较轻量级 主要是为p
  • 读别人写的代码 VS 自己写代码

    概述 专业程序员非常重要的一项技能是读别人写的代码 这项技能甚至比自己写代码更重要 分析 这让我想到很多程序员讨厌去阅读代码 来接受它吧 人人都喜欢编写代码 写代码是很有乐趣的事 但阅读代码却是一种困难的工作 它不仅仅繁重 而且很无聊 让我
  • 使用Docker进行模型部署

    文章目录 1 思路 2 实现步骤 2 1 数据 模型准备 2 2 镜像制作 2 3 使用 1 思路 因为多数公司正式集群都不能使用公网环境 对于模型部署比较麻烦 所以想这在公网环境下完成模型调试 然后根据相关环境和参数直接制作一个docke
  • C++中static_cast/const_cast/dynamic_cast/reinterpret_cast的区别和使用

    C风格的强制转换较简单 如将float a转换为int b 则可以这样 b int a 或者b int a C 类型转换分为隐式类型转换和显示类型转换 隐式类型转换又称为标准转换 包括以下几种情况 1 算术转换 在混合类型的算术表达式中 最
  • gsonformat java代码_插件GsonFormat快速實現JavaBean

    寫在前面的話 本文章只適合使用AndroidStudio的小伙伴觀看 還在糾結eclipse的小伙伴趕緊洗洗睡吧 最近看見一篇快速實現javaBean的屎丟丟插件 這是一個根據JSONObject格式的字符串 自動生成實體類參數 如果想要使
  • windows编程中wParam和lParam消息

    windows编程中wParam和lParam消息 1 WM PAINT消息 LOWORD lParam 是客户区的宽 HIWORD lParam 是客户区的高 2 滚动条WM VSCROLL或WM HSCROLL消息 LOWORD wPa
  • 【Vim】Vim 常用编辑操作

    目录 正则表达式 vim 命令 vim的工作模式 撤销修改 重做与保存 光标移动命令 文本插入操作 文本删除操作 文本复制 剪切与粘贴 文本的修改与替换 多窗口操作 正则表达式 简单地说 正则表达式是一种符号表示法 用于识别文本模式 在某种
  • 神经网络中epoch、batch、batch_size、epoch、iteration理解

    1 epoch 当一个完整的数据集通过神经网络一次并且返回一次的过程称为一个epoch 然而 当一个epoch对于计算机太过庞大时 就需要把它分成多个小块 2 batch 在不能将数据一次性通过神经网络的适合 就需要将数据集分成几个batc
  • 记录服务器上,不定时出现io.lettuce.core.RedisCommandTimeoutException: Command timed out after xxx millisecond(s)

    记录服务器上 不定时出现io lettuce core RedisCommandTimeoutException Command timed out after 12 millisecond s 日志 org springframework
  • E--释怀的RT--2023河南萌新联赛第(二)场:河南工业大学

    示例1 输入 5 0 1 0 0 10 输出 4 说明 前四个方格被最后一个心岩照亮 示例2 输入 5 0 1 0 0 1 输出 3 说明 第一个方格和第三个方格被第二个格子的心岩照亮 第四个方格被第五个格子的心岩照亮 一共有三个格子被照亮
  • SQLZOO习题(我的错题)

    SQLZOO习题 目录 SQLZOO习题 一 All the vowels 参考答案 备注 二 Nobel Quiz 解释 三 Knights of the realm 答案 备注 四 Chemistry and Physics last