第六章:决胜秋招

本文展示了多个SQL查询示例,包括根据部门获取最高薪资员工、计算座位连续编号、按分数排名以及分析日志中连续数字出现的情况。这些查询涉及到数据库的联接操作、聚合函数、子查询和条件过滤,对于理解SQL语言的运用具有很好的实践价值。
摘要由CSDN通过智能技术生成

Q1

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');

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');

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;



Q2

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;

Q3

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);

select score,
(select count(distinct score)
		from score
    	where score>=s.score) as rank
	from score as s
    order by score desc;

Q4

 -- 创建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');


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;

Q5

   -- 创建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;
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


Q6

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');
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;


Q7

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');
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;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值