当前位置:   article > 正文

MSQL——力扣SQL语句练习_sql语句训练 力扣

sql语句训练 力扣

1.【查找重复邮箱】编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个。

  1. +----+------------------+
  2. | Id | Email |
  3. +----+------------------+
  4. | 1 | john@example.com |
  5. | 2 | bob@example.com |
  6. | 3 | john@example.com |
  7. +----+------------------+
  1. SELECT p1.*
  2. FROM Person p1,
  3. Person p2
  4. WHERE
  5. p1.Email = p2.Email
  1. Id 是这个表的主键。
  2. 例如,在运行你的查询语句之后,上面的 Person表应返回以下几行:
  1. +----+------------------+
  2. | Id | Email |
  3. +----+------------------+
  4. | 1 | john@example.com |
  5. | 2 | bob@example.com |
  6. +----+------------------+

答案:delete from Person where Id not in(select Id from ( select min(Id) Id,Email from Person group by Email)t)

官方答案方法:使用 DELETE 和 WHERE 子句

算法

我们可以使用以下代码,将此表与它自身在电子邮箱列中连接起来

  1. SELECT p1.*
  2. FROM Person p1,
  3. Person p2
  4. WHERE
  5. p1.Email = p2.Email
  6. ;

然后我们需要找到其他记录中具有相同电子邮件地址的更大 ID。所以我们可以像这样给 WHERE 子句添加一个新的条件

  1. SELECT p1.*
  2. FROM Person p1,
  3. Person p2
  4. WHERE
  5. p1.Email = p2.Email AND p1.Id > p2.Id
  6. ;

因为我们已经得到了要删除的记录,所以我们最终可以将该语句更改为 DELETE

  1. DELETE p1 FROM Person p1,
  2. Person p2
  3. WHERE
  4. p1.Email = p2.Email AND p1.Id > p2.Id

 

 2.【大的国】

 答案:select name,population,area from World where area > 3000000 or population > 25000000; 

select name ,population ,area from World where area > 3000000 UNION select name ,population ,area from World where population > 25000000;

3.【查找重复电子邮箱】

答案:select email from Person group by email having count(email)>1;

答案:select Email from(select Email, count(Email) as num from Person group by Email) as statistic where num > 1;

4. 【有趣的电影】

答案: select * from cinema where mod(id, 2) = 1 and description != 'boring' order by rating DESC;

取余是用函数mod(numer1,number2),其返回的值为其余数值

如:mod(id,2) = 1 返回id号是奇数的id

 5.【组合两个表】

编写一个 SQL 查询,满足条件:无论 person 是否有地址信息,都需要基于上述两表提供 person 的以下信息:

FirstName, LastName, City, State

答案:select FirstName, LastName, City, State from Person left join Address on Person.PersonId = Address.PersonId;

6.【交换工资】

答案: UPDATE salary SET sex = CASE sex WHEN 'm' THEN 'f' ELSE 'm' END;

7.【超过经理收入的员工】

答案:select p2.name Employee from employee p1 inner join employee p2 on p1.Id = p2.ManagerId and p2.Salary > p1.Salary and p2.ManagerId IS NOT NULL

答案:select a.Name as Employee from Employee a where a.Salary > (select b.Salary from Employee b where b.Id = a.ManagerId)

8. 【从不订购的客户】

答案:select Name as Customers from Customers c left join Orders o on c.Id=o.CustomerId where o.CustomerId is null

答案:select Name as Customers from Customers as c1 where c1.Id not in ( select distinct CustomerId
from Orders)

9. 【换座位】

  1. 答案:
  2. select (case
  3.     when mod(id,2) =1 and id = sm.ma then id
  4.     when mod(id,2) =1 and id <> sm.ma then id+1
  5.     else id-1
  6.    end) id,
  7.    student
  8. from seat,(select max(id) as ma from seat) sm order by id 

10.【上升的温度】

 答案:SELECT weather.id AS 'Id' FROM weather JOIN weather w ON DATEDIFF(weather.RecordDate, w.RecordDate) = 1 AND weather.Temperature > w.Temperature;

DATEDIFF() 函数返回两个日期之间的天数。语法DATEDIFF(date1,date2)

11.【超过5名学生的课】

 

答案:select class from courses group by class having count(distinct(student))>4

12.【第二高的薪水】

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

答案:SELECT  IFNULL( (SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC  LIMIT 1 OFFSET 1), NULL) AS SecondHighestSalary

 //待续*********

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/872077
推荐阅读
相关标签
  

闽ICP备14008679号