Leetcode中的SQL题目练习(二)

175. Combine Two Tables

https://leetcode.com/problems/combine-two-tables/description/

Description

Person 表:

Column NameType
PersonIdint
FirstNamevarchar
LastNamevarchar

Address 表:

Column NameType
AddressIdint
PersonIdint
Cityvarchar
Statevarchar

查找 FirstName, LastName, City, State 数据,而不管一个用户有没有填地址信息。

solution

SELECT
    FirstName,
    LastName,
    City,
    State
FROM
    Person P
    LEFT JOIN Address A
    ON P.PersonId = A.PersonId;

181. Employees Earning More Than Their Managers ??

https://leetcode.com/problems/employees-earning-more-than-their-managers/description/

Description

Employee 表:

IdNameSalaryManagerId
1Joe700003
2Henry800004
3Sam60000NULL
4Max90000NULL

查找薪资大于其经理薪资的员工信息。

solution

 SELECT
    E1.NAME AS Employee 
FROM
    Employee E1
    INNER JOIN Employee E2
    ON E1.ManagerId = E2.Id
    AND E1.Salary > E2.Salary;

183. Customers Who Never Order

https://leetcode.com/problems/customers-who-never-order/description/

Description

Curstomers 表:

IdName
1Joe
2Henry
3Sam
4Max

Orders 表:

IdCustomerId
13
21

查找没有订单的顾客信息:

Customers
Henry
Max

soulution:

(1)子查询

SELECT 
   Name AS Customers 
FROM 
   Customers
WHERE
   Id NOT IN  (SELECT CustomerId FROM Orders)

(2)

SELECT
    C.Name AS Customers
FROM
    Customers C
    LEFT JOIN Orders O
    ON C.Id = O.CustomerId
WHERE
    O.CustomerId IS NULL;

184. Department Highest Salary ???

https://leetcode.com/problems/department-highest-salary/description/

Description

Employee 表:

IdNameSalaryDepartmentId
1Joe700001
2Henry800002
3Sam600002
4Max900001

Department 表:

IdName
1IT
2Sales

查找一个 Department 中收入最高者的信息:

DepartmentEmployeeSalary
ITMax90000
SalesHenry80000

soulution:

SELECT
    D.NAME Department,
    E.NAME Employee,
    E.Salary
FROM
    Employee E,
    Department D,
    ( SELECT DepartmentId, MAX( Salary ) Salary FROM Employee GROUP BY DepartmentId ) M
WHERE
    E.DepartmentId = D.Id
    AND E.DepartmentId = M.DepartmentId
    AND E.Salary = M.Salary;

176. Second Highest Salary ??

https://leetcode.com/problems/second-highest-salary/description/

Description

IdSalary
1100
2200
3300

查找工资第二高的员工。

solution

SELECT
    (SELECT DISTINCT
            Salary
        FROM
            Employee
        ORDER BY Salary DESC
        LIMIT 1 OFFSET 1) AS SecondHighestSalary

177. Nth Highest Salary

https://leetcode.com/problems/nth-highest-salary/
Description
查找工资第 N 高的员工。

CREATE FUNCTION getNthHighestSalary ( N INT ) 
        RETURNS INT 
        BEGIN
            SET N = N - 1;
             RETURN ( 
                    SELECT (
                        SELECT DISTINCT Salary 
                        FROM Employee 
                        ORDER BY Salary DESC 
                        LIMIT N, 1 ) 
             );
        END

178. Rank Scores ??

https://leetcode.com/problems/rank-scores/description/

Description:

IdScore
13.50
23.65
34.00
43.85
54.00
63.65

将得分排序,并统计排名。

ScoreRank
4.001
4.001
3.852
3.653
3.653
3.504

Solution:

(1)解法一:对于每一个分数,从表中找出有多少个大于或等于该分数的不重复分数,然后降序排列

select Score,
(select count(distinct Score) from Scores s2 where s2.Score >= s1.Score) Rank 
from Scores  s1
order by Score DESC;

(2)解法二:使用联结 join,条件是左表的分数小于等于右表的分数时,对右表的分数进行计数(即计算有几个不重复的分数大于自己,计算结果就是rank),然后根据id分组后,再根据分数降序排列

select s1.Score, count(distinct s2.Score) Rank
from Scores as s1 inner join Scores as s2 on s1.Score <= s2.Score
group by s1.Id 
order by s1.Score DESC;

180. Consecutive Numbers

https://leetcode.com/problems/consecutive-numbers/description/

Description

数字表:

IdNum
11
21
31
42
51
62
72

编写一个 SQL 查询,查找所有至少连续出现三次的数字。

ConsecutiveNums
1

solution:

(1)用l1分别和l2, l3内交,l1和l2的Id下一个位置比,l1和l3的下两个位置比,然后将Num都相同的数字返回即可

select distinct l1.Num ConsecutiveNums from Logs l1
join Logs l2 on l1.Id = l2.Id - 1
join Logs l3 on l1.Id = l3.Id - 2
where l1.Num = l2.Num and l2.Num = l3.Num;

(2)直接在三个表的实例中查找,然后把四个条件限定上,就可以返回正确结果了 ??

SELECT DISTINCT l1.Num FROM Logs l1, Logs l2, Logs l3
WHERE l1.Id = l2.Id - 1 AND l2.Id = l3.Id - 1
AND l1.Num = l2.Num AND l2.Num = l3.Num;

626. Exchange Seats ???

https://leetcode.com/problems/exchange-seats/description/
seat 表存储着座位对应的学生。

idstudent
1Abbot
2Doris
3Emerson
4Green
5Jeames

要求交换相邻座位的两个学生,如果最后一个座位是奇数,那么不交换这个座位上的学生。

idstudent
1Doris
2Abbot
3Green
4Emerson
5Jeames

solution

对照上表及其查询结果可以得知,当原id为奇数时,交换座位后的id变为id+1,当原id为偶数时,交换座位后的id变为id-1,另一个方面需要考虑的是,学生人数为奇数时,最后一个学生的id不变,故应当用子查询确定学生的人数,然后分情况讨论即可

select (case
      when mod(id,2)!=0 and id!=counts then id+1
      when mod(id,2)!=0 and id=counts then id
      else id-1 end)as id,student
      from seat,(select count(*)as counts from seat)as seat_counts
                order by id;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值