--显示员工的编号,姓名,工资,工资级别,所在部门的名称;(使用emp表)
SELECT * FROM emp;
SELECT * FROM salgrade;
SELECT * FROM dept;
SELECT e.empno, e.ename, e.sal, s.grade, d.dname
FROM emp e, salgrade s, dept d
WHERE e.sal BETWEEN s.losal AND s.hisal
AND e.deptno = d.deptno;
--现要求查询出每个雇员的姓名,工资,部门名称,工资在公司的等级(salgrade),及领导姓名及工资所在公司的等级
SELECT e1.empno,
e1.ename,
e1.sal,
s1.grade 员工工资等级,
d.dname,
e2.ename,
e2.sal 领导工资,
s2.grade 领导工资等级
FROM emp e1, salgrade s1, salgrade s2, dept d, emp e2
WHERE e1.sal BETWEEN s1.losal AND s1.hisal
AND e1.deptno = d.deptno
AND e1.mgr = e2.empno
AND e2.sal BETWEEN s2.losal AND s2.hisal
ORDER BY e1.sal;
--外联接
SELECT d.*, e.* FROM dept d, emp e WHERE d.deptno(+) = e.deptno; -- 右外联接(一)
SELECT d.*, e.* FROM dept d, emp e WHERE d.deptno = e.deptno(+); --左外联接(一)
SELECT d.*, e.* FROM dept d RIGHT JOIN emp e ON d.deptno = e.deptno; -- 右外联接(二)
SELECT d.*, e.* FROM dept d LEFT JOIN emp e ON d.deptno = e.deptno; -- 左外联接(二) --查询员工编号,姓名,部门编号,部门名称(左外连接)
SELECT e.empno, e.ename, e.deptno, d.dname
FROM emp e
LEFT JOIN dept d
ON e.deptno = d.deptno;
SELECT e.empno, e.ename, e.deptno, d.dname
FROM emp e, dept d
WHERE e.deptno = d.deptno(+);
--查询员工编号,姓名,部门编号,部门名称(右外连接)
SELECT e.empno, e.ename, e.deptno, d.dname
FROM emp e
RIGHT JOIN dept d
ON e.deptno = d.deptno;
SELECT e.empno, e.ename, e.deptno, d.dname
FROM emp e, dept d
WHERE e.deptno(+) = d.deptno;
--查询30号部门的员工
SELECT * FROM emp;
SELECT * FROM emp e JOIN dept d USING (deptno) WHERE deptno = 30; --查询员工编号,姓名,部门编号,部门名称,职务编号,职务名称并且薪资大于8000 按照薪资降序排列
SELECT e.employee_id,
e.last_name,
d.department_id,
d.department_name,
e.salary,
j.job_id,
j.job_title
FROM employees e
INNER JOIN departments d
ON e.department_id = d.department_id
INNER JOIN jobs j
ON e.job_id = j.job_id
WHERE e.salary > 8000
ORDER BY e.salary DESC;
COMMIT;
--查询所有部门的编号,部门名称,部门经理ID,部门经理姓名,部门所在的城市,国家,地区
SELECT * FROM departments;
SELECT * FROM locations;
SELECT * FROM employees;
SELECT * FROM countries;
SELECT d.department_id 部门编号,
d.department_name 部门名称,
d.manager_id 部门经理id,
e.last_name 部门经理姓名,
l.city 部门所在城市,
c.country_name 部门所在国家,
l.state_province 部门所在地区
FROM departments d
LEFT JOIN employees e
ON d.manager_id = e.manager_id
LEFT JOIN locations l
ON d.location_id = l.location_id
LEFT JOIN countries c
ON l.country_id = c.country_id
ORDER BY d.department_id;
--查询谁的工资比'ABEL’
select * from employees;
SELECT first_name || last_name NAME, salary
FROM employees
WHERE salary >= (SELECT salary FROM employees WHERE last_name = 'Abel')
ORDER BY salary;
----查询工资比7654高,同时与7788从事相同工作的全部雇员信息(emp)
SELECT * FROM emp;
SELECT *
FROM emp
WHERE sal > (SELECT sal FROM emp WHERE empno = 7654)
AND job = (SELECT job FROM emp WHERE empno = 7788);
--部门名称,部门员工数,部门平均工资,部门的最低收入雇员的姓名
SELECT d.dname, e.deptno, tmp.c, tmp.a, tmp.m, e.ename
FROM dept d,
emp e,
(SELECT e.deptno, COUNT(e.deptno) c, AVG(sal) a, MIN(sal) m
FROM emp e GROUP BY e.deptno) tmp
WHERE d.deptno = e.deptno
AND e.sal = tmp.m;
--求出每个部门的最低工资的雇员信息
SELECT *
FROM emp e, (SELECT MIN(sal) m FROM emp GROUP BY deptno) tmp
WHERE e.sal = tmp.m;
SELECT *
FROM emp e
WHERE e.sal in (SELECT MIN(sal) m FROM emp GROUP BY deptno);
----查询工资与10部门中的任意一个人相等的员工信息
select * from emp;
SELECT * FROM emp WHERE sal in (SELECT sal FROM emp WHERE deptno = 10);
SELECT * FROM emp WHERE sal = ANY(SELECT sal FROM emp WHERE deptno = 10);
--查询所有是部门经理的员工(employees)
SELECT * FROM employees;
SELECT *
FROM employees
WHERE employee_id IN (SELECT manager_id FROM employees) ORDER BY employee_id;
SELECT *
FROM employees e1
WHERE EXISTS(SELECT manager_id FROM employees e2 WHERE e1.employee_id = e2.manager_id) ORDER BY employee_id;
----查询所有不是部门经理的员工(employees)
SELECT *
FROM employees
WHERE employee_id <> ALL(SELECT manager_id FROM employees WHERE manager_id IS NOT NULL ) ORDER BY employee_id;
SELECT *
FROM employees
WHERE employee_id NOT IN (SELECT manager_id FROM employees WHERE manager_id IS NOT NULL) ORDER BY employee_id;
SELECT *
FROM employees e1
WHERE NOT EXISTS(SELECT manager_id FROM employees e2 WHERE e1.employee_id = e2.manager_id) ORDER BY employee_id;
--查询所有员工人数不少于3人的部门信息(employees)
SELECT * FROM departments;
SELECT * FROM employees;
SELECT d.*,tmp.c
FROM departments d INNER JOIN
(SELECT department_id,COUNT(employee_id) c FROM employees GROUP BY department_id) tmp
ON tmp.c >= 3 AND d.department_id = tmp.department_id;
----查询姓名,职位,薪资在按照 职位,薪资 去重复之后存在的数据
SELECT ename,job,sal FROM emp WHERE exists(select distinct job,sal from emp);
--查询是本部门入职最早的但不是部门经理的员工
SELECT * FROM employees;
--1.查询本部门入职最早的员工
SELECT MIN(hire_date) FROM employees GROUP BY department_id;
--2.查询不是部门经理的员工
SELECT *
FROM employees e1
WHERE NOT EXISTS(SELECT manager_id FROM employees e2 WHERE e1.employee_id = e2.manager_id) ORDER BY employee_id;
--3.结合1、2给出本部门入职最早的但不是部门经理的员工
SELECT *
FROM employees e1
WHERE NOT EXISTS
(SELECT manager_id
FROM employees e2
WHERE e1.employee_id = e2.manager_id)
AND hire_date IN
(SELECT MIN(hire_date) FROM employees GROUP BY department_id)
ORDER BY employee_id;
--查询所有工资超过本部门平均工资的员工
--1. 查询各个部门的平均工资
SELECT department_id,AVG(salary) avgsa FROM employees GROUP BY department_id;
--2.
SELECT e.*, tmp.avgsa
FROM employees e,
(SELECT department_id, AVG(salary) avgsa
FROM employees
GROUP BY department_id) tmp
WHERE e.department_id = tmp.department_id
AND e.salary > tmp.avgsa; --查询所有换过职务的员工
SELECT e.*,jh.* FROM employees e,job_history jh WHERE e.employee_id = jh.employee_id;
SELECT e.*, jh.*
FROM employees e
INNER JOIN job_history jh
ON e.employee_id = jh.employee_id;