Leetcode Datebase Problem(2)

183. Customers Who Never Order

Problem

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

Answer

利用查询嵌套

select Customers.Name as Customers 
from Customers
where Customers.Id not in(
select CustomerId from Orders);

184. Department Highest Salary

Problem

The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id.

+----+-------+--------+--------------+
| Id |  Name | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1  | Joe   | 70000  |      1       |
| 2  | Henry | 80000  |      2       |
| 3  | Sam   | 60000  |      2       |
| 4  | Max   | 90000  |      1       |
+----+-------+--------+--------------+

The Department table holds all departments of the company.

+----+----------+
| Id |   Name   |
+----+----------+
| 1  |   IT     |
| 2  |   Sales  |
+----+----------+

Write a SQL query to find employees who have the highest salary in each of the departments. For the above tables, Max has the highest salary in the IT department and Henry has the highest salary in the Sales department.

+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
|     IT     |    Max   |  90000 |
|     Sales  |    Henry |  80000 |
+------------+----------+--------+

Answer

先利用分组找出每个部门最高的工资,将工资和部门Id存到一个表中,然后将三个表进行联结。

select Department.Name AS Department,Employee.Name as Employee,tmp.Salary
from Employee,
(select DepartmentId,max(Salary) as Salary 
from Employee
group by DepartmentId) tmp,Department
where Employee.DepartmentId=tmp.DepartmentId
and Employee.Salary=tmp.Salary
and Department.Id=tmp.DepartmentId;

185. Department Top Three Salaries

Problem

The Employee table holds all employees. Every employee has an Id, and there is also a column for the department Id.

+----+-------+--------+--------------+
| Id | Name  | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1  | Joe   | 70000  | 1            |
| 2  | Henry | 80000  | 2            |
| 3  | Sam   | 60000  | 2            |
| 4  | Max   | 90000  | 1            |
| 5  | Janet | 69000  | 1            |
| 6  | Randy | 85000  | 1            |
+----+-------+--------+--------------+

The Department table holds all departments of the company.

+----+----------+
| Id | Name     |
+----+----------+
| 1  | IT       |
| 2  | Sales    |
+----+----------+

Write a SQL query to find employees who earn the top three salaries in each of the department. For the above tables, your SQL query should return the following rows.

+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT         | Max      | 90000  |
| IT         | Randy    | 85000  |
| IT         | Joe      | 70000  |
| Sales      | Henry    | 80000  |
| Sales      | Sam      | 60000  |
+------------+----------+--------+

Answer

联结的嵌套,使用自联结找出表中同个部门最高的三个工资(利用计数函数在同一表中查找比自己工资高的人数小于3的人),利用内部联结联结表Employee和表Department。

select d.Name Department, e1.Name Employee, e1.Salary 
from Employee e1
join Department d on e1.DepartmentId = d.Id
where 3 > (select count(distinct(e2.Salary))
           from Employee e2 
           where e2.Salary > e1.Salary and e1.DepartmentId = e2.DepartmentId );

196. Delete Duplicate Emails

Problem

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

Answer

使用自联结

Delete p1 from Person p1,Person p2 
where p1.Email=p2.Email and p1.Id>p2.Id;

197. Rising Temperature

Problem

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

Answer

利用日期函数对两个日期进行计算。

select w1.Id from Weather w1,Weather w2
where w1.Date=adddate(w2.Date,1) and w1.Temperature>w2.Temperature;

262. Trips and Users

Problem

The Trips table holds all taxi trips. Each trip has a unique Id, while Client_Id and Driver_Id are both foreign keys to the Users_Id at the Users table. Status is an ENUM type of (‘completed’, ‘cancelled_by_driver’, ‘cancelled_by_client’).

+----+-----------+-----------+---------+--------------------+----------+
| Id | Client_Id | Driver_Id | City_Id |       Status       |Request_at|
+----+-----------+-----------+---------+--------------------+----------+
| 1  |     1     |     10    |    1    |     completed      |2013-10-01|
| 2  |     2     |     11    |    1    | cancelled_by_driver|2013-10-01|
| 3  |     3     |     12    |    6    |     completed      |2013-10-01|
| 4  |     4     |     13    |    6    | cancelled_by_client|2013-10-01|
| 5  |     1     |     10    |    1    |     completed      |2013-10-02|
| 6  |     2     |     11    |    6    |     completed      |2013-10-02|
| 7  |     3     |     12    |    6    |     completed      |2013-10-02|
| 8  |     2     |     12    |    12   |     completed      |2013-10-03|
| 9  |     3     |     10    |    12   |     completed      |2013-10-03|
| 10 |     4     |     13    |    12   | cancelled_by_driver|2013-10-03|
+----+-----------+-----------+---------+--------------------+----------+

The Users table holds all users. Each user has an unique Users_Id, and Role is an ENUM type of (‘client’, ‘driver’, ‘partner’).

+----------+--------+--------+
| Users_Id | Banned |  Role  |
+----------+--------+--------+
|    1     |   No   | client |
|    2     |   Yes  | client |
|    3     |   No   | client |
|    4     |   No   | client |
|    10    |   No   | driver |
|    11    |   No   | driver |
|    12    |   No   | driver |
|    13    |   No   | driver |
+----------+--------+--------+

Write a SQL query to find the cancellation rate of requests made by unbanned clients between Oct 1, 2013 and Oct 3, 2013. For the above tables, your SQL query should return the following rows with the cancellation rate being rounded to two decimal places.

+------------+-------------------+
|    Day     | Cancellation Rate |
+------------+-------------------+
| 2013-10-01 |        0.33       |
| 2013-10-02 |        0.00       |
| 2013-10-03 |        0.50       |
+------------+-------------------+

Answer

select Request_at as Day,
round(count(Status!='completed' or NULL)/count(*),2) as 'Cancellation Rate'
from Trips
inner join Users
on Trips.Client_Id=Users.Users_Id and Banned='No'
and Request_at between '2013-10-01' and '2013-10-03'
group by Request_at;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,547评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,399评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,428评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,599评论 1 274
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,612评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,577评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,941评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,603评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,852评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,605评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,693评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,375评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,955评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,936评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,172评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,970评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,414评论 2 342

推荐阅读更多精彩内容