MySQL table join query packet statistics (3)

1 query employee salary information than the company's average basic salary?

select
e.empno,e.ename,e.sal
from t_emp as e
join (select avg(sal) as avg from t_emp) t on e.sal > t.avg

2 Number formatting time statistics

select
count(*) as count, max(a.sal),min(a.sal),
avg(a.sal) , floor(avg(DATEDIFF(NOW(),a.hiredate)/365))
from
t_emp a
join t_dept b on a.deptno = b.deptno
where
b. dname = 'RESEARCH'

3 maximum wage query each occupation, the minimum wage average wage ...

select
e.job,
max(e.sal + IFNULL(e.comm,0)),
min(e.sal + IFNULL(e.comm,0)),
avg(e.sal + IFNULL(e.comm,0)),
max(s.grade),
min(s.grade)
from t_emp e join t_salgrade s
on e.sal + IFNULL(e.comm,0) BETWEEN s.losal and s.hisal
GROUP BY e.job

4 query each sector salary exceeds the average salary of employees

select e.empno,
e.ename,
e.sal

from t_emp e join
(select deptno,AVG(sal) avg from t_emp GROUP BY deptno) t
on e.deptno = t.deptno
where e.sal >= t.avg

 5 Statistics department name and number

select
d.dname,count(e.deptno)
from t_dept d
left join t_emp e on d.deptno = e.deptno
GROUP BY d.deptno HAVING d.dname is not null

6 union connection method

(select
d.dname,count(e.deptno)
from t_dept d
left join t_emp e on d.deptno = e.deptno
GROUP BY d.deptno)
union
(select
d.dname,count(*)
from t_dept d
right join t_emp e on d.deptno = e.deptno
GROUP BY d.deptno)

 

Name Number 7 queries boss

select
e.empno,
e.ename,
d.dname,
(e.sal+ IFNULL(comm,0)) AS sal,
s.grade,
FLOOR(DATEDIFF(NOW(),e.hiredate)/365) AS hire,
t.empno m_empno,
t.ename m_ename,
t.dname m_dname
from
t_emp e
left join t_dept d on d.deptno = e.deptno
left join t_salgrade s on e.sal BETWEEN s.losal and s.hisal
left join (select
e.deptno,e.empno,e.ename,d.dname
from t_emp e
join t_dept d on d.deptno = e.deptno) as t on t.empno = e.mgr


 

Guess you like

Origin www.cnblogs.com/ericblog1992/p/11316910.html