查询在部门的location_id为1700的部门工作的员工的员工号
写法一:
select employee_id from employees e,departments d,locations l where e.department_id = d.department_id and d.location_id = l.location_id and l.location_id = 1700;
写法二:
select employee_id from employees e join departments d on e.department_id = d.department_id
join locations l on d.location_id = l.location_id and l.location_id = 1700;
查询管理者是King的员工姓名和工资
select initcap(concat(e.last_name,e.first_name)) as "姓名",e.salary,e.manager_id from employees e,employees m where e.manager_id=m.manager_id and m.last_name='King';
7.切换到scott表做练习:
找到员工表中工资最高的前三名,如下格式:
select rownum,d.* from (select empno,ename,sal from emp order by sal desc )d where rownum<=3;
统计每年入职的员工个数,效果如下格式
方法一:
select
(select count(*) from emp) total,
(select count(*) from emp where to_char(hiredate, 'yyyy')='1980')"1980",
(select count(*) from emp where to_char(hiredate, 'yyyy')='1981')"1981",
(select count(*) from emp where to_char(hiredate, 'yyyy')='1982')"1982",
(select count(*) from emp where to_char(hiredate, 'yyyy')='1987')"1987",
(select count(*) from emp where to_char(hiredate, 'yyyy')='1990') "1990"
from emp where rownum=1;
方法二:
select count(*) total,
sum(Decode(to_char(hiredate, 'yyyy'), '1980', 1, 0)) "1980",
sum(Decode(to_char(hiredate, 'yyyy'), '1981', 1, 0)) "1981",
sum(Decode(to_char(hiredate, 'yyyy'), '1982', 1, 0)) "1982",
sum(Decode(to_char(hiredate, 'yyyy'), '1987', 1, 0)) "1987",
sum(Decode(to_char(hiredate, 'yyyy'), '1990', 1, 0)) "1990"
from emp;