一、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.

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

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

  1. +----+
  2. | Id |
  3. +----+
  4. | 2 |
  5. | 4 |
  6. +----+
    分析:意思就是在Weather表中,写一个SQL查询与前一天相比温度更高的日期对应的ID
    代码:
  1. # Write your MySQL query statement below
  2. SELECT w1.Id
  3. 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开始的天数 ) 

其他解法:

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

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

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

  1. +----+------------------+
  2. | Id | Email |
  3. +----+------------------+
  4. | 1 | john@example.com |
  5. | 2 | bob@example.com |
  6. +----+------------------+

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

代码:

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

 其他解法:

  1. DELETE FROM Person
  2. WHERE Id IN
  3. (SELECT P1.Id FROM Person AS P1, Person AS P2
  4. 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 : 

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

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

  1. +----+-------+
  2. | Id | Name |
  3. +----+-------+
  4. | 1 | Joe |
  5. | 2 | Henry |
  6. | 3 | Sam |
  7. | 4 | Max |
  8. +----+-------+

Table: Orders.

  1. +----+------------+
  2. | Id | CustomerId |
  3. +----+------------+
  4. | 1 | 3 |
  5. | 2 | 1 |
  6. +----+------------+

Using the above tables as example, return the following:

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

 解法二:

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

 解法三:

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

  

  1.  
  1.  

 

 

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

    password 123 A http://acm.hust.edu.cn/vjudge/contest/view.action?cid=96950#problem/A 题意:n个数初始ai,m次操作 ...

  2. <string>和<string.h>的区别

    转自:http://blog.csdn.net/houjixin/article/details/8648969 在C++开发过程中经常会遇到两个比较容易混淆的头文件引用#include<str ...

  3. C/C++中内存区域划分大总结

    C++作为一款C语言的升级版本,具有非常强大的功能.它不但能够支持各种程序设计风格,而且还具有C语言的所有功能.我们在这里为大家介绍的是其中一个比较重要的内容,C和C++内存区域的划分. 一. 在c中 ...

  4. Sqli-labs less 18

    Less-18 本关我们这里从源代码直接了解到 对uname和passwd进行了check_input()函数的处理,所以我们在输入uname和passwd上进行注入是不行的,但是在代码中,我们看到了 ...

  5. node-debug 三法三例之node debugger + node inspector

    大家对nodejs调试应该都比较头疼,至少我这个不用IDE写js的人很头疼这个,其实node的生态圈非常好 有非常好的工具和非常潮的开发方式 这里总结了3法3例,希望能对大家有所帮助 文档地址  ht ...

  6. JavaScript基于对象编程

    js面向对象特征介绍 javascript是一种面向(基于)对象的动态脚本语言,是一种基于对象(Object)和事件驱动(EventDirven)并具有安全性能的脚本语言.它具有面向对象语言所特有的各 ...

  7. 为什么toString方法可以用来区分数组和对象?

    首先大家都应该知道在javascript中只有是对象都存在toString方法,将调用该方法的值转换为字符串返回,如下: var arr = [1, 2, 3]; console.log(arr.to ...

  8. lintcode:接雨水

    接雨水 给出 n 个非负整数,代表一张X轴上每个区域宽度为 1 的海拔图, 计算这个海拔图最多能接住多少(面积)雨水. 如上图所示,海拔分别为 [0,1,0,2,1,0,1,3,2,1,2,1], 返 ...

  9. JTAG的SWD接线方式

    JTAG引脚示意图: 一.SWD 和传统的调试方式区别 1. SWD 模式比 JTAG 在高速模式下面更加可靠 2. GPIO 刚好缺一个的时候, 可以使用 SWD 仿真, 这种模式支持更少的引脚 3 ...

  10. QT 读取文件夹下所有文件(超级简单的方法,不需要QDirIterator)

    之前,用标准C++写过读取文件夹.现在用QT重写代码,顺便看了下QT如何实现,还是相当简单的.主要用到QDir,详细文档可见这里 A program that lists all the files ...