sql語句實現查詢例項
說明:本文章銜接上一篇
十三、min()函式與max()函式
查詢員工編號emp_now為10001其自入職以來的薪水salary漲幅值growth
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));
實現語句:
select max(salary)-min(salary)
from salaries
where emp_no='10001';
十四、複雜的查詢語句(巢狀連線)
查詢所有員工自入職以來的薪水漲幅情況,給出員工編號emp_noy以及其對應的薪水漲幅growth,並按照growth進行升序
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));
答題技巧分析:所有員工:故應該採用左連線
動態生成兩張表(入職的工資以及到目前為止的工資)
然後,實現查詢增值;
實現語句:
select sStart.emp_no,sCurrent.salary-sStart.salary as growth
from
(select s.emp_no,s.salary
from employees e left join
salaries s on s.emp_no=e.emp_no
where s.to_date='9999-01-01') sCurrent
inner join
(select s.emp_no,s.salary
from employees e left join
salaries s on s.emp_no=e.emp_no
where e.hire_date=s.from_date) sStart
on sCurrent.emp_no=sStart.emp_no
order by (sCurrent.salary-sStart.salary) asc;
十五、sql語句實現排名
對所有員工的當前(to_date='9999-01-01')薪水按照salary進行按照1-N的排名,相同salary並列且按照emp_no升序排列
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));
select s1.emp_no,s1.salary,count(distinct s2.salary) as rank
from salaries s1,salaries s2
where s1.to_date='9999-01-01' and s2.to_date='9999-01-01' and s1.salary<=s2.salary
group by s1.emp_no
order by s1.salary desc,s1.emp_no asc;
十六、not in的用法
獲取所有非manager員工當前的薪水情況,給出dept_no、emp_no以及salary
,當前表示to_date='9999-01-01'
CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,
`dept_no` char(4) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE `dept_manager` (
`dept_no` char(4) NOT NULL,
`emp_no` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));
實現方法1:
select dep.dept_no,e.emp_no,s.salary
from employees e,dept_emp dep,salaries s
where e.emp_no not in(select emp_no from dept_manager where to_date='9999-01-01')
and dep.emp_no=e.emp_no and e.emp_no=s.emp_no and dep.to_date='9999-01-01' and s.to_date='9999-01-01';
方法二:
select dep.dept_no,e.emp_no,s.salary
from employees e left join dept_emp dep on(dep.emp_no=e.emp_no) left join salaries s on(e.emp_no=s.emp_no)
where e.emp_no not in(select emp_no from dept_manager where to_date='9999-01-01') and
dep.to_date='9999-01-01'
and s.to_date='9999-01-01';
十七、查詢巢狀
獲取員工其當前的薪水比其manager當前薪水還高的相關資訊,當前表示to_date='9999-01-01',
結果第一列給出員工的emp_no,
第二列給出其manager的manager_no,
第三列給出該員工當前的薪水emp_salary,
第四列給該員工對應的manager當前的薪水manager_salary
CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,
`dept_no` char(4) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE `dept_manager` (
`dept_no` char(4) NOT NULL,
`emp_no` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));
技術要點:
本題主要思想是建立兩張表(一張記錄當前所有員工的工資,另一張只記錄部門經理的工資)進行比較,具體思路如下: 1、先用INNER JOIN連線salaries和demp_emp,建立當前所有員工的工資記錄sem 2、再用INNER JOIN連線salaries和demp_manager,建立當前所有員工的工資記錄sdm 3、最後用限制條件sem.dept_no = sdm.dept_no AND sem.salary > sdm.salary找出同一部門中工資比經理高的員工,並根據題意依次輸出emp_no、manager_no、emp_salary、manager_salary實現語句:
select employ.emp_no,manager.emp_no as manager_no,employ.salary as emp_salary,manager.salary
as manager_salary
from
(select deptm.dept_no,deptm.emp_no,s.salary
from dept_manager deptm,salaries s
where deptm.emp_no=s.emp_no and s.to_date='9999-01-01') as manager,
(select depte.dept_no,depte.emp_no,s.salary
from dept_emp depte,salaries s
where depte.emp_no=s.emp_no and s.to_date='9999-01-01')as employ
where employ.dept_no=manager.dept_no and employ.salary>manager.salary;