本文主要是介绍leetcode_database由易到难记录一(简单),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目来源链接
分类 - Database
其中会员才能看的题需要注明出处:
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
细节问题回顾
1.SQL是否对大小写敏感:
关键字、表中的字段函数名均不敏感。
简单题目、解答、相关概念
1.组合两个表
编写一个 SQL 查询,满足条件:无论 person 是否有地址信息,都需要基于上述两表提供 person 的以下信息:
FirstName, LastName, City, State
select p.FirstName,p.LastName,a.City,a.State
from Person as p left join Address as a on p.PersonId = a.PersonId
2.第二高的薪水
编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) 。没有则返回null。
相关概念:
limit子句和offset子句用法:
【MySQL】LIMIT以及LIMIT OFFSET 使用方法介绍
select(select distinctsalary from employeeorder by salary desclimit 1 offset 1) as SecondHighestSalary
3.超过经理收入的员工
给定 Employee 表,编写一个SQL查询,该查询可以获取收入超过他们经理的员工的姓名。在上面的表格中,Joe是唯一一个收入超过他的经理的员工。
运行错误原因:
输出的表格结构不符合题目要求
select a.name as Employee #此处的as语句将决定输出结果的属性名
from employee as a , employee as b
where a.managerid = b.id and a.salary > b.salary
4.查找重复的电子邮箱
编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。
说明:所有电子邮箱都是小写字母。
select Email
from Person
group by Email
having count(Email) >1 #注意SQL语法,计数(计算有多少条数据)使用count
5.从不订购的客户
某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。
select Customers.name as Customers
from Customers
where Customers.Id not in
(select Customerid from Orders)
6.删除重复的电子邮箱
编写一个SQL查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留Id最小的那个。
注意:
自连接效率较低,待提升
delete p1
from Person as p1,Person as p2
where p1.email = p2.emailand p1.id > p2.id
7.上升的温度
给定一个Weather表,编写一个SQL查询,来查找与之前(昨天的)日期相比温度更高的所有日期的Id。
注意:
不能只因为表面的ID与日期之间的关系就不使用日期来比较今天和昨天的差别。
datediff函数的使用方法是如何的?
select w1.id as Id
from Weather w1, Weather w2
where w1.Temperature > w2.Temperature#and w1.id = w2.id + 1and datediff(w1.RecordDate,w2.RecordDate) = 1 #w1 是 w2的后一天
8.游戏玩法分析
写一条 SQL 查询语句获取每位玩家第一次登陆平台的日期。
select player_id, min(event_date) as first_login
from Activity
group by player_id
9.游戏玩法分析 II
请编写一个 SQL 查询,描述每一个玩家首次登陆的设备名称
注意:
where的效率比join慢
select a.player_id, a.device_id
from Activity as a
where (a.player_id, a.event_date) #注意where后面包含两个以上的字段要加括号
in (select player_id, min(event_date) as first_loginfrom Activitygroup by player_id)
10.员工奖金
选出所有 bonus < 1000 的员工的 name 及其 bonus。
select e.name as name, b.bonus as bonus
from Employee as e left join Bonus as b
on e.empId = b.empId
where b
这篇关于leetcode_database由易到难记录一(简单)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!