SQL練習(xí)題-2

員工表結(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)行分組,每組個數(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)行分組,每組個數(shù)大于等于2,給出title以及對應(yīng)的數(shù)目t。注意對于重復(fù)的emp_no進(jìn)行忽略。

分析:要忽略重復(fù)的emp_no,先對title分組,然后count(distinct emp_no)可以統(tǒng)計同一title下不同員工的個數(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)計出當(dāng)前各個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')排名第二多的員工編號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,也包括暫時沒有分配部門的員工

部門表結(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.查找員工編號emp_no為10001其自入職以來的薪水salary漲幅值growth

分析:分別找出該員工入職時的薪水及最后一次的薪水,然后做差,別名為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;

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容