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,防止出错。

你可能感兴趣的:(2020-08-23 easy 12-18 类比ing)