Customers Who Never Order. check 1point3acres for more.
Write an SQL query to report all customers who never order anything.
Return the result table in any order.
Input: . 1point 3 acres
Customers table:
+----+-------+
| id | name |
+----+-------+
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
+----+-------+
Orders table:. Waral dи,
+----+------------+
| id | customerId |. ----
+----+------------+
| 1 | 3 |
| 2 | 1 |
+----+------------+. 1point3acres
Output:
+-----------+
| Customers |
+-----------+
| Henry |
| Max |
+-----------+
Solution:
select
name as Customers
from Customers.1point3acres
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
group by a.id, a.name
)
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.
. check 1point3acres for more.Input:
Employee table: .google и+----+-------+--------+--------------+
| id | name | salary | departmentId |
+----+-------+--------+--------------+. 1point 3 acres
| 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 |
+------------+----------+--------+
| IT | Max | 90000 |
| IT | Joe | 85000 |
| IT | Randy | 85000 |.google и
| IT | Will | 70000 |
| Sales | Henry | 80000 |
| Sales | Sam | 60000 |
+------------+----------+--------+. 1point 3acres
Solution:
with cte as (
select
b.name as Department,
a.name as Employee,.
a.salary as Salary,. Waral dи,
dense_rank() over(partition by a.departmentId order by a.salary desc) as r
from Employee a
left join Department b. check 1point3acres for more.
on a.departmentId = b.id
)
select
Department,
Employee,
Salary
from cte
where r<=3
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.
• 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 |
+-----------+------------+--------------+. check 1point3acres for more.
| 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 |. Waral dи,
| 3 | 4 | 2016/06/09 |
| 3 | 4 | 2016/06/10 |. .и
+--------------+-------------+-------------+
Output:
+-------------+
| accept_rate |.
+-------------+
| 0.8 |
+-------------+
Solution:
with
x as (. 1point 3acres
select
count(distinct requester_id, accepter_id) as x_cnt
from RequestAccepted
),
y as (. check 1point3acres for more.
select
count(distinct sender_id, send_to_id) as y_cnt. 1point 3acres
from FriendRequest
)
select.1point3acres
round(coalesce(x_cnt / (select y_cnt from y), 0.00), 2) as accept_rate. From 1point 3acres bbs
from x
. 1point3acres.com 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 |
+----+--------+-------------+-----------+-------+-----------+ . 1point3acres.com Output: . 1point3acres.com
+------------+
| 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-baidu 1point3acres
from SurveyLog
group by question_id
). 1point3acres
select
question_id as survey_log ..
from cte
order by answer_rate desc, question_id asc.--
limit 1
.1point3acres Median Employee Salary
Input: . Χ
Employee table:
+----+---------+--------+
| id | company | salary |. 1point3acres
+----+---------+--------+. 1point3acres.com
| 1 | A | 2341 |. 1point 3 acres
| 2 | A | 341 |
| 3 | A | 15 |
| 4 | A | 15314 |
| 5 | A | 451 |
| 6 | A | 513 |
| 7 | B | 15 |.--
| 8 | B | 13 |
| 9 | B | 1154 |
| 10 | B | 1345 |
| 11 | B | 1221 |
| 12 | B | 234 |
| 13 | C | 2345 |.--
| 14 | C | 2645 |.--
| 15 | C | 2645 |
| 16 | C | 2652 |. From 1point 3acres bbs
| 17 | C | 65 |
+----+---------+--------+
Output:
+----+---------+--------+. 1point3acres.com
| id | company | salary |.1point3acres
+----+---------+--------+
| 5 | A | 451 | ..
| 6 | A | 513 |
| 12 | B | 234 |
| 9 | B | 1154 |
| 14 | C | 2645 |
+----+---------+--------+. check 1point3acres for more.
Solution:
with temp as .--(
select id, company, salary,
rank() over (partition by company order by salary asc) as lowrank,.1point3acres
rank() over (partition by company order by salary desc) as highrank
from Employee
group by 1,2
). check 1point3acres for more.
select id, company, salary.--
from temp
where lowrank = highrank+1 or highrank=lowrank+1 or lowrank=highrank
group by 2,3. check 1point3acres for more.
order by 2,3
Who Has the Most Friends
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.
Input:
RequestAccepted table:. 1point 3acres
+--------------+-------------+-------------+
| requester_id | accepter_id | accept_date |
+--------------+-------------+-------------+. Waral dи,
| 1 | 2 | 2016/06/03 |
| 1 | 3 | 2016/06/08 |
| 2 | 3 | 2016/06/08 | .--| 3 | 4 | 2016/06/09 |
+--------------+-------------+-------------+
Output:
+----+-----+
| id | num |
+----+-----+
| 3 | 3 |. ----
+----+-----+
Solution:
with cte as (
select
requester_id as id,
accepter_id as friend_id
from RequestAccepted. From 1point 3acres bbs
union
select
accepter_id as id,
requester_id as friend_id.1point3acres
from RequestAccepted
). 1point3acres.com
select
id,
count(distinct friend_id) as num-baidu 1point3acres
from cte
group by id. Waral dи,
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.
A second-degree follower is a user who: ..
• follows at least one user, and-baidu 1point3acres
• is followed by at least one user.. 1point 3acres
.1point3acres
Input: . 1point 3 acres
Follow table:
+----------+----------+.
| followee | follower |
+----------+----------+
| Alice | Bob |. 1point3acres.com
| Bob | Cena |
| Bob | Donald |
| Donald | Edward |
+----------+----------+ ..
Output:
+----------+-----+
| follower | num |.--
+----------+-----+
| Bob | 2 |. Χ
| Donald | 1 |.google и
+----------+-----+
Solution:
select
a.follower,
count(distinct b.follower) as num
from Follow a. 1point3acres.com
left join Follow b
on a.follower=b.followee
group by a.follower
having num>0
order by a.follower asc
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.