码迷,mamicode.com
首页 > 数据库 > 详细

mysql常见面试题

时间:2020-03-12 23:23:34      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:icc   span   format   内连接   mft   asi   business   conf   psi   

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常见面试题

标签:icc   span   format   内连接   mft   asi   business   conf   psi   

原文地址:https://www.cnblogs.com/turningli/p/12483377.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!