createtable employee(
employee_id char(4)notnull,
employee_name varchar(100)notnull,
salary integernotnull,
department_id char(4)notnull,primarykey(Employee_id));insertinto employee values('0001','Joe','70000','0001');insertinto employee values('0002','Henry','80000','0002');insertinto employee values('0003','Sam','60000','0002');insertinto employee values('0004','Max','50000','0001');createtable department(
department_id char(4)notnull,
department_name varchar(100)notnull,primarykey(department_id));insertinto department values('0001','IT');insertinto department values('0002','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
groupby department_id)tempontemp.department_id = d.department_id
where employee.salary = max_salary
orderby
max_salary desc;
Q2
createtable seat(id int(11)notnull,
student varchar(100)notnull,primarykey(id));-- 插入数据insertinto seat(id,student)values('1','ABBot');insertinto seat(id,student)values('2','Doris');insertinto seat(id,student)values('3','Emerson');insertinto seat(id,student)values('4','Green');insertinto seat(id,student)values('5','Jeames');SELECT(CASEWHENMOD(id,2)!=0AND counts != id THEN id +1WHENMOD(id,2)!=0AND counts = id THEN id
ELSE id -1END)AS id,
student
FROM
seat,(SELECTCOUNT(*)AS counts
FROM
seat)AS seat_counts
ORDERBY id ASC;
Q3
createtable score(
id intnotnull,
score decimal(3,2)notnull,primarykey(id));-- 插入数据:insertinto score(id,score)values(1,3.50);insertinto score(id,score)values(2,3.65);insertinto score(id,score)values(3,4.00);insertinto score(id,score)values(4,3.85);insertinto score(id,score)values(5,4.00);insertinto score(id,score)values(6,3.65);select score,(selectcount(distinct score)from score
where score>=s.score)as rank
from score as s
orderby score desc;
Q4
-- 创建Logs表createtable Logs(id intnotnull,
num intnotnull,primarykey(id));-- 插入数据insertinto Logs(id,num)values('1','1');insertinto Logs(id,num)values('2','1');insertinto Logs(id,num)values('3','1');insertinto Logs(id,num)values('4','2');insertinto Logs(id,num)values('5','1');insertinto Logs(id,num)values('6','2');insertinto Logs(id,num)values('7','2');selectdistinct A.num ConsecutiveNums from logs as a
innerjoin logs as B on A.id+1= B.id and A.num=B.num
innerjoin logs as C on B.id+1= C.id and B.num=C.num;
Q5
-- 创建tree表createtable tree(id char(1),
p_id integer,primarykey(id));-- 插入数据insertinto tree values('1',null);insertinto tree values('2',1);insertinto tree values('3',1);insertinto tree values('4',2);insertinto tree values('5',2);commit;SELECT id,CASEWHEN p_id ISNULLTHEN'Root'WHEN id in(SELECT p_id FROM tree)THEN'Inner'ELSE'Leaf'ENDASTYPEFROM tree
ORDERBY id
Q6
createtable employee (Id intprimarykey,
Name varchar(255),
Department varchar(255),
Managerid int);-- 插入数据insertinto employee values('101','John','A','null');insertinto employee values('102','Dan','A','101');insertinto employee values('103','James','A','101');insertinto employee values('104','Amy','A','101');insertinto employee values('105','Anne','A','101');insertinto employee values('106','Ron','B','101');select name
from(select ManagerId,count(ID)as n
from employee
groupby ManagerId) m, employee e
where m.ManagerId = e.Id and n >=5;
Q7
createtable employee (Id intprimarykey,
Name varchar(255),
Department varchar(255),
Managerid int);-- 插入数据insertinto employee values('101','John','A','null');insertinto employee values('102','Dan','A','101');insertinto employee values('103','James','A','101');insertinto employee values('104','Amy','A','101');insertinto employee values('105','Anne','A','101');insertinto employee values('106','Ron','B','101');select name
from(select ManagerId,count(ID)as n
from employee
groupby ManagerId) m, employee e
where m.ManagerId = e.Id and n >=5;