595. 大的国家

题目

  1. 这里有张 World
  2. +-----------------+------------+------------+--------------+---------------+
  3. | name | continent | area | population | gdp |
  4. +-----------------+------------+------------+--------------+---------------+
  5. | Afghanistan | Asia | 652230 | 25500100 | 20343000 |
  6. | Albania | Europe | 28748 | 2831741 | 12960000 |
  7. | Algeria | Africa | 2381741 | 37100000 | 188681000 |
  8. | Andorra | Europe | 468 | 78115 | 3712000 |
  9. | Angola | Africa | 1246700 | 20609294 | 100990000 |
  10. +-----------------+------------+------------+--------------+---------------+
  11. 如果一个国家的面积超过300万平方公里,或者人口超过2500万,那么这个国家就是大国家。
  12. 编写一个SQL查询,输出表中所有大国家的名称、人口和面积。
  13. 例如,根据上表,我们应该输出:
  14. +--------------+-------------+--------------+
  15. | name | population | area |
  16. +--------------+-------------+--------------+
  17. | Afghanistan | 25500100 | 652230 |
  18. | Algeria | 37100000 | 2381741 |
  19. +--------------+-------------+--------------+

解答

  1. # Write your MySQL query statement below
  2. select name,population,area from World where area>3000000 or population>25000000;

627. 交换工资

题目

  1. 给定一个 salary 表,如下所示,有 m = 男性 f = 女性 的值。交换所有的 f m 值(例如,将所有 f 值更改为 m,反之亦然)。要求只使用一个更新(Update)语句,并且没有中间的临时表。
  2. 注意,您必只能写一个 Update 语句,请不要编写任何 Select 语句。
  3. 例如:
  4. | id | name | sex | salary |
  5. | --- | ---- | --- | ------ |
  6. | 1 | A | m | 2500 |
  7. | 2 | B | f | 1500 |
  8. | 3 | C | m | 5500 |
  9. | 4 | D | f | 500 |
  10. 运行你所编写的更新语句之后,将会得到以下表:
  11. | id | name | sex | salary |
  12. | --- | ---- | --- | ------ |
  13. | 1 | A | f | 2500 |
  14. | 2 | B | m | 1500 |
  15. | 3 | C | f | 5500 |
  16. | 4 | D | m | 500 |

解答

  1. # 考察 if 或 case 的用法
  2. # 1, 用if
  3. update salary
  4. set sex = if(sex='m', 'f', 'm')
  5. # 2, 用case
  6. update salary
  7. set sex=
  8. case sex
  9. when 'm' then 'f'
  10. when 'f' then 'm'
  11. end;

620. 有趣的电影

题目

  1. 某城市开了一家新的电影院,吸引了很多人过来看电影。该电影院特别注意用户体验,专门有个 LED显示板做电影推荐,上面公布着影评和相关电影描述。
  2. 作为该电影院的信息部主管,您需要编写一个 SQL查询,找出所有影片描述为非 boring (不无聊) 的并且 id 为奇数 的影片,结果请按等级 rating 排列。
  3. 例如,下表 cinema:
  4. +---------+-----------+--------------+-----------+
  5. | id | movie | description | rating |
  6. +---------+-----------+--------------+-----------+
  7. | 1 | War | great 3D | 8.9 |
  8. | 2 | Science | fiction | 8.5 |
  9. | 3 | irish | boring | 6.2 |
  10. | 4 | Ice song | Fantacy | 8.6 |
  11. | 5 | House card| Interesting| 9.1 |
  12. +---------+-----------+--------------+-----------+
  13. 对于上面的例子,则正确的输出是为:
  14. +---------+-----------+--------------+-----------+
  15. | id | movie | description | rating |
  16. +---------+-----------+--------------+-----------+
  17. | 5 | House card| Interesting| 9.1 |
  18. | 1 | War | great 3D | 8.9 |
  19. +---------+-----------+--------------+-----------+

解答

  1. # Write your MySQL query statement below
  2. # 1
  3. select * from cinema where id%2=1 and not description="boring" order by rating desc;
  4. # 2
  5. select * from cinema where id%2=1 and description!="boring" order by rating desc;

596. 超过5名学生的课

题目

  1. 有一个courses ,有: student (学生) class (课程)。
  2. 请列出所有超过或等于5名学生的课。
  3. 例如,表:
  4. +---------+------------+
  5. | student | class |
  6. +---------+------------+
  7. | A | Math |
  8. | B | English |
  9. | C | Math |
  10. | D | Biology |
  11. | E | Math |
  12. | F | Computer |
  13. | G | Math |
  14. | H | Math |
  15. | I | Math |
  16. +---------+------------+
  17. 应该输出:
  18. +---------+
  19. | class |
  20. +---------+
  21. | Math |
  22. +---------+

解答

  1. # Write your MySQL query statement below
  2. select class from courses group by class having count(distinct student)>=5;

182. 查找重复的电子邮箱

题目

  1. 编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。
  2. 示例:
  3. +----+---------+
  4. | Id | Email |
  5. +----+---------+
  6. | 1 | a@b.com |
  7. | 2 | c@d.com |
  8. | 3 | a@b.com |
  9. +----+---------+
  10. 根据以上输入,你的查询应返回以下结果:
  11. +---------+
  12. | Email |
  13. +---------+
  14. | a@b.com |
  15. +---------+

解答

  1. # Write your MySQL query statement below
  2. select Email from Person group by Email having count(Email)>=2;

196. 删除重复的电子邮箱

题目

  1. 编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个。
  2. +----+------------------+
  3. | Id | Email |
  4. +----+------------------+
  5. | 1 | john@example.com |
  6. | 2 | bob@example.com |
  7. | 3 | john@example.com |
  8. +----+------------------+
  9. Id 是这个表的主键。
  10. 例如,在运行你的查询语句之后,上面的 Person 表应返回以下几行:
  11. +----+------------------+
  12. | Id | Email |
  13. +----+------------------+
  14. | 1 | john@example.com |
  15. | 2 | bob@example.com |
  16. +----+------------------+

解答

  1. DELETE from Person
  2. Where Id not in
  3. (
  4. select id from
  5. # 加上这个外层筛选可以避免You can't specify target table for update in FROM clause错误
  6. (
  7. Select MIN(Id) as id
  8. From Person
  9. Group by Email
  10. ) t
  11. )