select emp_no, birth_date, first_name, last_name, gender, hire_date
from (
select , row_number() over (order by hire_date desc) as rownum
from employees
) a1
where rownum = 3;
另外还有几个新增知识点:
row_number() over (partition by col1 order by col2) 表示按col1进行分区,然后每个区块内按col2进行升序排序,并给对应的区块内编号(也可以不写分区,就整个数据集为一个分区);
select a.emp_no, b.salary, b.from_date, b.to_date, a.dept_no
from (select
from dept_manager
) as a
join (select *
from salaries
) as b
on a.emp_no = b.emp_no
order by a.emp_no;
涉及知识点: <br>
join 连接,还有左连接(left join)和右连接(right join)
sql 执行顺序(一天发一遍,记住)
from -> where -> group by -> having -> select -> order by -> limit
select b.,a.dept_no
from dept_manager as a, salaries as b
where a.to_date='9999-01-01'
and b.to_date='9999-01-01'
and a.emp_no = b.emp_no
order by b.emp_no;
SQL4 查找所有已经分配部门的员工的last_name和first_name以及dept_no
我的思路: 直接通过emp_no连接两个表就可以啦,和上一个题几乎一样。 我的题解:
select a.last_name, a.first_name, b.dept_no
from (select emp_no, dept_no
from dept_emp) as b
join (select emp_no, last_name, first_name
from employees) as a
on b.emp_no = a.emp_no;
涉及知识点: <br>
子查询(嵌套查询)
完整sql执行顺序(每天看一遍,不信记不住):
from -> where -> group by -> having -> select -> order by -> limit
select a.last_name, a.first_name, b.dept_no
from (select emp_no, dept_no
from dept_emp) as b
right join (select emp_no, last_name, first_name
from employees) as a
on b.emp_no = a.emp_no;
涉及知识点: <br>
子查询(嵌套查询)
完整sql执行顺序(每天看一遍,不信记不住):
from -> where -> group by -> having -> select -> order by -> limit
select tmp.emp_no, count(tmp.whether) as t
from ( select emp_no,
ifnull(s1.salary > (select salary
from salaries as s2
where s1.from_date > s2.from_date
and s1.emp_no = s2.emp_no
order by s2.from_date desc
limit 1),
1) as whether
from salaries as s1) as tmp
group by tmp.emp_no
having count(tmp.whether) > 15;另外还有几个新增知识点:
select a.emp_no, b.emp_no
from dept_emp as a
left join dept_manager as b
on a.dept_no = b.dept_no
and a.emp_no != b.emp_no
where b.emp_no is not null
;
涉及知识点: <br>
完整sql执行顺序(每天看一遍,不信记不住):
from -> where -> group by -> having -> select -> order by -> limit
join 连接表
提交结果:<br> 其他题解:
同样思路,可以直接join两表。
select a.emp_no, b.emp_no
from dept_emp as a
join dept_manager as b
on a.dept_no = b.dept_no
and a.emp_no != b.emp_no;
select dept_no, emp_no, salary
from (
select a.dept_no, a.emp_no, b.salary
from dept_emp as a
join salaries as b
on a.emp_no = b.emp_no
and a.to_date='9999-01-01'
and b.to_date='9999-01-01') as a2
where a2.salary = (
select max(b.salary)
from dept_emp as a
join salaries as b
on a.emp_no = b.emp_no
and a.to_date='9999-01-01'
and b.to_date='9999-01-01'
where a2.dept_no = a.dept_no
group by a.dept_no
)
order by dept_no
;
涉及知识点: <br>
完整sql执行顺序(每天看一遍,不信记不住):
from -> where -> group by -> having -> select -> order by -> limit
select title, avg(salary) as avg_s
from (
select a.emp_no, a.title, b.salary
from titles as a
join salaries as b
on a.emp_no = b.emp_no
and a.to_date = b.to_date
and a.to_date = '9999-01-01') as c
group by title
order by avg_s
;
涉及知识点: <br>
完整sql执行顺序(每天看一遍,不信记不住):
from -> where -> group by -> having -> select -> order by -> limit
select emp_no, salary
from salaries
where to_date = '9999-01-01'
and salary = (
select salary
from salaries
group by salary
order by salary desc
limit 1,1
)
order by salary desc
;
SQL18 获取当前薪水第二多的员工的emp_no以及其对应的薪水salary
我的思路: 紧接上一题,和sql17的区别就是 将员工薪水另外存了一张表,另外要求不能使用order by 排序。我们可以先找出薪水表中第二高的薪水,然后从employees、salaries两表中筛选出薪水第二高的员工信息。
取第二高的薪水,这里采用的是去除当前最高薪水后,再查找剩余薪水中的最高薪水。 我的题解:
select a.emp_no, b.salary, a.last_name, a.first_name
from employees as a
join salaries as b
on a.emp_no = b.emp_no
and b.salary = (
select max(salary)
from salaries
where to_date = '9999-01-01'
and salary < (
select max(salary)
from salaries
where to_date = '9999-01-01'
)
);涉及知识点: <br>
完整sql执行顺序(每天看一遍,不信记不住):
from -> where -> group by -> having -> select -> order by -> limit
select a.emp_no, b.salary, a.last_name, a.first_name
from employees as a
join salaries as b
on a.emp_no = b.emp_no
and b.salary = (
select s1.salary
from salaries as s1
join salaries as s2
on s1.salary <= s2.salary
and s1.to_date = s2.to_date
and s1.to_date = '9999-01-01'
group by s1.salary
having count(distinct s2.salary) = 2
)
;
select a.last_name, a.first_name, b.dept_name
from (
select emp_no, last_name, first_name
from employees) as a
left join (
select d1.emp_no, d2.dept_name
from dept_emp as d1
join departments as d2
on d1.dept_no = d2.dept_no
) as b
on b.emp_no = a.emp_no
;
涉及知识点: <br>
完整sql执行顺序(每天看一遍,不信记不住):
from -> where -> group by -> having -> select -> order by -> limit
select s1.emp_no, (s2.salary-s1.salary) as growth
from (
-- 入职薪水
select a1.emp_no, a2.salary
from employees as a1
join salaries as a2
on a1.emp_no = a2.emp_no
and a1.hire_date = a2.from_date
) as s1
join
(
-- 当前薪水
select emp_no, salary
from salaries
where to_date = '9999-01-01'
) as s2
on s1.emp_no = s2.emp_no
order by growth
;
涉及知识点: <br>
完整sql执行顺序(每天看一遍,不信记不住):
from -> where -> group by -> having -> select -> order by -> limit