题目
这里有张 World 表+-----------------+------------+------------+--------------+---------------+| name | continent | area | population | gdp |+-----------------+------------+------------+--------------+---------------+| Afghanistan | Asia | 652230 | 25500100 | 20343000 || Albania | Europe | 28748 | 2831741 | 12960000 || Algeria | Africa | 2381741 | 37100000 | 188681000 || Andorra | Europe | 468 | 78115 | 3712000 || Angola | Africa | 1246700 | 20609294 | 100990000 |+-----------------+------------+------------+--------------+---------------+如果一个国家的面积超过300万平方公里,或者人口超过2500万,那么这个国家就是大国家。编写一个SQL查询,输出表中所有大国家的名称、人口和面积。例如,根据上表,我们应该输出:+--------------+-------------+--------------+| name | population | area |+--------------+-------------+--------------+| Afghanistan | 25500100 | 652230 || Algeria | 37100000 | 2381741 |+--------------+-------------+--------------+
解答
# Write your MySQL query statement belowselect name,population,area from World where area>3000000 or population>25000000;
题目
给定一个 salary 表,如下所示,有 m = 男性 和 f = 女性 的值。交换所有的 f 和 m 值(例如,将所有 f 值更改为 m,反之亦然)。要求只使用一个更新(Update)语句,并且没有中间的临时表。注意,您必只能写一个 Update 语句,请不要编写任何 Select 语句。例如:| id | name | sex | salary || --- | ---- | --- | ------ || 1 | A | m | 2500 || 2 | B | f | 1500 || 3 | C | m | 5500 || 4 | D | f | 500 |运行你所编写的更新语句之后,将会得到以下表:| id | name | sex | salary || --- | ---- | --- | ------ || 1 | A | f | 2500 || 2 | B | m | 1500 || 3 | C | f | 5500 || 4 | D | m | 500 |
解答
# 考察 if 或 case 的用法# 1, 用ifupdate salaryset sex = if(sex='m', 'f', 'm')# 2, 用caseupdate salaryset sex=case sexwhen 'm' then 'f'when 'f' then 'm'end;
题目
某城市开了一家新的电影院,吸引了很多人过来看电影。该电影院特别注意用户体验,专门有个 LED显示板做电影推荐,上面公布着影评和相关电影描述。作为该电影院的信息部主管,您需要编写一个 SQL查询,找出所有影片描述为非 boring (不无聊) 的并且 id 为奇数 的影片,结果请按等级 rating 排列。例如,下表 cinema:+---------+-----------+--------------+-----------+| id | movie | description | rating |+---------+-----------+--------------+-----------+| 1 | War | great 3D | 8.9 || 2 | Science | fiction | 8.5 || 3 | irish | boring | 6.2 || 4 | Ice song | Fantacy | 8.6 || 5 | House card| Interesting| 9.1 |+---------+-----------+--------------+-----------+对于上面的例子,则正确的输出是为:+---------+-----------+--------------+-----------+| id | movie | description | rating |+---------+-----------+--------------+-----------+| 5 | House card| Interesting| 9.1 || 1 | War | great 3D | 8.9 |+---------+-----------+--------------+-----------+
解答
# Write your MySQL query statement below# 1select * from cinema where id%2=1 and not description="boring" order by rating desc;# 2select * from cinema where id%2=1 and description!="boring" order by rating desc;
题目
有一个courses 表 ,有: student (学生) 和 class (课程)。请列出所有超过或等于5名学生的课。例如,表:+---------+------------+| student | class |+---------+------------+| A | Math || B | English || C | Math || D | Biology || E | Math || F | Computer || G | Math || H | Math || I | Math |+---------+------------+应该输出:+---------+| class |+---------+| Math |+---------+
解答
# Write your MySQL query statement belowselect class from courses group by class having count(distinct student)>=5;
题目
编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。示例:+----+---------+| Id | Email |+----+---------+| 1 | a@b.com || 2 | c@d.com || 3 | a@b.com |+----+---------+根据以上输入,你的查询应返回以下结果:+---------+| Email |+---------+| a@b.com |+---------+
解答
# Write your MySQL query statement belowselect Email from Person group by Email having count(Email)>=2;
题目
编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个。+----+------------------+| Id | Email |+----+------------------+| 1 | john@example.com || 2 | bob@example.com || 3 | john@example.com |+----+------------------+Id 是这个表的主键。例如,在运行你的查询语句之后,上面的 Person 表应返回以下几行:+----+------------------+| Id | Email |+----+------------------+| 1 | john@example.com || 2 | bob@example.com |+----+------------------+
解答
DELETE from Person Where Id not in ( select id from # 加上这个外层筛选可以避免You can't specify target table for update in FROM clause错误 ( Select MIN(Id) as id From Person Group by Email ) t)