2020.4.18
176. Second Highest Salary
SQL Schema
Create table If Not Exists Employee (Id int, Salary int) Truncate table Employee insert into Employee (Id, Salary) values ('1', '100') insert into Employee (Id, Salary) values ('2', '200') insert into Employee (Id, Salary) values ('3', '300')
Write a SQL query to get the second highest salary from the
Employee
table.+----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+
For example, given the above Employee table, the query should return
200
as the second highest salary. If there is no second highest salary, then the query should returnnull
.+---------------------+ | SecondHighestSalary | +---------------------+ | 200 | +---------------------+
-- 第二大就是小于最大里面的最大,好理解
# Write your MySQL query statement below
select max(salary) SecondHighestSalary
from employee
where salary < (
select max(salary)
from employee
)
181. Employees Earning More Than Their Managers
SQL Schema
Create table If Not Exists Employee (Id int, Name varchar(255), Salary int, ManagerId int) Truncate table Employee insert into Employee (Id, Name, Salary, ManagerId) values ('1', 'Joe', '70000', '3') insert into Employee (Id, Name, Salary, ManagerId) values ('2', 'Henry', '80000', '4') insert into Employee (Id, Name, Salary, ManagerId) values ('3', 'Sam', '60000', 'None') insert into Employee (Id, Name, Salary, ManagerId) values ('4', 'Max', '90000', 'None')
The
Employee
table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.+----+-------+--------+-----------+ | Id | Name | Salary | ManagerId | +----+-------+--------+-----------+ | 1 | Joe | 70000 | 3 | | 2 | Henry | 80000 | 4 | | 3 | Sam | 60000 | NULL | | 4 | Max | 90000 | NULL | +----+-------+--------+-----------+
Given the
Employee
table, write a SQL query that finds out employees who earn more than their managers. For the above table, Joe is the only employee who earns more than his manager.+----------+ | Employee | +----------+ | Joe | +----------+
# Write your MySQL query statement below
select a.Name Employee
from Employee a
where a.salary > (
select b.salary
from employee b
where a.managerid=b.id
)
182. Duplicate Emails
SQL Schema
Create table If Not Exists Person (Id int, Email varchar(255)) Truncate table Person insert into Person (Id, Email) values ('1', '[email protected]') insert into Person (Id, Email) values ('2', '[email protected]') insert into Person (Id, Email) values ('3', '[email protected]')
Write a SQL query to find all duplicate emails in a table named
Person
.+----+---------+ | Id | Email | +----+---------+ | 1 | a@b.com | | 2 | c@d.com | | 3 | a@b.com | +----+---------+
For example, your query should return the following for the above table:
+---------+ | Email | +---------+ | a@b.com | +---------+
-- 统计出现次数大于1即可 记得去重
# Write your MySQL query statement below
select distinct Email
from Person
where email in (
select email
from Person
group by email
having count(email ) > 1
)