mysql常见面试题
Posted turningli
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql常见面试题相关的知识,希望对你有一定的参考价值。
176.编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) 。
select distinct Salary as SecondHighestSalary from Employee order by Salary desc limit 1 offset 1;
184.编写一个 SQL 查询,找出每个部门工资最高的员工。例如,根据上述给定的表格,Max 在 IT 部门有最高工资,Henry 在 Sales 部门有最高工资。
select d.Name as Department,e.Name as Employee,e.Salary from Employee e inner join Department d on e.DepartmentId = d.Id and e.Salary >=( select max(Salary) from Employee where DepartmentId = d.Id );
596.有一个courses 表 ,有: student (学生) 和 class (课程)。
请列出所有超过或等于5名学生的课。
select class from courses group by class having count(distinct student)>=5;
178.编写一个 SQL 查询来实现分数排名。如果两个分数相同,则两个分数排名(Rank)相同。请注意,平分后的下一个名次应该是下一个连续的整数值。换句话说,名次之间不应该有“间隔”。
select Score,( select count(distinct Score) from Scores where Score >= s.score) as Rank from Scores s order by Score desc;
如何统计某张表里每个小时(存的年月日时分秒格式)的数据量
SELECT DATE_FORMAT(TimeStart, ‘%Y-%m-%d %H:00:00‘) AS time, COUNT(*) AS num FROM track WHERE Flag = 0 AND Duration >= 300 GROUP BY time ORDER BY time;
输出某个字段下值出现次数超过三次的值
SELECT business_unit_code from t_replenish_config GROUP BY business_unit_code HAVING count( *)>3;
1.INNER JOIN (内连接)
内连接是一种一一映射关系,就是两张表都有的才能显示出来 用韦恩图表示是两个集合的交集,如图:
SELECT A.PK AS A_PK,A.Value AS A_Value,B.PK AS B_PK,B.Value AS B_Value FROM table_a A INNER JOIN table_b B ON A.PK = B.PK;
2.LEFT JOIN (左连接)
左连接是左边表的所有数据都有显示出来,右边的表数据只显示共同有的那部分,没有对应的部分只能补空显示,所谓的左边表其实就是指放在left join的左边的表
用韦恩图表示如下:
SELECT A.PK AS A_PK,A.Value AS A_Value,B.PK AS B_PK,B.Value AS B_Value FROM table_a A LEFT JOIN table_b B ON A.PK = B.PK;
3.RIGHT JOIN(右连接)
右连接正好是和左连接相反的,这里的右边也是相对right join来说的,在这个右边的表就是右表
用韦恩图表示如下:
SELECT A.PK AS A_PK,A.Value AS A_Value,B.PK AS B_PK,B.Value AS B_Value FROM table_a A RIGHT JOIN table_b B ON A.PK = B.PK;
4.OUTER JOIN(外连接、全连接)
查询出左表和右表所有数据,但是去除两表的重复数据
韦恩图表示如下:
SELECT A.PK AS A_PK,A.Value AS A_Value,B.PK AS B_PK,B.Value AS B_Value FROM table_a A LEFT JOIN table_b B ON A.PK = B.PK UNION SELECT A.PK AS A_PK,A.Value AS A_Value,B.PK AS B_PK,B.Value AS B_Value FROM table_a A RIGHT JOIN table_b B ON A.PK = B.PK;
https://blog.csdn.net/u014204541/article/details/79739980
以上是关于mysql常见面试题的主要内容,如果未能解决你的问题,请参考以下文章