打卡第6天 -- 3S2A1P : 三道sql,2道算法,1道简答
sql-1
https://www.nowcoder.com/practice/b9068bfe5df74276bd015b9729eec4bf?tpId=82&&tqId=29775&rp=1&ru=/ta/sql&qru=/ta/sql/question-ranking
对所有员工的当前(to_date='9999-01-01')薪水按照salary进行按照1-N的排名,相同salary并列且按照emp_no升序排列
知识点:
排序:dense_rank() over(order by)
rank() over(order by)
row_number() over( oder by)
select emp_no,salary,dense_rank() over(order by salary desc) rank from salaries where to_date='9999-01-01' order by rank asc,emp_no asc
sql-2
https://www.nowcoder.com/practice/8fe212a6c71b42de9c15c56ce354bebe?tpId=82&&tqId=29776&rp=1&ru=/ta/sql&qru=/ta/sql/question-ranking
获取所有非manager员工当前的薪水情况,给出dept_no、emp_no以及salary ,当前表示to_date='9999-01-01'
知识点:
非:可以用 not in
重要的是处理好:谁是主表,以及查询的先后关系和join 的先后关系和条件
select d.dept_no,d.emp_no,s.salary
from
(select emp_no from employees
where emp_no not in (select emp_no from dept_manager where to_date='9999-01-01'))
t inner join dept_emp d on t.emp_no=d.emp_no
inner join salaries s on t.emp_no=s.emp_no
where s.to_date='9999-01-01'
sql-3
https://www.nowcoder.com/practice/f858d74a030e48da8e0f69e21be63bef?tpId=82&&tqId=29777&rp=1&ru=/ta/sql&qru=/ta/sql/question-ranking
获取员工其当前的薪水比其manager当前薪水还高的相关信息,当前表示to_date='9999-01-01',
结果第一列给出员工的emp_no,第二列给出其manager的manager_no,第三列给出该员工当前的薪水emp_salary,第四列给该员工对应的manager当前的薪水manager_salary
知识点:
如何把问题拆分是关键,其次细节比较多,容易出现笔误
select a.emp_no, b.emp_no, a.salary, b.salary
from
(select de.dept_no,de.emp_no, s.salary
from dept_emp de inner join salaries s
on de.emp_no=s.emp_no and s.to_date='9999-01-01') a -- 员工、薪水、部门
inner join
(select dm.dept_no, dm.emp_no, s.salary
from dept_manager dm inner join salaries s
on dm.emp_no=s.emp_no and s.to_date='9999-01-01') b -- 部门,部门经理,薪水
on a.dept_no = b.dept_no where a.salary > b.salary
-- 方法2
select de.emp_no,
dm.emp_no as manager_no,
s1.salary as emp_salary,
s2.salary as manager_salary
from dept_emp de,dept_manager dm,salaries s1,salaries s2
where de.dept_no=dm.dept_no
and de.emp_no=s1.emp_no
and dm.emp_no=s2.emp_no
and s1.to_date='9999-01-01'
and s2.to_date='9999-01-01'
and s1.salary>s2.salary