Table of contents
1. Managers with at least five direct reports (570)
3. Looking for user referrals (584)
1. Managers with at least five direct reports (570)
surface: Employee
+-------------+---------+ | Column Name | Type | +-------------+---- -----+ | id | int | | name | varchar | | department | varchar | | managerId | int | +-------------+--------- + In SQL, id is the primary key column of the table. Each row of this table represents the employee's name, their department, and the id of their manager. If managerId is empty, the employee has no manager. No employee becomes his own manager.
Query managers with at least 5 direct reports .
Return the result table in any order .
Method one (in)
Find at least five IDs of the same boss first, and then in.
# Write your MySQL query statement below
select name from employee where id in
(select managerId from employee
group by managerId having count(*)>=5 )
Method 2 (join)
First find out the satisfactory manager id, and then join the connection, which is faster than method one.
# Write your MySQL query statement below
select name from employee e join
(select managerId from employee
group by managerId having count(*)>=5) temp
on e.id=temp.managerId
Method 3 (row_number)
Use row_number to sort the same boss, then filter out the boss id, and then self-connect. The method is slow and not recommended.
# # Write your MySQL query statement below
select name from employee a join
(select managerId from
(select managerId,(ROW_NUMBER() over (partition by managerId order by id)) num
from employee ) e
group by e.managerId having max(e.num)>=5)
b on a.id=b.managerId
Method 4 (self-join)
Self-connection, the idea is very good, the second method and this method are recommended.
SELECT
e1. NAME
FROM
Employee AS e1
JOIN Employee AS e2 ON (e1.id = e2.managerid)
GROUP BY
e1.id
HAVING
count(e1.id) >= 5
2. Staff bonus(577)
Select the name and bonus of all employees with bonus < 1000.
Employee
form
+-------+--------+-----------+--------+ | empId | name | supervisor| salary | +- ------+--------+-----------+--------+ | 1 | John | 3 | 1000 | | 2 | | 3 | 2000 | | 3 | Brad | null | 4000 | | 4 | Thomas | 3 | 4000 | +-------+-------+--------- --+-------+ empId is the primary key of this form
Bonus
form
+-------+-------+ | empId | bonus | +-------+-------+ | 2 | 500 | | 4 | 2000 | + -------+-------+ empId is the primary key of this form
Example output:
+-------+-------+ | name | bonus | +-------+-------+ | John | null | | Dan | 500 | | Brad | null | +-------+-------+
Method one (left join)
It should be noted here that the bonus can be empty.
# Write your MySQL query statement below
select e.name,b.bonus
from employee e left join bonus b
on b.empId=e.empId
where b.bonus<1000 or b.bonus is null
3. Looking for user referrals (584)
Given a table customer
, which holds all customer information and their referrers.
+------+------+-----------+ | id | name | referee_id| +------+------+-----------+ | 1 | Will | NULL | | 2 | Jane | NULL | | 3 | Alex | 2 | | 4 | Bill | NULL | | 5 | Zack | 1 | | 6 | Mark | 2 | +------+------+-----------+
Write a query statement that returns a list of customers whose referrer numbers are not 2.
For the example data above, the result is:
+------+ | name | +------+ | Will | | Jane | | Bill | | Zack | +------+
method one
simpler, no longer described
# Write your MySQL query statement below
select name from customer
where referee_id != 2 or referee_id is null
4. Investments in 2016 (585)
Insurance
surface:
+-------------+-------+ | Column Name | Type | +-------------+------ -+ | pid | int | | tiv_2015 | float | | tiv_2016 | float | | lat | float | | lon | float | +-------------+-------+ pid is the primary key of this table. Each row in the table contains a piece of insurance information, where: pid is the policyholder's policy number. tiv_2015 is the total insurance amount of the policyholder in 2015, and tiv_2016 is the total insurance amount of the policyholder in 2016. lat is the latitude of the policyholder's city. The topic data ensures that lat is not empty. lon is the longitude of the policyholder's city. The title data ensures that lon is not empty.
Please write a SQL query to report the sum of the sum insured amount of all policyholders who meet the following conditions in 2016 ( tiv_2016
):
- His 2015 sum insured (
tiv_2015
) was at least the same as the 2015 sum insured of at least one other policyholder. - His city must be different from the other policyholders (that is, (
lat, lon
) cannot be exactly the same as any other policyholder).
tiv_2016
Rounded to two decimal places .
The query result format is shown in the following example.
example
Input: Insurance table: +-----+----------+----------+-----+-----+ | pid | tiv_2015 | tiv_2016 | lat | lon | +-----+----------+----------+-----+-----+ | 1 10 | 5 | 10 | 10 | | 2 | 20 | 20 | 20 | 20 | | 3 | 10 | 30 | 20 | 20 | | 4 | 10 | 40 | 40 | 40 | +-----+- ---------+----------+-----+-----+ Output: +----------+ | tiv_2016 | +----------+ | 45.00 | +----------+ Explanation: Both the first record and the last record in the table meet two conditions. The tiv_2015 value of 10 is the same as the third and fourth records, and their positions are unique. The second record does not meet either condition. Its tiv_2015 is different from other policyholders, and its location is the same as the third record, which also causes the third record not to meet the title requirements. So the result is the sum of tiv_2016 of the first and last record, which is 45.
Method 1 (join, count)
First query condition one and condition two respectively, and then connect the query.
Condition 1:
select tiv_2015 from insurance group by tiv_2015 having count(*)>=2
Condition two:
select * from insurance group by lat,lon having count(*)=1
# Write your MySQL query statement below
select round(sum(a.tiv_2016),2) tiv_2016 from
# 他在 2015 年的投保额 (tiv_2015) 至少跟一个其他投保人在 2015 年的投保额相同
(select * from insurance group by lat,lon having count(*)=1) a join
# 他在 2015 年的投保额 (tiv_2015) 至少跟一个其他投保人在 2015 年的投保额相同。
(select tiv_2015 from insurance group by tiv_2015 having count(*)>=2) b
on a.tiv_2015=b.tiv_2015
Method 2 (in)
The concat() function is used to concatenate two strings to form a single string
select round(sum(tiv_2016), 2) tiv_2016 from insurance
where tiv_2015 in (
select tiv_2015 from insurance group by tiv_2015 having count(*) > 1
) and concat(lat, lon) in (
select concat(lat, lon) from insurance group by lat, lon having count(*) = 1
);
Method 3 (count)
Using the count window function, the idea is similar to method one
select round(sum(tiv_2016), 2) tiv_2016 from (
select
tiv_2016,
count(*) over(partition by tiv_2015) count_tiv_2015,
count(*) over(partition by lat, lon) count_lat_lon
from insurance
) as temp where count_lat_lon = 1 and count_tiv_2015 > 1
Method 4 (connection plus in)
First use self-connection to satisfy condition one, and then use not in to satisfy condition two
# Write your MySQL query statement below
select round(sum(distinct o1.TIV_2016), 2) tiv_2016
from insurance o1 inner join insurance o2
on o1.PID != o2.PID and o1.TIV_2015 = o2.TIV_2015
where (o1.LAT,o1.lon) not in
(select lat, lon from insurance where o1.PID != PID)