查看: 1605| 回复: 0
跳转到指定楼层
上一主题 下一主题
收起左侧

求米,分享买它DS面试中几个SQL常见题目

 
全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
Customers Who Never Order
Write an SQL query to report all customers who never order anything.
Return the result table in any order..
Input:
Customers table:
+----+-------+
| id | name  |
+----+-------+
| 1  | Joe   |
| 2  | Henry | ..
| 3  | Sam   |
| 4  | Max   |
+----+-------+
Orders table:
+----+------------+
| id | customerId |
+----+------------+.
| 1  | 3          |
| 2  | 1          |
+----+------------+
Output:
+-----------+
| Customers |
+-----------+. 1point 3 acres
| Henry     |
| Max       |
+-----------+
Solution:
select
    name as Customers
from Customers
where id not in (select customerId from Orders)
Solution 2:
with cte as (
select
    a.name,
    sum(case when b.id is null then 0 else 1 end) as total_order
from Customers a
left join Orders b
on a.id=b.customerId. Waral dи,
group by a.id, a.name. 1point 3acres
). 1point3acres
select
    name as Customers
from cte
where total_order=0

Department Top Three Salaries
A company's executives are interested in seeing who earns the most money in each of the company's departments. A high earner in a department is an employee who has a salary in the top three unique salaries for that department. ..

Input:
Employee table:
+----+-------+--------+--------------+
| id | name  | salary | departmentId |
+----+-------+--------+--------------+
| 1  | Joe   | 85000  | 1            |
| 2  | Henry | 80000  | 2            |
| 3  | Sam   | 60000  | 2            |
| 4  | Max   | 90000  | 1            |
| 5  | Janet | 69000  | 1            |
| 6  | Randy | 85000  | 1            |
| 7  | Will  | 70000  | 1            |
+----+-------+--------+--------------+
Department table:
+----+-------+
| id | name  |
+----+-------+
| 1  | IT    |
| 2  | Sales |
. ----+----+-------+
Output:
+------------+----------+--------+
| Department | Employee | Salary |.1point3acres
+------------+----------+--------+
| IT         | Max      | 90000  |
| IT         | Joe      | 85000  |
| IT         | Randy    | 85000  |
| IT         | Will     | 70000  |
| Sales      | Henry    | 80000  |
| Sales      | Sam      | 60000  |
+------------+----------+--------+.--
Solution:
with cte as (
select
    b.name as Department,
    a.name as Employee,
    a.salary as Salary,
    dense_rank() over(partition by a.departmentId order by a.salary desc) as r
from Employee a
left join Department b
on a.departmentId = b.id
)
select
    Department,
    Employee,
    Salary
from cte
where r<=3
.google  и
Overall Acceptance Rate
•        The accepted requests are not necessarily from the table friend_request. In this case, 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..google  и
•        If there are no requests at all, you should return 0.00 as the accept_rate.
FriendRequest table:
There is no primary key for this table, it may contain duplicates.
+-----------+------------+--------------+
| sender_id  | send_to_id | request_date |
+-----------+------------+--------------+.google  и
|       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   |
+-----------+------------+--------------+
RequestAccepted table:
There is no primary key for this table, it may contain duplicates.
+--------------+-------------+-------------+
| 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  |-baidu 1point3acres
+--------------+-------------+-------------+
Output:
+-------------+-baidu 1point3acres
| accept_rate |
+-------------+
|       0.8         |. 1point3acres.com
+-------------+
Solution:
with
x as (
select
count(distinct requester_id, accepter_id) as x_cnt-baidu 1point3acres
from RequestAccepted
),
y as (
select
count(distinct sender_id, send_to_id) as y_cnt
from FriendRequest. check 1point3acres for more.
)
select
round(coalesce(x_cnt / (select y_cnt from y), 0.00), 2) as accept_rate
from x. ----
..
Get Highest Answer Rate Question
The answer rate for a question is the number of times a user answered the question by the number of times a user showed the question.
Write an SQL query to report the question that has the highest answer rate. If multiple questions have the same maximum answer rate, report the question with the smallest question_id.

Input:
SurveyLog table:
There is no primary key for this table. It may contain duplicates.
action is an ENUM of the type: "show", "answer", or "skip".
+----+--------+-------------+-----------+-------+-----------+
| id | action | question_id | answer_id | q_num | timestamp |
+----+--------+-------------+-----------+-------+-----------+
| 5  | show   | 285         | null      | 1     | 123       |
| 5  | answer | 285         | 124124    | 1     | 124       |
| 5  | show   | 369         | null      | 2     | 125       |
| 5  | skip   | 369         | null      | 2     | 126       |
+----+--------+-------------+-----------+-------+-----------+
Output:
+------------+
| survey_log |
+------------+
| 285        |. Χ
+------------+
Solution:
with cte as (
select
    question_id,
    sum(case when action='answer' then 1 else 0 end) / sum(case when action='show' then 1 else 0 end) as answer_rate
from SurveyLog
group by question_id
)
select
. ----    question_id as survey_log. Χ
from cte
order by answer_rate desc, question_id asc
limit 1

Median Employee Salary
Input:
Employee table:. ----
+----+---------+--------+
| id | company | salary |
+----+---------+--------+
| 1  | A       | 2341   |
| 2  | A       | 341    |
| 3  | A       | 15     |
| 4  | A       | 15314  |.
| 5  | A       | 451    |. From 1point 3acres bbs
| 6  | A       | 513    |
| 7  | B       | 15     |
| 8  | B       | 13     |
| 9  | B       | 1154   |
.google  и| 10 | B       | 1345   |. From 1point 3acres bbs
| 11 | B       | 1221   |
| 12 | B       | 234    |
| 13 | C       | 2345   |
| 14 | C       | 2645   |
| 15 | C       | 2645   |
| 16 | C       | 2652   |. 1point3acres.com
| 17 | C       | 65     |
+----+---------+--------+
Output:
+----+---------+--------+.google  и
| id | company | salary |.--
+----+---------+--------+
| 5  | A       | 451    |
| 6  | A       | 513    |
| 12 | B       | 234    |
| 9  | B       | 1154   |. ----
| 14 | C       | 2645   |
+----+---------+--------+
Solution:
with temp as. From 1point 3acres bbs
(
select id, company, salary,. Waral dи,
rank() over (partition by company order by salary asc) as lowrank,
rank() over (partition by company order by salary desc) as highrank.--
from Employee.
group by 1,2
)
select id, company, salary
from temp
where lowrank = highrank+1 or highrank=lowrank+1 or lowrank=highrank
group by 2,3
order by 2,3

Who Has the Most Friends. 1point 3acres
Write an SQL query to find the people who have the most friends and the most friends number.
The test cases are generated so that only one person has the most friends.. 1point3acres
Input:
RequestAccepted table:
+--------------+-------------+-------------+
| requester_id | accepter_id | accept_date |
+--------------+-------------+-------------+. check 1point3acres for more.
| 1            | 2           | 2016/06/03  |
| 1            | 3           | 2016/06/08  |. .и
| 2            | 3           | 2016/06/08  |
| 3            | 4           | 2016/06/09  |
+--------------+-------------+-------------+
Output:
+----+-----+.google  и
| id | num |
+----+-----+
| 3  | 3   |
+----+-----+
Solution:
with cte as (
select
    requester_id as id,
    accepter_id as friend_id
from RequestAccepted
union
select
    accepter_id as id,
    requester_id as friend_id
from RequestAccepted
)
select
    id,
    count(distinct friend_id) as num
from cte.google  и
group by id
order by num desc
limit 1. Χ

Second Degree Follower
Write an SQL query to report the second-degree users and the number of their followers.. check 1point3acres for more.
A second-degree follower is a user who:
•        follows at least one user, and
•        is followed by at least one user.

Input:
Follow table:. From 1point 3acres bbs
+----------+----------+
| followee | follower |.1point3acres
+----------+----------+
| Alice    | Bob      |
| Bob      | Cena     |
| Bob      | Donald   |
| Donald   | Edward   |
+----------+----------+
Output: . From 1point 3acres bbs
+----------+-----+
| follower | num |-baidu 1point3acres
+----------+-----+
| Bob      | 2   |
| Donald   | 1   |
+----------+-----+
Solution:
select
    a.follower,. check 1point3acres for more.
    count(distinct b.follower) as num
from Follow a
left join Follow b
on a.follower=b.followee
group by a.follower
having num>0
order by a.follower asc

Reported Posts

Write an SQL query that reports the number of posts reported yesterday for each report reason. Assume today is 2019-07-05
Actions table:
+---------+---------+-------------+--------+--------+
| user_id | post_id | action_date | action | extra  |
+---------+---------+-------------+--------+--------+.
| 1       | 1       | 2019-07-01  | view   | null   |
| 1       | 1       | 2019-07-01  | like   | null   |
| 1       | 1       | 2019-07-01  | share  | null   |
| 2       | 4       | 2019-07-04  | view   | null   |
| 2       | 4       | 2019-07-04  | report | spam   |
| 3       | 4       | 2019-07-04  | view   | null   |
| 3       | 4       | 2019-07-04  | report | spam   |
| 4       | 3       | 2019-07-02  | view   | null   |
| 4       | 3       | 2019-07-02  | report | spam   |
| 5       | 2       | 2019-07-04  | view   | null   |
| 5       | 2       | 2019-07-04  | report | racism |
| 5       | 5       | 2019-07-04  | view   | null   |
| 5       | 5       | 2019-07-04  | report | racism |
+---------+---------+-------------+--------+--------+
Output:
+---------------+--------------+.1point3acres
| report_reason | report_count |
+---------------+--------------+
| spam          | 1            |
| racism        | 2            |
+---------------+--------------+
Solution:
select-baidu 1point3acres
    extra as report_reason,
    count(distinct post_id) as report_count
from Actions
where action='report' and action_date='2019-07-04'
group by extra
having report_count>0

Reported Posts II
Write an SQL query to find the average daily percentage of posts that got removed after being reported as spam, rounded to 2 decimal places.

Input:
Actions table:
+---------+---------+-------------+--------+--------+
| user_id | post_id | action_date | action | extra  |
+---------+---------+-------------+--------+--------+-baidu 1point3acres
| 1       | 1       | 2019-07-01  | view   | null   |
| 1       | 1       | 2019-07-01  | like   | null   |
. Χ| 1       | 1       | 2019-07-01  | share  | null   |
| 2       | 2       | 2019-07-04  | view   | null   | ..
| 2       | 2       | 2019-07-04  | report | spam   |
| 3       | 4       | 2019-07-04  | view   | null   |
| 3       | 4       | 2019-07-04  | report | spam   |
| 4       | 3       | 2019-07-02  | view   | null   |
| 4       | 3       | 2019-07-02  | report | spam   |
| 5       | 2       | 2019-07-03  | view   | null   |
| 5       | 2       | 2019-07-03  | report | racism |
| 5       | 5       | 2019-07-03  | view   | null   |-baidu 1point3acres
| 5       | 5       | 2019-07-03  | report | racism |
+---------+---------+-------------+--------+--------+
Removals table:
+---------+-------------+
| post_id | remove_date |
+---------+-------------+
| 2       | 2019-07-20  |
| 3       | 2019-07-18  |
+---------+-------------+
Output:
+-----------------------+
| average_daily_percent |
+-----------------------+. Waral dи,
| 75.00                 |
+-----------------------+
with cte as (
select
    a.post_id,
    a.action_date,
    a.extra,
    b.post_id as remove_id
from Actions a
left join Removals b
on a.post_id = b.post_id
where a.action='report' and a.extra='spam'
),
cte2 as (. 1point3acres.com
select
    count(distinct remove_id) / count(distinct post_id) as daily_percent
. 1point3acres.com from cte. 1point 3acres
group by action_date.1point3acres
). Waral dи,
select
    round(avg(daily_percent)*100, 2) as average_daily_percent
from cte2

评分

参与人数 6大米 +6 收起 理由
xhao297 + 1 赞一个
sayredrive + 1 很有用的信息!
Florence0909 + 1 赞一个
brtt13 + 1 赞一个
眼袋 + 1 赞一个!

查看全部评分


上一篇:统计常见问题
下一篇:有偿求SQL Python tutor

本帖被以下淘专辑推荐:

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表