2020-08-23 easy 12-18 类比ing

12. Friend Requests I: Overall Acceptance Rate

In social network like Facebook or Twitter, people send friend requests and accept others’ requests as well. Now given two tables as below:

Table: friend_request

| sender_id | send_to_id |request_date|

|-----------|------------|------------|

| 1        | 2          | 2016_06-01 |

| 1        | 3          | 2016_06-01 |

| 1        | 4          | 2016_06-01 |

| 2        | 3          | 2016_06-02 |

| 3        | 4          | 2016-06-09 |


Table: request_accepted

| requester_id | accepter_id |accept_date |

|--------------|-------------|------------|

| 1            | 2          | 2016_06-03 |

| 1            | 3          | 2016-06-08 |

| 2            | 3          | 2016-06-08 |

| 3            | 4          | 2016-06-09 |

| 3            | 4          | 2016-06-10 |

Write a query to find the overall acceptance rate of requests rounded to 2 decimals, which is the number of acceptance divide the number of requests.

Note:

The accepted requests are not necessarily from the table friend_request. In this case, you just need to simply count the total accepted requests (no matter whether they are in the original requests), and divide it by the number of requests to get the acceptance rate.It is possible that a sender sends multiple requests to the same receiver, and a request could be accepted more than once. In this case, the ‘duplicated’ requests or acceptances are only counted once.If there is no requests at all, you should return 0.00 as the accept_rate.


SELECT

ROUND(IFNULL((SELECT COUNT(accepter_id)

FROM

(SELECT accepter_id

FROM request_accepted

GROUP BY requester_id, accepter_id) AS a)

/

(SELECT COUNT(sender_id)

FROM

(SELECT sender_id

FROM friend_request

GROUP BY sender_id, send_to_id) AS B), 0), 2) AS accept_rate

;


这道题要注意,当使用完group by以后,用count时,count的是每个group里面的column,而不是全体的count。还有就是两个子查询相除的时候,分子和分母都要带上括号。



13. Find the Team Size

Table: Employee

+---------------+---------+

| Column Name  | Type    |

+---------------+---------+

| employee_id  | int    |

| team_id      | int    |

+---------------+---------+

employee_id is the primary key for this table.

Each row of this table contains the ID of each employee and their respective team.

Write an SQL query to find the team size of each of the employees.

Return result table in any order.


SELECT b.employee_id, a.team_size

FROM

(SELECT team_id, COUNT(employee_id) AS team_size

FROM employee

GROUP BY team_id) AS a

RIGHT JOIN employee AS b

ON a.team_id = b.team_id

;


14. Triangle Judgement

A pupil Tim gets homework to identify whether three line segments could possibly form a triangle. However, this assignment is very heavy because there are hundreds of records to calculate.Could you help Tim by writing a query to judge whether these three sides can form a triangle, assuming table triangle holds the length of the three sides x, y and z.

| x  | y  | z  |

|----|----|----|

| 13 | 15 | 30 |

| 10 | 20 | 15 |


SELECT

x, y, z,

CASE WHEN x+y>z AND x+z>y AND y+z>x THEN 'Yes' ELSE 'No' END AS triangle

FROM triangle

;


15.  Swap Salary(该题型属于update,很少考)

16. Immediate Food Delivery I

Table: Delivery

+-----------------------------+---------+

| Column Name                | Type    |

+-----------------------------+---------+

| delivery_id                | int    |

| customer_id                | int    |

| order_date                  | date    |

| customer_pref_delivery_date | date    |

+-----------------------------+---------+

delivery_id is the primary key of this table.

The table holds information about food delivery to customers that make orders at some date and specify a preferred delivery date (on the same order date or after it).

If the preferred delivery date of the customer is the same as the order date then the order is called immediate otherwise it's called scheduled.

Write an SQL query to find the percentage of immediate orders in the table,rounded to 2 decimal places.

SELECT

ROUND(IFNULL((SELECT COUNT(delivery_id)

FROM delivery

WHERE order_date = customer_pref_delivery_date)

/  (SELECT COUNT(delivery_id) FROM delivery)*100, 0), 2) AS immediate_percentage

;

这道题需要注意,在求百分比的时候,最好直接把结果乘100。


17. List the Products Ordered in a Period

Table: Products

+------------------+---------+

| Column Name      | Type    |

+------------------+---------+

| product_id      | int    |

| product_name    | varchar |

| product_category | varchar |

+------------------+---------+

product_id is the primary key for this table.

This table contains data about the company's products.

Table: Orders

+---------------+---------+

| Column Name  | Type    |

+---------------+---------+

| product_id    | int    |

| order_date    | date    |

| unit          | int    |

+---------------+---------+

There is no primary key for this table. It may have duplicate rows. product_id is a foreign key to Products table. unit is the number of products ordered in order_date.

Write an SQL query to get the names of products with greater than or equal to 100 units ordered in February 2020 and their amount. Return result table in any order.


SELECT b.product_name, a.unit

FROM

(SELECT product_id, SUM(unit) AS unit

FROM orders

WHERE order_date LIKE '%2020-02%'

GROUP BY product_id) AS a

LEFT JOIN products AS b

ON a.product_id = b.product_id

HAVING unit >= 100

;

有的时候我们会很容易忘记最后的限制条件,记住要加上。


18. Sales Analysis II

Table:Product

+--------------+---------+

| Column Name  | Type    |

+--------------+---------+

| product_id  | int    |

| product_name | varchar |

| unit_price  | int    |

+--------------+---------+

product_id is the primary key of this table.

Table:Sales

+-------------+---------+

| Column Name | Type    |

+-------------+---------+

| seller_id  | int    |

| product_id  | int    |

| buyer_id    | int    |

| sale_date  | date    |

| quantity    | int    |

| price      | int    |

+------ ------+---------+

This table has no primary key, it can have repeated rows, product_id is a foreign key to Product table.

Write an SQL query that reports the buyers who have bought S8 but not iPhone. Note that S8and iPhone are products present in the Product table.


WITH temp AS 

(SELECT a.buyer_id, GROUP_CONCAT(a.product_name) AS product

FROM

(SELECT b.product_name, a.buyer_id

FROM sales AS a

LEFT JOIN product AS b

ON a.product_id = b.product_id) AS a

GROUP BY a.buyer_id)

SELECT DISTINCT buyer_id FROM temp WHERE product LIKE '%S8%' AND product NOT LIKE '%iPhone%'

;

在这个solution中,需要注意的是,如果最后只输出一个column的话,最好是加上distinct,防止出错。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,761评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,953评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,998评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,248评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,130评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,145评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,550评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,236评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,510评论 1 291
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,601评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,376评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,247评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,613评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,911评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,191评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,532评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,739评论 2 335