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

利用查詢(xún)嵌套

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

先利用分組找出每個(gè)部門(mén)最高的工資,將工資和部門(mén)Id存到一個(gè)表中乃秀,然后將三個(gè)表進(jìn)行聯(lián)結(jié)等限。

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

聯(lián)結(jié)的嵌套峦树,使用自聯(lián)結(jié)找出表中同個(gè)部門(mén)最高的三個(gè)工資(利用計(jì)數(shù)函數(shù)在同一表中查找比自己工資高的人數(shù)小于3的人)政供,利用內(nèi)部聯(lián)結(jié)聯(lián)結(jié)表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

使用自聯(lián)結(jié)

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

利用日期函數(shù)對(duì)兩個(gè)日期進(jìn)行計(jì)算投剥。

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;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末迷帜,一起剝皮案震驚了整個(gè)濱河市输吏,隨后出現(xiàn)的幾起案子权旷,更是在濱河造成了極大的恐慌,老刑警劉巖贯溅,帶你破解...
    沈念sama閱讀 216,402評(píng)論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件拄氯,死亡現(xiàn)場(chǎng)離奇詭異躲查,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)译柏,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén)镣煮,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人鄙麦,你說(shuō)我怎么就攤上這事典唇。” “怎么了黔衡?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,483評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵蚓聘,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我盟劫,道長(zhǎng)夜牡,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,165評(píng)論 1 292
  • 正文 為了忘掉前任侣签,我火速辦了婚禮塘装,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘影所。我一直安慰自己蹦肴,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,176評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布猴娩。 她就那樣靜靜地躺著阴幌,像睡著了一般。 火紅的嫁衣襯著肌膚如雪卷中。 梳的紋絲不亂的頭發(fā)上矛双,一...
    開(kāi)封第一講書(shū)人閱讀 51,146評(píng)論 1 297
  • 那天,我揣著相機(jī)與錄音蟆豫,去河邊找鬼议忽。 笑死,一個(gè)胖子當(dāng)著我的面吹牛十减,可吹牛的內(nèi)容都是我干的栈幸。 我是一名探鬼主播,決...
    沈念sama閱讀 40,032評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼帮辟,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼速址!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起织阅,我...
    開(kāi)封第一講書(shū)人閱讀 38,896評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤壳繁,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體闹炉,經(jīng)...
    沈念sama閱讀 45,311評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡蒿赢,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,536評(píng)論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了渣触。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片羡棵。...
    茶點(diǎn)故事閱讀 39,696評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖嗅钻,靈堂內(nèi)的尸體忽然破棺而出皂冰,到底是詐尸還是另有隱情,我是刑警寧澤养篓,帶...
    沈念sama閱讀 35,413評(píng)論 5 343
  • 正文 年R本政府宣布秃流,位于F島的核電站,受9級(jí)特大地震影響柳弄,放射性物質(zhì)發(fā)生泄漏舶胀。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,008評(píng)論 3 325
  • 文/蒙蒙 一碧注、第九天 我趴在偏房一處隱蔽的房頂上張望嚣伐。 院中可真熱鬧,春花似錦萍丐、人聲如沸轩端。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)基茵。三九已至,卻和暖如春壳影,著一層夾襖步出監(jiān)牢的瞬間耿导,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,815評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工态贤, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人醋火。 一個(gè)月前我還...
    沈念sama閱讀 47,698評(píng)論 2 368
  • 正文 我出身青樓悠汽,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親芥驳。 傳聞我的和親對(duì)象是個(gè)殘疾皇子柿冲,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,592評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容