leetCode数据查询笔记(简单)

发布时间:2019-08-07 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了leetCode数据查询笔记(简单)脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

说明

LeetCode上的数据库题,涉及到的大多是单表操作,数据量也不大,所以可以作为熟悉SQL这种集合式编程语言的练习。

点击题目可以直接跳转

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      |
+--------------+-------------+--------------+

思路/笔记:这道题太基础了,上面的赞和踩比例是2:8。和命令式编程语言函数式编程语言不同,SQL太像日常使用的英语了。

Fast Lane里面有一句歌词:

CATCH me 
   IN my mercedes

SELECT Fish
  From river

就是一样的格式——但SQL和自然语言又不同,用SQL写代码就需要转换为SQL的思维方式。

select name, population, area
  from World
 where population > 25000000
    or area > 3000000

182. 查找重复的电子邮箱

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

示例:

+----+---------+
| Id | Email   |
+----+---------+
| 1  | a@b.COM |
| 2  | c@d.com |
| 3  | a@b.com |
+----+---------+

根据以上输入,你的查询应返回以下结果:

+---------+
| Email   |
+---------+
| a@b.com |
+---------+

说明:所有电子邮箱都是小写字母。
思路/笔记:这道题也属于基础,where和having的区别是:

  • where作用于单条记录
  • having可以作用于所有记录聚合的结果,比如count,sum,max,avg。

where就像把一块蛋糕切出想要的部分;having就像

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

627. 交换工资

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

例如:

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

思路/笔记:基础题,CASE是一个很强大的功能,很多条件判断都可以用CASE替代WHERE,而穷更加方便。

update salary
   set sex = (CASE sex WHEN 'm' THEN 'f'
                       WHEN 'f' THEN 'm'
              ELSE sex END)

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     |
+---------+-----------+--------------+-----------+

对于上面的例子,则正确的输出是为:

+---------+-----------+--------------+-----------+
|   id    | movie     |  description |  rating   |
+---------+-----------+--------------+-----------+
|   5     | House card|   Interesting|   9.1     |
|   1     | War       |   great 3D   |   8.9     |
+---------+-----------+--------------+-----------+

思路/笔记:基础题

select id, movie, description, rating
  from cinema
 where description <> 'boring'
   and (id%2) = 1
 order by rating desc

175. 组合两个表

表1: Person

+-------------+---------+
| 列名         | 类型     |
+-------------+---------+
| PersonId    | int     |
| FirstName   | vArchar |
| LastName    | VARchar |
+-------------+---------+

PersonId 是上表主键
表2: Address

+-------------+---------+
| 列名         | 类型    |
+-------------+---------+
| AddressId   | int     |
| PersonId    | int     |
| City        | varchar |
| state       | varchar |
+-------------+---------+

AddressId 是上表主键

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

FirstName, LastName, City, State

思路/笔记:基础题,一般来说JOIN都要少用,如果有A、B两表,数据量分别为m、n。那么join的运算量就是m×n,如果再加一张数据量为l的C表,就是m×n×l,@R_957_1304@为O(n^3)。所以最好能够通过好的表结构设计,避免大表的JOIN运算。

另外有一些很长的字符串,比如公司编号、商店编号、收银设备编号、会员卡号等,在每一笔交易里都存放的话,会占据更多的空间,可以通过建立映射表的方式存放,使用INT/NUMBER等存放,既节省了空间,又可以提高计算效率。

select p.FirstName, p.LastName, a.City, a.State
  from Person p 
  left join Address a on p.PersonId = a.PersonId

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      |
+----------+

思路/笔记:基础题,关于这种树状结构表的递归查询有一个语句:select…start with…connect by…PRior

select e1.Name Employee
  from Employee e1,Employee e2
 where e1.ManagerId = e2.Id
   and e1.Salary > e2.Salary

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       |
+-----------+

思路/笔记:基础题,exists和in的区别

select c.Name Customers
  from Customers c
 where not exists(select 1
                    from Orders o
                   where c.Id = o.CustomerId)

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  |
+----+------------------+

思路/笔记:自连接可以用来解决很多同表内数据的比较,这里要注意的是P3不可少,否则会出现You can't specify target table 'Person' for update in FROM clause错误。

  delete
    from Person
   where Id in (select Id
                  from (select p2.Id
                          from Person p1, Person p2
                         where p1.Email = p2.Email
                           and p1.Id < p2.Id)p3)

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 |
+----+

思路/笔记:用自连接的方式,可以找到相邻数据,在oracle里有LAG(),LEAD()函数可以用,但通用的还是自连接。

select w2.Id
  from Weather w1, Weather w2
 where (datediff(w1.RecordDate, w2.RecordDate) = -1)
   and w1.Temperature < w2.Temperature

596. 超过5名学生的课

有一个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    |
+---------+

Note:
学生在每个课中不应被重复计算。
思路/笔记:不能有重复的,所以需要加distinct

select class
  from (select distinct student, class
          from courses) c
 group by class
having count(0) >= 5

176. 第二高的薪水

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

+----+--------+
| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+

例如上述 Employee 表,SQL查询应该返回 200 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 null。

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

思路/笔记MySQL里没有rownum这个值,所以可以通过@rownum赋值,不过mysql里可以直接用select 1这种形式,oracle里则需要select 1 from dual

# select (case Salary when Salary then Salary
#         else '' end) SecondHighestSalary
#   from (select e.Salary, @rownum:=@rownum+1 rownum
#           from Employee e, (select @rownum:=0) r
#          order by e.Salary desc)s
#  where rownum = 2

SELECT
    (SELECT DISTINCT Salary
       FROM Employee
      ORDER BY Salary DESC
      LIMIT 1 OFFSET 1) AS SecondHighestSalary

脚本宝典总结

以上是脚本宝典为你收集整理的leetCode数据查询笔记(简单)全部内容,希望文章能够帮你解决leetCode数据查询笔记(简单)所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。