SQL面试题

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

FirstName, LastName, City, State

表1: Person

+-------------+---------+
| 列名         | 类型     |
+-------------+---------+
| PersonId    | int     |
| FirstName   | varchar |
| LastName    | varchar |
+-------------+---------+             
PersonId 是上表主键

表2: Address

+-------------+---------+
| 列名         | 类型    |
+-------------+---------+
| AddressId   | int     |
| PersonId    | int     |
| City        | varchar |
| State       | varchar |
+-------------+---------+
AddressId 是上表主键
@不管address是否有信息,选出员工即可,想到对person表进行左外连接
select p.FirstName,p.LastName,a.City,a.State 
from Person as p left outer join Address as a 
on p.PersonId=a.PersonId 
where p.FirstName is not null or p.LastName is not null; 

2.编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) 。

+----+--------+
| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+

例如上述 Employee 表,SQL查询应该返回 200 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 null

+---------------------+
| SecondHighestSalary |
+---------------------+
| 200                 |
+---------------------+
@先写出一般情况,再写出只有一条记录没有第二大的情况
@使用ifnull和limit限制函数完美解决!
@需要考虑到值重复的情况!

本人的:
select ifnull( 
(select Salary from  (select distinct Salary from Employee) as tmp 
 order by  Salary desc limit 1,1) , null) 
as SecondHighestSalary;


/* 别人 效率高的: 不等于这块很关键*/
select max(Salary) as SecondHighestSalary 
from Employee 
where salary<>(
select max(Salary)  from Employee 
)

3.编写一个 SQL 查询,获取 Employee 表中第 高的薪水(Salary)。

+----+--------+
| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+

例如上述 Employee 表,n = 2 时,应返回第二高的薪水 200。如果不存在第 高的薪水,那么查询应返回 null

+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| 200                    |
+------------------------+
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
  set N = N-1;
  RETURN (
      # Write your MySQL query statement below.
      select ifnull(
      (select Salary from (select distinct Salary from Employee) as tmp
      order by  Salary desc limit N,1) ,null)
  );
END

@
create function 函数名([参数列表]) returns 数据类型
begin
 sql语句;
 return 值;
end;
@

4.编写一个 SQL 查询来实现分数排名。如果两个分数相同,则两个分数排名(Rank)相同。请注意,平分后的下一个名次应该是下一个连续的整数值。换句话说,名次之间不应该有“间隔”。

+----+-------+
| Id | Score |
+----+-------+
| 1  | 3.50  |
| 2  | 3.65  |
| 3  | 4.00  |
| 4  | 3.85  |
| 5  | 4.00  |
| 6  | 3.65  |
+----+-------+

例如,根据上述给定的 Scores 表,你的查询应该返回(按分数从高到低排列):

+-------+------+
| Score | Rank |
+-------+------+
| 4.00  | 1    |
| 4.00  | 1    |
| 3.85  | 2    |
| 3.65  | 3    |
| 3.65  | 3    |
| 3.50  | 4    |
+-------+------+
@思路:使用交叉连接,要去重然后计算rank。
select Score,(select count(distinct Score) from Scores where Score>=s.Score) as Rank
from Scores as s order by Score desc;

5.编写一个 SQL 查询,查找所有至少连续出现三次的数字。

+----+-----+
| Id | Num |
+----+-----+
| 1  |  1  |
| 2  |  1  |
| 3  |  1  |
| 4  |  2  |
| 5  |  1  |
| 6  |  2  |
| 7  |  2  |
+----+-----+

例如,给定上面的 Logs 表, 1 是唯一连续出现至少三次的数字。

+-----------------+
| ConsecutiveNums |
+-----------------+
| 1               |
+-----------------+
@对num分组筛选出大于三的组,然后在计算有多少组
@select count(*) as ConsecutiveNums from
@(select Num from Logs group by Num having count(*)>=3) as tmp;

@至少连续出现三次的数字 ,上面错了.....
@对每一位数字计算后两位是否跟当前数字相同 用到了相关子查询

select distinct l1.Num as ConsecutiveNums from
Logs as l1 where exists (
   select Id from Logs as l2 where l2.Id=l1.Id+1 and l2.Num=l1.Num)
and  exists (
   select Id from Logs as l3 where l3.Id=l1.Id+2 and l3.Num=l1.Num);

@但是比较慢,利用between and来处理可以快点
select distinct l1.Num as ConsecutiveNums from Logs as l1 where 
(select count(1) from Logs as l2 where l2.Id between l1.Id and l1.Id+2 and l1.Num=l2.Num)>=3;

6.Employee 表包含所有员工,他们的经理也属于员工。每个员工都有一个 Id,此外还有一列对应员工的经理的 Id。

+----+-------+--------+-----------+
| Id | Name  | Salary | ManagerId |
+----+-------+--------+-----------+
| 1  | Joe   | 70000  | 3         |
| 2  | Henry | 80000  | 4         |
| 3  | Sam   | 60000  | NULL      |
| 4  | Max   | 90000  | NULL      |
+----+-------+--------+-----------+

给定 Employee 表,编写一个 SQL 查询,该查询可以获取收入超过他们经理的员工的姓名。在上面的表格中,Joe 是唯一一个收入超过他的经理的员工。

+----------+
| Employee |
+----------+
| Joe      |
+----------+
@对每一行找到对应的经理看是否钱多于经理的钱 
@利用相关子查询实现

select Name as Employee from Employee as e
where e.Salary>(
    select ee.Salary from Employee as ee where e.ManagerId=ee.Id);

@也可进行自连接(内连接实现),效率高
select e1.Name as Employee from Employee as e1 inner join Employee as e2
on e1.ManagerId=e2.Id and e1.Salary>e2.Salary;

7.编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。

示例:

+----+---------+
| Id | Email   |
+----+---------+
| 1  | [email protected] |
| 2  | [email protected] |
| 3  | [email protected] |
+----+---------+

根据以上输入,你的查询应返回以下结果:

+---------+
| Email   |
+---------+
| [email protected] |
+---------+
@对Email进行分组,找出count大于1的组即可
select Email from (
    select Email from Person group by Email having count(*)>1
) as tmp;

select Email from Person group by Email having count(*)>1 #也可以

8.某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。

Customers 表:

+----+-------+
| Id | Name  |
+----+-------+
| 1  | Joe   |
| 2  | Henry |
| 3  | Sam   |
| 4  | Max   |
+----+-------+

Orders 表:

+----+------------+
| Id | CustomerId |
+----+------------+
| 1  | 3          |
| 2  | 1          |
+----+------------+

例如给定上述表格,你的查询应返回:

+-----------+
| Customers |
+-----------+
| Henry     |
| Max       |
+-----------+
@只要customers表的id和order表的customerid不相等就选出来 
@使用不相关子查询

select Name as Customers from Customers as c where Id not in (
    select CustomerId from Orders
);


@使用外连接custormers表,选出custormerid不为null的行
select Name as Customers from Customers as c left outer join Orders as o 
on c.Id=o.CustomerId
where o.CustomerId is null;

9.Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id。

+----+-------+--------+--------------+
| Id | Name  | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1  | Joe   | 70000  | 1            |
| 2  | Henry | 80000  | 2            |
| 3  | Sam   | 60000  | 2            |
| 4  | Max   | 90000  | 1            |
+----+-------+--------+--------------+

Department 表包含公司所有部门的信息。

+----+----------+
| Id | Name     |
+----+----------+
| 1  | IT       |
| 2  | Sales    |
+----+----------+

编写一个 SQL 查询,找出每个部门工资最高的员工。例如,根据上述给定的表格,Max 在 IT 部门有最高工资,Henry 在 Sales 部门有最高工资。

+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT         | Max      | 90000  |
| Sales      | Henry    | 80000  |
+------------+----------+--------+
@对departmentid分组,找出id号和salary,在内联两个表筛选,注意有两个最大的

select d.Name as Department, e.Name as Employee, e.Salary
from Employee as e inner join DepartMent as d on e.DepartmentId=d.Id
where (e.Salary,e.DepartmentId) in (
    select max(Salary),DepartmentId
    from Employee group by DepartmentId
);

10.Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id 。

+----+-------+--------+--------------+
| Id | Name  | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1  | Joe   | 70000  | 1            |
| 2  | Henry | 80000  | 2            |
| 3  | Sam   | 60000  | 2            |
| 4  | Max   | 90000  | 1            |
| 5  | Janet | 69000  | 1            |
| 6  | Randy | 85000  | 1            |
+----+-------+--------+--------------+

Department 表包含公司所有部门的信息。

+----+----------+
| Id | Name     |
+----+----------+
| 1  | IT       |
| 2  | Sales    |
+----+----------+

编写一个 SQL 查询,找出每个部门工资前三高的员工。例如,根据上述给定的表格,查询结果应返回:

+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT         | Max      | 90000  |
| IT         | Randy    | 85000  |
| IT         | Joe      | 70000  |
| Sales      | Henry    | 80000  |
| Sales      | Sam      | 60000  |
+------------+----------+--------+
@因为有重复,所以要对employee表左外连接,进行去重分组选出前3大的id号,
然后在通过employee与department进行子查询来选出最终结果!

select d.Name as Department,e.Name as Employee,e.Salary from
Employee as e inner join Department as d on e.DepartmentId=d.Id
where e.Id in (
    select a.Id from Employee as a left outer join Employee as b 
    on a.DepartmentId=b.DepartmentId and a.Salary

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

+----+------------------+
| Id | Email            |
+----+------------------+
| 1  | [email protected] |
| 2  | [email protected]  |
| 3  | [email protected] |
+----+------------------+
Id 是这个表的主键。

例如,在运行你的查询语句之后,上面的 Person 表应返回以下几行:

+----+------------------+
| Id | Email            |
+----+------------------+
| 1  | [email protected] |
| 2  | [email protected]  |
+----+------------------+
@先选出每组最小的id号,在选出email!
select Id,Email from Person where Id in(
     select min(Id) from Person group by Email
 );

@注意Id最小,没有相等项!!!
@额,发现这个是让删除....
@SQL不允许子查询直接引用外层查询的同一张表,需要select .. as p 生成一张临时表.
delete from Person  where Id not in(
    select * from (select min(Id) as Id from Person  group by Email) as p
);

@隐式的交叉连接(笛卡尔积)
DELETE p1 FROM Person p1, Person p2 WHERE p1.Email = p2.Email AND p1.Id > p2.Id;

12.上升的温度

给定一个 Weather 表,编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的 Id。

+---------+------------------+------------------+
| Id(INT) | RecordDate(DATE) | Temperature(INT) |
+---------+------------------+------------------+
|       1 |       2015-01-01 |               10 |
|       2 |       2015-01-02 |               25 |
|       3 |       2015-01-03 |               20 |
|       4 |       2015-01-04 |               30 |
+---------+------------------+------------------+
例如,根据上述给定的 Weather 表格,返回如下 Id:

+----+
| Id |
+----+
|  2 |
|  4 |
+----+

//使用了相关子查询
//注意DATEDIFF计算日期的差值!
select w1.Id from Weather as w1 where exists(
    select w2.Temperature from Weather as w2 where 
    w1.Temperature>w2.Temperature and DATEDIFF(w1.RecordDate,w2.RecordDate)=1);

13.换座位

小美是一所中学的信息科技老师,她有一张 seat 座位表,平时用来储存学生名字和与他们相对应的座位 id。

其中纵列的 id 是连续递增的

小美想改变相邻俩学生的座位。

你能不能帮她写一个 SQL query 来输出小美想要的结果呢?

示例:

+---------+---------+
|    id   | student |
+---------+---------+
|    1    | Abbot   |
|    2    | Doris   |
|    3    | Emerson |
|    4    | Green   |
|    5    | Jeames  |
+---------+---------+
假如数据输入的是上表,则输出结果如下:

+---------+---------+
|    id   | student |
+---------+---------+
|    1    | Doris   |
|    2    | Abbot   |
|    3    | Green   |
|    4    | Emerson |
|    5    | Jeames  |
+---------+---------+

确定id,使用相关子查询找出上一个或者下一个学生的名字。注意ifnull。
select s1.id,ifnull( 
     (select s2.student from seat as s2 where
 (s1.id%2=1 and s2.id=s1.id+1) or (s1.id%2=0 and s1.id-1=s2.id)),
     s1.student) as student from seat as s1;

 

你可能感兴趣的:(数据库,SQL,面试)