https://leetcode.com/problems/second-highest-salary/
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 second highest salary is 200. If there is no second highest salary, then the query should return null.# Write y...
https://leetcode.com/problems/combine-two-tables/
Combine Two TablesTable: Person
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
+-------------+---------+
PersonId is the primary key column for this table.Table: Address
+-------------+---------+
| Column Name | Type |
+-------------+---...
2.用到的知识点limit : limit可以接收两个参数 limit 10 或者 limit 0,10 前者表示查询显示前10行后者表示从第0行的往后10行,也就是第1行到第10行
IFNULL(expression_1,expression_2); 如果expression_1不为NULL 就显示自己,否则显示expression_2类似Java expression1 != null? expression1:expression2
3.最终的SQL语句 select IFNULL
((select distinct Salary from Employee order by Salary desc limit 1,1),(n...
Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id.+----+------------------+
| Id | Email |
+----+------------------+
| 1 | john@example.com |
| 2 | bob@example.com |
| 3 | john@example.com |
+----+------------------+
Id is the primary key column for this table.
For example, after running your query, t...
题目1 1112. 每位学生的最高成绩
编写一个 SQL 查询,查询每位学生获得的最高成绩和它所对应的科目,若科目成绩并列,取?course_id?最小的一门。查询结果需按?student_id?增序进行排序。
题解
注意这里grade的外层查询需要结合group by,或者查max(grade),或者使用join查grade
代码
# Write your MySQL query statement below
select student_id, min(course_id) as course_id,grade
from Enrollments
where (student_id,grade) in...
题目
编写一个 SQL 查询,查询购买了 S8 手机却没有购买 iPhone 的买家。
题解
使用having + sum+if,而不是自查询。
代码
# Write your MySQL query statement below
select buyer_id
from Sales s join Product p
on s.product_id = p.product_id
group by buyer_id
having sum(if(product_name=‘S8‘,1,0))>0
and sum(if(product_name=‘iPhone‘,1,0))=0
[LeetCode]1083. 销售分析 II(Mysql,having+if)标签:rod and 销售...
题目
几个朋友来到电影院的售票处,准备预约连续空余座位。你能利用表?cinema?,帮他们写一个查询语句,获取所有空余座位,并将它们按照 seat_id 排序后返回吗?| seat_id | free |
|---------|------|
| 1 | 1 |
| 2 | 0 |
| 3 | 1 |
| 4 | 1 |
| 5 | 1 |
?对于如上样例,你的查询语句应该返回如下结果。?| seat_id |
|---------|
| 3 |
| 4 |
| 5 |
题解
使用自...
题目
Table:?Product+--------------+---------+
| Column Name | Type |
+--------------+---------+
| product_id | int |
| product_name | varchar |
| unit_price | int |
+--------------+---------+
product_id 是这个表的主键
Table:?Sales+-------------+---------+
| Column Name | Type |
+-------------+---------+
| seller_id | int |
| product_id | int |
| buyer_id | int ...
id,
@cnt:=if(@id=id and @pre_date=date_sub(login_date, interval 1 day), @cnt+1, 1) as cnt,
@id:=id,
@pre_date:=login_date
from
(select * from `Logins` order by id, login_date) as a
(select @id:=null, @pre_date:=null, @cnt:=0) as b
注意!!!有坑由题目或者一般情况下,用户一天内登录次数可能不止一次,所以会有同用户同时间多次的登录记录存在所以得去重select
id,
@cnt:=if(@id=id and @pre_date=date_sub(l...
Evaluate the value of arithmetic expression in Reverse Polish Notation. Valid operator are ,-,*,/. Each operand may be an integer or another expression. Some examples: [2, 1, , 3, *] - ((21)*3) - 9 [4, 13, 5, /, ] - (4 (13 / 5)) - 6 分析:Evaluate the value of arithmetic expression in Reverse Polish Notation.
Valid operator are +,-,*,/. Each operand may be an integer or another expression.
Some ex...
1 MYSQL8窗口函数
窗口函数与聚合函数很像,他们都是在一组记录而不是整张表上执行的。但是,一个聚合函数在一组记录执行后只返回一条结果而窗口函却会对改分组内的每行记录都返回一个结果。
函数名 参数 描述
cume_dist() 否 累计分布值。即分组值小于等于当前值的行数与分组总行数的比值。取值范围为(0,1]。
dense_rank() 否 不间断的组内排序。使用这个函数时,可以出现1,1,2,2这种形式的分组。
first_value() 是;first_value(...
计算机小白QAQ,因为想找数分岗暑期实习所以充了会员想集中刷一下leetcode的mysql部分。写这个系列博文和大家们交流一下,后面也会持续更新面经准备的一些问题,欢迎同好们一起交流,求大佬轻喷QAQ。因为自己初学也走了很多弯路,所以会尽量写得详细一点,如果可以帮助到后来的朋友们,请各位留言鼓励一下哈哈哈哈。
1076.项目员工II这道题比较难,我一共大概有三个思路和大家交流。
思路1:首先构造出一个表得到各个项目与该项目人...
计算机小白QAQ,因为想找数分岗暑期实习所以充了会员想集中刷一下leetcode的mysql部分。写这个系列博文和大家们交流一下,后面也会持续更新面经准备的一些问题,欢迎同好们一起交流,求大佬轻喷QAQ。因为自己初学也走了很多弯路,所以会尽量写得详细一点,如果可以帮助到后来的朋友们,请各位留言鼓励一下哈哈哈哈。
1069.产品销售分析II思路:根据product_id分组利用group by分组统计,讲真我觉得这个product表没啥用。
代码:
se...
计算机小白QAQ,因为想找数分岗暑期实习所以充了会员想集中刷一下leetcode的mysql部分。写这个系列博文和大家们交流一下,后面也会持续更新面经准备的一些问题,欢迎同好们一起交流,求大佬轻喷QAQ。因为自己初学也走了很多弯路,所以会尽量写得详细一点,如果可以帮助到后来的朋友们,请各位留言鼓励一下哈哈哈哈。
584.寻找用户的推荐人思路:推荐人编号一共就三种状态:2,其他id,null。不是2,只要过滤出不是2和null就满足条件...
编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) 。
IdSalary110022003300
例如上述 Employee 表,SQL查询应该返回 200 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 null。
SecondHighestSalary200
解题思路1: 使用 dense_rank()函数来获取去重之后第二高的工资,使用聚合函数max()能确保在没有没有返回值时获取到的最终结果为null。
select
max(Salary) as SecondHighestSalary
from
(select ...