員工表結(jié)構(gòu):
部門表結(jié)構(gòu):
部門員工表結(jié)構(gòu):
部門經(jīng)理表結(jié)構(gòu):
薪資表結(jié)構(gòu):
1.獲取所有員工當(dāng)前的manager筷黔,如果當(dāng)前的manager是自己的話結(jié)果不顯示,當(dāng)前表示to_date='9999-01-01'嫉沽。結(jié)果第一列給出當(dāng)前員工的emp_no,第二列給出其manager對應(yīng)的manager_no史翘。
分析:join連接兩表番官,限制條件dept_no要相等备籽,而兩表emp_no不相等舶治,兩表to_date='9999-01-01'
select de.emp_no, dm.emp_no as manager_no from dept_emp as? de inner join dept_manager dm?
on de.dept_no=dm.dept_no?
where de.to_date='9999-01-01'?
and dm.to_date='9999-01-01'
?and de.emp_no<>dm.emp_no;
2.獲取所有部門中當(dāng)前員工薪水最高的相關(guān)信息,給出dept_no, emp_no以及其對應(yīng)的salary
分析:對部門group by分組,max()函數(shù)找到salary最大值车猬,限定條件to_date='9999-01-01'
select d.dept_no, s.emp_no,max(salary) from dept_emp d, salaries s?
where d.emp_no=s.emp_no?
and d.to_date=s.to_date?
and s.to_date='9999-01-01'?
group by d.dept_no;
3.從titles表獲取按照title進(jìn)行分組霉猛,每組個(gè)數(shù)大于等于2,給出title以及對應(yīng)的數(shù)目t珠闰。
titles表結(jié)構(gòu):
分析:group by分組惜浅,count() 函數(shù)記錄行數(shù),having過濾條件伏嗜。
select title,count(*) t from titles group by title having count(*)>=2;
4.從titles表獲取按照title進(jìn)行分組坛悉,每組個(gè)數(shù)大于等于2伐厌,給出title以及對應(yīng)的數(shù)目t。注意對于重復(fù)的emp_no進(jìn)行忽略裸影。
分析:要忽略重復(fù)的emp_no挣轨,先對title分組,然后count(distinct emp_no)可以統(tǒng)計(jì)同一title下不同員工的個(gè)數(shù)
select title,count(distinct emp_no) t from titles group by title having t>=2;
5.查找employees表所有emp_no為奇數(shù)空民,且last_name不為Mary的員工信息刃唐,并按照hire_date逆序排列
分析: 使用where過濾條件, 奇數(shù)判斷可用emp_no%2=1,
select * from employees where emp%2=1 and last_name<>Mary order by hire_date desc;
6.統(tǒng)計(jì)出當(dāng)前各個(gè)title類型對應(yīng)的員工當(dāng)前(to_date='9999-01-01')薪水對應(yīng)的平均工資羞迷。結(jié)果給出title以及平均工資avg界轩。
分析:聯(lián)合查詢,限定條件t.emp_no=s.emp_no衔瓮,限制當(dāng)前(9999-01-01)條件浊猾,對title進(jìn)行分組,avg()函數(shù)求平均值
select t.title, avg(salary) avg from titles t,salaries s?
where t.emp_no=s.emp_no?
and t.to_date=s.to_date?
and s.to_date='9999-01-01'?
group by t.title;?
7.獲取當(dāng)前(to_date='9999-01-01')薪水第二多的員工的emp_no以及其對應(yīng)的薪水salary
分析:要考慮到多人同一薪資的情況热鞍,對薪水分組降序排序葫慎,用limit m,n 查詢第二條記錄
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 limit1,1);
8.查找當(dāng)前薪水(to_date='9999-01-01')排名第二多的員工編號(hào)emp_no、薪水salary薇宠、last_name以及first_name
分析:max()函數(shù)找到第二高的薪水偷办,連接兩表,限制條件為e.emp_no=s.emp_no澄港,to_date='9999-01-01'
select e.emp_no,s.salary,e.last_name,e.first_name from employees e join salaries s?
on e.emp_no=s.emp_no and s.salary=
(select max(salary) from salaries? where salary <?
(select max(salary) from salaries where to_date='9999-01-01'01') and to_date='9999-01-01')?
and e.to_date='9999-01-01';
9.查找所有員工的last_name和first_name以及對應(yīng)的dept_name椒涯,也包括暫時(shí)沒有分配部門的員工
部門表結(jié)構(gòu):
分析:“員工表”、“部門員工表”和“部門表”回梧,用left join連接employees和dept_emp兩表废岂,再用left join連接departments表,最后得到想要的結(jié)果狱意。
select e.last_name,e.first_name,d.dept_name from employees e
left join dept_emp de on e.emp_no = de.emp_no
left join departments d on de.dept_no = d.dept_no;
10.查找員工編號(hào)emp_no為10001其自入職以來的薪水salary漲幅值growth
分析:分別找出該員工入職時(shí)的薪水及最后一次的薪水湖苞,然后做差,別名為growth
select (
(select salary from salaries where emp_no='10001' order by to_date desc limit 1)-
(select salary from salaries where emp_no='10001' order by to_date asc limit 1)
) as growth;