SELECT m.salary - s.salary FROM (SELECT sal *12as salary FROM emp WHERE ename ='MILLER') as m, (SELECT sal *12as salary FROM emp WHERE ename ='SMITH') as s;
7.列出直接向King汇报的员工
WHERE子句子查询
1
SELECT*FROM emp WHERE mgr = (SELECT empno FROM emp WHERE ename ='King');
FROM子句子查询
1
SELECT e.*FROM emp e, (SELECT empno FROM emp WHERE ename ='King') k WHERE e.mgr = k.empno;
8.列出公司所有员工的工龄,并倒序排列
SQL中获取当前系统时间并格式化显示
1
SELECT DATE_FORMAT(NOW(), "%Y/%m/%d");
1 2 3
SELECT*FROM (SELECT emp.*, DATE_FORMAT(NOW(), "%Y") - DATE_FORMAT(hiredate, "%Y") wage FROM emp) d ORDERBY d.wage DESC;
9.计算管理者与基层员工平均薪资差距
1 2 3
SELECT a.avg_sal - b.avg_sal FROM (SELECTavg(sal) avg_sal FROM emp where job ='MANAGER'or job ='PRESIDENT') a, (SELECTavg(sal) avg_sal FROM emp where job in('CLERK', 'SALESMAN', 'ANALYST')) b;