创建Employee表,包含所有员工信息,每个员工有其对应的id,salary和 department id.
create table employee(
employee_id char(4) not null,
employee_name varchar(100) not null,
salary integer not null,
department_id char(4) not null,
primary key(Employee_id)
);
insert into employee values('0001','Joe','70000','0001');
insert into employee values('0002','Henry','80000','0002');
insert into employee values('0003','Sam','60000','0002');
insert into employee values('0004','Max','50000','0001');
创建Department 表,包含公司所有部门的信息。
create table department(
department_id char(4) not null,
department_name varchar(100) not null,
primary key(department_id)
);
insert into department values('0001','IT');
insert into department values('0002','Sales');
编写一个 SQL 查询,找出每个部门工资最高的员工。例如,根据上述给定的表格,Max在IT 部门有最高工资,Henry 在 Sales 部门有最高工资。
代码如下:
select d.department_name,employee_name,max_salary
from employee
join department d on employee.department_id = d.department_id
join (select department_id,max(salary) as max_salary
from employee
group by department_id)temp
on temp.department_id = d.department_id
where employee.salary = max_salary
order by
max_salary desc;
小美是一所中学的信息科技老师,她有一张 seat 座位表,平时用来储存学生名字和与他们相对应的座位 id。
其中纵列的id是连续递增的
小美想改变相邻俩学生的座位。
你能不能帮她写一个 SQL query 来输出小美想要的结果呢?
请创建如下所示seat表:
-- 创建seat表
create table seat(id int(11) not null,
student varchar(100) not null,
primary key(id));
-- 插入数据
insert into seat(id,student) values('1','ABBot');
insert into seat(id,student) values('2','Doris');
insert into seat(id,student) values('3','Emerson');
insert into seat(id,student) values('4','Green');
insert into seat(id,student) values('5','Jeames');
例如数据输入的是上表,则输出结果如下:
SELECT
(CASE
WHEN MOD(id, 2) != 0 AND counts != id THEN id + 1
WHEN MOD(id, 2) != 0 AND counts = id THEN id
ELSE id - 1
END) AS id,
student
FROM
seat,
(SELECT
COUNT(*) AS counts
FROM
seat) AS seat_counts
ORDER BY id ASC;
注意:
如果学生人数是奇数,则不需要改变最后一个同学的座位。
编写一个 SQL 查询来实现分数排名。如果两个分数相同,则两个分数排名(Rank)相同。请注意,平分后的下一个名次应该是下一个连续的整数值。换句话说,名次之间不应该有“间隔”。
创建以下score表:
create table score(
id int not null,
score decimal(3,2) not null,
primary key(id)
);
-- 插入数据:
insert into score(id,score) values(1,3.50);
insert into score(id,score) values(2,3.65);
insert into score(id,score) values(3,4.00);
insert into score(id,score) values(4,3.85);
insert into score(id,score) values(5,4.00);
insert into score(id,score) values(6,3.65);
例如,根据上述给定的 Scores 表,你的查询应该返回(按分数从高到低排列):
+-------+------+
| Score | Rank |
+-------+------+
| 4.00 | 1 |
| 4.00 | 1 |
| 3.85 | 2 |
| 3.65 | 3 |
| 3.65 | 3 |
| 3.50 | 4 |
+-------+------+
select score,
(select count(distinct score)
from score
where score>=s.score) as rank
from score as s
order by score desc;
编写一个 SQL 查询,查找所有至少连续出现三次的数字。
-- 创建Logs表
create table Logs(id int not null,
num int not null,
primary key(id)
);
-- 插入数据
insert into Logs(id,num) values('1','1');
insert into Logs(id,num) values('2','1');
insert into Logs(id,num) values('3','1');
insert into Logs(id,num) values('4','2');
insert into Logs(id,num) values('5','1');
insert into Logs(id,num) values('6','2');
insert into Logs(id,num) values('7','2');
例如,给定上面的 Logs 表,1是唯一连续出现至少三次的数字。
select distinct A.num ConsecutiveNums from logs as a
inner join logs as B on A.id+1 = B.id and A.num=B.num
inner join logs as C on B.id+1 = C.id and B.num=C.num;
对于tree表。id是树节点的标识,p_id是其父节点的id。
-- 创建tree表
create table tree(id char(1),
p_id integer,
primary key(id));
-- 插入数据
insert into tree values('1',null);
insert into tree values('2',1);
insert into tree values('3',1);
insert into tree values('4',2);
insert into tree values('5',2);
commit;
每个节点都是以下三种类型中的一种:
写一条查询语句打印节点id及对应的节点类型。按照节点id排序。上面例子的对应结果为:
+----+------+
| id | Type |
+----+------+
| 1 | Root |
| 2 | Inner|
| 3 | Leaf |
| 4 | Leaf |
| 5 | Leaf |
+----+------+
SELECT id,
CASE WHEN p_id IS NULL THEN 'Root'
WHEN id in (SELECT p_id FROM tree) THEN 'Inner'
ELSE 'Leaf' END
AS TYPE
FROM tree
ORDER BY id
说明:
下面是树的图形:
1
/ \
2 3
/ \
4 5
注意:
如果一个树只有一个节点,只需要输出根节点属性。
Employee表包含所有员工及其上级的信息。每位员工都有一个id,并且还有一个对应主管的id(Manager).
+------+----------+-----------+----------+
|Id |Name |Department |ManagerId |
+------+----------+-----------+----------+
|101 |John |A |null |
|102 |Dan |A |101 |
|103 |James |A |101 |
|104 |Amy |A |101 |
|105 |Anne |A |101 |
|106 |Ron |B |101 |
+------+----------+-----------+----------+
-- 删除employee表
-- drop table employee
-- 重新创建employee表
create table employee (Id int primary key,
Name varchar(255),
Department varchar(255),
Managerid int);
-- 插入数据
insert into employee values('101','John','A','null');
insert into employee values('102','Dan','A','101');
insert into employee values('103','James','A','101');
insert into employee values('104','Amy','A','101');
insert into employee values('105','Anne','A','101');
insert into employee values('106','Ron','B','101');
针对Employee表,写一条SQL 语句找出5个下属的主管。对于上面的表,结果应输出:
+-------+
| Name |
+-------+
| John |
+-------+
代码如下:
select name
from (select ManagerId,count(ID) as n
from employee
group by ManagerId) m, employee e
where m.ManagerId = e.Id and n >=5;
注意:
没有人向自己汇报。
练习三的分数表,实现排名功能,但是排名需要是非连续的,如下:
+-------+------+
| Score | Rank |
+-------+------+
| 4.00 | 1 |
| 4.00 | 1 |
| 3.85 | 3 |
| 3.65 | 4 |
| 3.65 | 4 |
| 3.50 | 6 |
+-------+------
代码如下:
select score,rank() over(order by score desc) as rank
from score;
求出survey_log表中回答率最高的问题,表格的字段有:uid,action,question_id,answer_id,q_num,timestamp。
uid是用户id;action的值为:“show”, “answer”, “skip”;当action是"answer"时,answer_id不为空,相反,当action是"show"和"skip"时为空(null);q_num是问题的数字序号。
写一条sql语句找出回答率最高的问题:
输入:
-- 创建survey_log表
create table survey_log(uid int,
action char(100),
question_id int,
answer_id integer,
q_num int,
timestamp int,
primary key(timestamp)
);
-- 插入数据
insert into survey_log values('5','show','285','null','1','123');
insert into survey_log values('5','answer','285','124124','1','124');
insert into survey_log values('5','show','369','null','2','125');
insert into survey_log values('5','ship','369','null','2','126');
输出:
SELECT
question_id AS survey_log
FROM
survey_log
GROUP BY question_id
ORDER BY COUNT(answer_id) / COUNT(IF(action = 'show', 1, 0)) DESC
LIMIT 1;
说明:
问题285的回答率为1/1,然而问题369的回答率是0/1,所以输出是285。
注意:
最高回答率的意思是:同一个问题出现的次数中回答的比例。
将练习七中的Employee表清空,重新插入以下数据(其实是多插入5,6两行):
+----+-------+--------+--------------+
| 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 |
+----+-------+--------+--------------+
编写一个 SQL 查询,找出每个部门工资前三高的员工。例如,根据上述给定的表格,查询结果应返回:
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| IT | Randy | 85000 |
| IT | Joe | 70000 |
| Sales | Henry | 80000 |
| Sales | Sam | 60000 |
+------------+----------+--------+
此外,请考虑实现各部门前N高工资的员工功能。
代码如下:
-- 插入数据
insert into employee values(5,'janet',69000,1),
(6,'randy',85000,1);
SELECT department_name, emplyee_name, salary
FROM department
JOIN(
SELECT emplyee_name,
department_id,
salary,
DENSE_RANK() over (PARTITION BY department_id
ORDER BY salary DESC) as "rank"
FROM employee) salary_rank
ON department.department_id=salary_rank.department_id
WHERE salary_rank.rank<4;
point_2d表包含一个平面内一些点(超过两个)的坐标值(x,y)。
写一条查询语句求出这些点中的最短距离并保留2位小数。
|x | y |
|----|----|
| -1 | -1 |
| 0 | 0 |
| -1 | -2 |
最短距离是1,从点(-1,-1)到点(-1,-2)。所以输出结果为:
+--------+
|shortest|
+--------+
|1.00 |
+--------+
注意: 所有点的最大距离小于10000。
Trips 表中存所有出租车的行程信息。每段行程有唯一键 Id,Client_Id 和 Driver_Id 是 Users 表中 Users_Id 的外键。Status 是枚举类型,枚举成员为 (‘completed’, ‘cancelled_by_driver’, ‘cancelled_by_client’)。
Users 表存所有用户。每个用户有唯一键 Users_Id。Banned 表示这个用户是否被禁止,Role 则是一个表示(‘client’, ‘driver’, ‘partner’)的枚举类型。
+----------+--------+--------+
| Users_Id | Banned | Role |
+----------+--------+--------+
| 1 | No | client |
| 2 | Yes | client |
| 3 | No | client |
| 4 | No | client |
| 10 | No | driver |
| 11 | No | driver |
| 12 | No | driver |
| 13 | No | driver |
+----------+--------+--------+
写一段 SQL 语句查出2013年10月1日至2013年10月3日期间非禁止用户的取消率。基于上表,你的 SQL 语句应返回如下结果,取消率(Cancellation Rate)保留两位小数。
+------------+-------------------+
| Day | Cancellation Rate |
+------------+-------------------+
| 2013-10-01 | 0.33 |
| 2013-10-02 | 0.00 |
| 2013-10-03 | 0.50 |
+------------+-------------------+