Mysql 子查询,最值查询

1.leetcode-184:查找部门内最高的薪水

Mysql 子查询,最值查询_第1张图片
Mysql 子查询,最值查询_第2张图片
Mysql 子查询,最值查询_第3张图片
首先是一个子查询,找出一个部门里最高的那个工资
在这里插入图片描述
Mysql 子查询,最值查询_第4张图片
随后查找最终需要的值,并且部门编号以及薪水应该包含在这个子查询中
最终答案:

# Write your MySQL query statement below
SELECT Department.name AS Department,
       Employee.name AS Employee,
       Salary
FROM Employee INNER JOIN Department 
ON Employee.departmentId = Department.id
WHERE 
    (Employee.DepartmentId,Salary) IN
    ( SELECT DepartmentId,MAX(Salary)
      FROM Employee
      GROUP BY DepartmentId
    );

2.leetcode-607 销售员(两次子查询)

Mysql 子查询,最值查询_第5张图片

Mysql 子查询,最值查询_第6张图片
Mysql 子查询,最值查询_第7张图片

# Write your MySQL query statement below
SELECT name 
FROM SalesPerson AS S
WHERE S.sales_id NOT IN (
    SELECT sales_id
    FROM Orders
    WHERE Orders.com_id = ( SELECT com_id
                            FROM Company
                            WHERE Company.name = 'RED'
    )
);

3.leetcode-585. 2016年的投资

Mysql 子查询,最值查询_第8张图片
Mysql 子查询,最值查询_第9张图片
Mysql 子查询,最值查询_第10张图片

# Write your MySQL query statement below
# roound(,2)四舍五入保留两位小数
SELECT round(SUM(Insurance.tiv_2016),2) AS tiv_2016
FROM  Insurance
WHERE Insurance.tiv_2015 IN ( SELECT tiv_2015
                              FROM Insurance 
                              GROUP BY tiv_2015
                              HAVING COUNT(tiv_2015 ) >=2)
AND CONCAT(Insurance.lat,' ',Insurance.lon) IN (  SELECT CONCAT(lat,' ',lon) 
                                              FROM Insurance
                                              GROUP BY lat,lon
                                              HAVING COUNT(*)=1)

;

你可能感兴趣的:(MySql,mysql,数据库)