LeetCode—数据库简单题(三)

1. 组合两个表(175)

表2: Address

±------------±--------+
| 列名 | 类型 |
±------------±--------+
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
±------------±--------+
AddressId 是上表主键

编写一个 SQL 查询,满足条件:无论 person 是否有地址信息,都需要基于上述两表提供 person 的以下信息:

FirstName, LastName, City, State

解法

这道题就是一道简单的左外链接

# Write your MySQL query statement below
select FirstName, LastName, City, State from Person 
left join Address on Person.PersonId = Address.PersonId

2. 第二高的薪水(176)

编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) 。

±—±-------+
| Id | Salary |
±—±-------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
±—±-------+
例如上述 Employee 表,SQL查询应该返回 200 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 null。

±--------------------+
| SecondHighestSalary |
±--------------------+
| 200 |
±--------------------+

解法1

寻找数组中第二大的数,我们只要在选择的时候去掉最大的数就好,所以我们使用一个子查询来解决:

# Write your MySQL query statement below
select max(Salary) as SecondHighestSalary from Employee 
where Salary < (select max(Salary) from Employee)

解法2

题目要求不存在返回null,则用ifnull函数
ifnull(A,B) 若不为空返回A,否则返回B

可能存在相同薪水的情况,所以用distinct去重
order by分类 desc–降序,默认是asc
limit[offset,n] – offset是偏移量,即跳过多少行,n是需要取出的条目

# Write your MySQL query statement below
SELECT IFNULL(
(SELECT DISTINCT Salary 
FROM Employee
ORDER BY Salary DESC LIMIT 1,1),NULL)
AS SecondHighestSalary;

3. 超过经理收入的员工(181)

Employee 表包含所有员工,他们的经理也属于员工。每个员工都有一个 Id,此外还有一列对应员工的经理的 Id。

±—±------±-------±----------+
| Id | Name | Salary | ManagerId |
±—±------±-------±----------+
| 1 | Joe | 70000 | 3 |
| 2 | Henry | 80000 | 4 |
| 3 | Sam | 60000 | NULL |
| 4 | Max | 90000 | NULL |
±—±------±-------±----------+
给定 Employee 表,编写一个 SQL 查询,该查询可以获取收入超过他们经理的员工的姓名。在上面的表格中,Joe 是唯一一个收入超过他的经理的员工。

±---------+
| Employee |
±---------+
| Joe |
±---------+

解法

用自链接来写

# Write your MySQL query statement below
select e1.Name as Employee from Employee as e1, Employee as e2 where e1.ManagerId = e2.Id and e1.Salary > e2.Salary 

4. 查找重复的电子邮箱(182)

编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。

示例:

±—±--------+
| Id | Email |
±—±--------+
| 1 | a@b.com |
| 2 | c@d.com |
| 3 | a@b.com |
±—±--------+
根据以上输入,你的查询应返回以下结果:

±--------+
| Email |
±--------+
| a@b.com |
±--------+
说明:所有电子邮箱都是小写字母。

解法

思路简单,用groupby分组,然后用count计数,最后用having筛选

select Email from Person group by Email having count(*) > 1

5. 从不订购的客户(183)

某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。

Customers 表:

±—±------+
| Id | Name |
±—±------+
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
±—±------+
Orders 表:

±—±-----------+
| Id | CustomerId |
±—±-----------+
| 1 | 3 |
| 2 | 1 |
±—±-----------+
例如给定上述表格,你的查询应返回:

±----------+
| Customers |
±----------+
| Henry |
| Max |
±----------+

解法

in关键词的使用,使用一个子查询查找出所有在Orders表中出现过的CustomersId即可

# Write your MySQL query statement below
select Name as Customers from Customers 
where id not in (select distinct CustomerId from Orders)

6. 删除重复的电子邮箱(196)

编写一个 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 |
±—±-----------------+

解法

两个表进行自链接,然后只留下id相同的部分,去掉id不同的部分,思路太神奇

# Write your MySQL query statement below
delete p1 from Person as p1, Person as p2 where p1.Email = p2.Email 
and p1.Id > p2.Id

7. 上升的温度(197)

给定一个 Weather 表,编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的 Id。

±--------±-----------------±-----------------+
| Id(INT) | RecordDate(DATE) | Temperature(INT) |
±--------±-----------------±-----------------+
| 1 | 2015-01-01 | 10 |
| 2 | 2015-01-02 | 25 |
| 3 | 2015-01-03 | 20 |
| 4 | 2015-01-04 | 30 |
±--------±-----------------±-----------------+
例如,根据上述给定的 Weather 表格,返回如下 Id:

±—+
| Id |
±—+
| 2 |
| 4 |
±—+

解法

这里要用到mysql的日期处理函数(http://www.cnblogs.com/ggjucheng/p/3352280.html),使用to_days函数将日期转换为整数。

# Write your MySQL query statement below
select w2.Id from Weather w1, Weather w2 
where To_days(w1.RecordDate)+1 = to_days(w2.RecordDate) and w1.Temperature < w2.Temperature

8. 大的国家(595)

这里有张 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 below
select name, population, area from World 
where area > 3000000 or population > 25000000

9. 超过5名学生的课(596)

有一个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 below
select class from courses group by class having count(distinct student) >= 5

10. 有趣的电影(620)

某城市开了一家新的电影院,吸引了很多人过来看电影。该电影院特别注意用户体验,专门有个 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 |
±--------±----------±-------------±----------+

解法

不等于也可以用<>表示

# Write your MySQL query statement below
select * from cinema where (id % 2) = 1 and description != 'boring' order by rating desc

11. 交换工资(627)

给定一个 salary 表,如下所示,有 m = 男性 和 f = 女性 的值。交换所有的 f 和 m 值(例如,将所有 f 值更改为 m,反之亦然)。要求只使用一个更新(Update)语句,并且没有中间的临时表。

注意,您必只能写一个 Update 语句,请不要编写任何 Select 语句。

例如:

idnamesexsalary
1Am2500
2Bf1500
3Cm5500
4Df500

运行你所编写的更新语句之后,将会得到以下表:

idnamesexsalary
1Af2500
2Bm1500
3Cf5500
4Dm500

解法1

用了二进制运算,两个相同字母的二进制异或是0

# Write your MySQL query statement below
update salary set sex = CHAR(ASCII('f') ^ ASCII('m') ^ ASCII(sex));

解法2

用 case then

update salary set sex = case when sex ='m' then 'f' else 'm' end

解法3

如果sex=‘f’,则 IF(sex=‘f’,‘m’,‘f’)表达式的值为’m’,

如果sex=‘m’,则 IF(sex=‘f’,‘m’,‘f’)表达式的值为’f’,

然后更新相应的值

UPDATE salary 
SET sex=IF(sex='f','m','f')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值