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