一、Rising Temperature

Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to its previous (yesterday's) dates.

+---------+------------+------------------+
| Id(INT) | Date(DATE) | Temperature(INT) |
+---------+------------+------------------+
| 1 | 2015-01-01 | 10 |
| 2 | 2015-01-02 | 25 |
| 3 | 2015-01-03 | 20 |
| 4 | 2015-01-04 | 30 |
+---------+------------+------------------+

For example, return the following Ids for the above Weather table:

+----+
| Id |
+----+
| 2 |
| 4 |
+----+
分析:意思就是在Weather表中,写一个SQL查询与前一天相比温度更高的日期对应的ID。
代码:
# Write your MySQL query statement below
SELECT w1.Id
FROM Weather w1 JOIN Weather w2 ON TO_DAYS(w1.Date)=TO_DAYS(w2.Date)+1 And w1.Temperature>w2.Temperature;

 其中,TO_DAYS(date) 给定一个日期date, 返回一个天数 (从年份0开始的天数 ) 

其他解法:

SELECT w1.Id FROM Weather w1, Weather w2 WHERE dateDiff(w1.Date,w2.Date) = 1 AND w1.Temperature > w2.Temperature;

  其中,dateDiff() 函数返回两个日期之间的天数。

还有这样的方式:

date_add(w1.date,interval 1 day)=w2.date

w2.Date = DATE_SUB(w1.Date, INTERVAL 1 DAY)

二、Delete Duplicate Emails

Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id.

+----+------------------+
| Id | Email |
+----+------------------+
| 1 | john@example.com |
| 2 | bob@example.com |
| 3 | john@example.com |
+----+------------------+
Id is the primary key column for this table.

For example, after running your query, the above Person table should have the following rows:

+----+------------------+
| Id | Email |
+----+------------------+
| 1 | john@example.com |
| 2 | bob@example.com |
+----+------------------+

分析:意思就是删除Email列中重复项所在的行,而且保留的不重复行Id更小。

代码:

# Write your MySQL query statement below
DELETE p1
FROM Person p1, Person p2
WHERE p1.Email = p2.Email AND p1.Id > p2.Id

 其他解法:

DELETE FROM Person
WHERE Id IN
(SELECT P1.Id FROM Person AS P1, Person AS P2
WHERE P1.Id > P2.Id AND P1.Email = P2.Email);

报错:Runtime Error Message:You can't specify target table 'Person' for update in FROM clause

 所以得注意:In mysql you must't update a table while using select clause , You can only do that step by step . However ,you can use a middle table as : 

delete from Person where id not in( select t.id from ( select min(id) as id from Person group by email ) t )

 或:

MySQL Don't allow referring delete target table in sub query, a workaround is use ( select * from Person ) to get a new table.

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

另外ps:刚开始也想过用"SELECT DISTINCT Email from Person" ,但是注意到:Delete and Distinct are completely different, while delete alters the table, distinct only selects distinct values and doesn't alter table. 所以这样是不可行的!

三、Customers Who Never Order

Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL query to find all customers who never order anything.

Table: Customers.

+----+-------+
| Id | Name |
+----+-------+
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
+----+-------+

Table: Orders.

+----+------------+
| Id | CustomerId |
+----+------------+
| 1 | 3 |
| 2 | 1 |
+----+------------+

Using the above tables as example, return the following:

+-----------+
| Customers |
+-----------+
| Henry |
| Max |
+-----------+
分析:题意为 假设一个网站包含两个表, 顾客表Customers和订单表Orders。编写一个SQL查询找出所有从未下过订单的顾客。
思路:使用NOT IN,NOT EXISTS,或者LEFT JOIN都是可以解决的。
解法一:
NOT IN
# Write your MySQL query statement below
SELECT Name
FROM Customers C WHERE C.Id not in (select O.CustomerId from Orders O);

 解法二:

NOT EXISTS
# Write your MySQL query statement below
SELECT Name FROM Customers c WHERE NOT EXISTS (SELECT CustomerId FROM Orders o WHERE o.CustomerId = c.id);

 解法三:

SELECT C.Name
FROM Customers AS C LEFT OUTER JOIN Orders AS O
ON C.Id = O.CustomerId
WHERE O.CustomerId IS NULL;

  

 
 

 

 

leetcode Database1(三)的更多相关文章

  1. leetcode第三题

    leetcode第三题: 题目: 给定一个字符串,找出不含有重复字符的最长子串的长度. 源码(使用java语言): class Solution { public int lengthOfLonges ...

  2. LeetCode 628. 三个数的最大乘积

    题目描述 LeetCode 628. 三个数的最大乘积 给定一个整型数组,在数组中找出由三个数组成的最大乘积,并输出这个乘积. 示例1 输入: [1,2,3] 输出: 6 示例2 输入: [1,2,3 ...

  3. LeetCode:三个数的最大乘积【628】

    LeetCode:三个数的最大乘积[628] 题目描述 给定一个整型数组,在数组中找出由三个数组成的最大乘积,并输出这个乘积. 示例 1: 输入: [1,2,3] 输出: 6 示例 2: 输入: [1 ...

  4. LeetCode:三数之和【15】

    LeetCode:三数之和[15] 题目描述 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的 ...

  5. [LeetCode] 3Sum 三数之和

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  6. Maximal Rectangle [leetcode] 的三种思路

    第一种方法是利用DP.时间复杂度是 O(m * m * n) dp(i,j):矩阵中同一行以(i,j)结尾的所有为1的最长子串长度 代码例如以下: int maximalRectangle(vecto ...

  7. 【LeetCode】三数之和【排序,固定一个数,然后双指针寻找另外两个数】

    给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...

  8. LeetCode 15. 三数之和(3Sum)

    15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...

  9. Java实现 LeetCode 15 三数之和

    15. 三数之和 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以 ...

随机推荐

  1. mongo二维数组操作

    有2个嵌套的数组: 如果我想查询comments里score大于5的记录: testProvider.find({"comments.score":{"$gt" ...

  2. 对话框Dialog

    QMainWindow QMainWindow是 Qt 框架带来的一个预定义好的主窗口类. 主窗口,就是一个普通意义上的应用程序(不是指游戏之类的那种)最顶层的窗口.通常是由一个标题栏,一个菜单栏,若 ...

  3. c3p0 --2

    c3p0号称是java界最好的数据池. c3p0的配置方式分为三种,分别是 1.setters一个个地设置各个配置项 2.类路径下提供一个c3p0.properties文件 3.类路径下提供一个c3p ...

  4. poj 2311

    Cutting Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2844   Accepted: 1036 Desc ...

  5. iOS-CALayer图片淡入淡出动画

    ]; } - (.f;          CABasicAnimation *boundsAnimation = [CABasicAnimation animationWithKeyPath:, ,  ...

  6. Linux下搭建Android NDK , Linux 驱动开发环境

    Eclispe Luna(4.4):http://www.eclipse.org/downloads/ CDT :http://www.eclipse.org/cdt/downloads.php AD ...

  7. HDU 1698 Just a Hook (线段树区间更新)

    题目链接 题意 : 一个有n段长的金属棍,开始都涂上铜,分段涂成别的,金的值是3,银的值是2,铜的值是1,然后问你最后这n段总共的值是多少. 思路 : 线段树的区间更新.可以理解为线段树成段更新的模板 ...

  8. JavaScript中函数的形参和实参的实现原理剖析

    我们都知道JS里面参数的传递是可以不一样的,比如我们有一个函数: <script type="text/javascript"> function one(a,b,c) ...

  9. 【poj1006-biorhythms】中国剩余定理

    http://poj.org/problem?id=1006 题意:中国剩余定理的裸题. 题目可转化为求最小的x满足以下条件: x%23=a;x%28=b;x%33=c; 关于中国剩余定理可看我昨天的 ...

  10. jquery的ajax()函数传值中文乱码解决方法介绍

    jquery的ajax()函数传值中文乱码解决方法介绍,需要的朋友可以参考下 代码如下: $.ajax({ dataType : ‘json', type : ‘POST', url : ‘http: ...