Orcle基本语句(四)
--显示员工的编号,姓名,工资,工资级别,所在部门的名称;(使用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;
Orcle基本语句(四)的更多相关文章
- Orcle 查询语句
首先,以超级管理员的身份登录oracle sqlplus sys/bjsxt as sysdba --然后,解除对scott用户的锁 alter user scott ac ...
- Orcle基本语句(三)
COMMIT; --查询表内所有内容 SELECT * FROM stu_info; --查询部分列,并赋予别名 SELECT stu_id 学生标号,stu_name 学生姓名 FROM stu_i ...
- 数据库 数据库SQL语句四
多表查询 等值连接 --查询员工信息,员工号,姓名,月薪,部门名称 select e.empno,e.ename,d.dname from emp e,dept d where e.deptno=d. ...
- Orcle基本语句(六)
-- Created on 2017/1/5 by ADMINISTRATOR DECLARE -- Local variables here i ; v_name ) := '张晓风'; v_age ...
- ORCLE基本语句(二)
select语句的基本语法: [ORDER BY < COLUMN1, COLUMN2, COLUMN3...> [ASC 或 DESC]]
- Orcle常用语句
在SQLPlus界面的操作语句: 查看\设置每行内显示的字符数:show\set linesize [linesize] 查看\设置一次显示的行数:show\set pagesize [pagesiz ...
- Orcle基本语句(一)
--创建学生信息表,约束放在所有列定义之后 CREATE TABLE stu_info( stu_id INTEGER, stu_name NVARCHAR2() NOT NULL, stu_sex ...
- Orcle基本语句(五)
--分页查询---begin --sqlserver top --mysql limit --oracle rownum(伪列),oracle中伪列(rownum,rowid) --查询工资为前五的信 ...
- JavaScript-分支语句与函数
一.分支语句-if语句 四种if语句: 1.if(判断条件) { 满足条件时需执行的语句 } 2.if(判断条件) { 满足条件时需执行的语句 } else { 不满足条件时需执行的语句 } 3.if ...
随机推荐
- CDN 技术详解(DNS,GSLB,Cache)
CDN 是什么 CDN(Content Delivery Network,内容分发网络),即全网内容加速服务.为了尽可能的避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输的更快.更稳 ...
- maven3.2.3+eclipse4.4+JDK1.8+win8.1_64bit环境搭建
--------------------------------------- 博文作者:迦壹 博客标题:win8.1_64bit+eclipse4.4+maven3.2.3+JDK1.8环境搭建 博 ...
- Kettle使用介绍
本文主要阅读目录如下: 1.Kettle概念 2.下载和部署 3.Kettle环境配置 4.Kettle使用及组件介绍 ETL(Extract-Transform-Load的缩写,即数据抽取.转换.装 ...
- 调用C++动态链接库出现错误
解决方式:将托管 System.String 中的内容复制到非托管内存(Marshal.StringToHGlobalAnsi) class HttpsSend { [DllImport(" ...
- 好程序员带你了解一下HTTPS和SSL/TLS协议的背景与基础
>> 相关背景知识 要说清楚 HTTPS 协议的实现原理,至少需要如下几个背景知识. 大致了解几个基本术语(HTTPS.SSL.TLS)的含义 大致了解 HTTP 和 TCP 的关系(尤其 ...
- IOS开发-影院选座算法 限制产生孤座
众所周知目前影院选座是不允许随便选的,我们不可以任性的挑三拣四,最后留下N个单独的座位,目的就是要留下至少2个连着的座位: 另外有些影院的座位摆放并不是规则的,有些座位被过道或者特殊座位分割开,产生了 ...
- 移动互联网广告 - 第十更 - 广告投放运营 DashBoard - 2016/12/10
广告投放运营 DashBoard设计 移动互联网互联网广告投放,数据监控DashBoard,基础样例示意,下图仅供参考(来自于互联网).
- C# MVC 微信支付之微信模板消息推送
微信支付之微信模板消息推送 今天我要跟大家分享的是"模板消息"的推送,这玩意呢,你说用途嘛,那还是真真的牛逼呐.原因在哪?就是因为它是依赖微信 ...
- 怎么打开sql server 数据库日志文件
To Open Log File Viewer, 1. Expand Server Node > 2. Expand SQL Server Agent > 3. Expand Jobs & ...
- Microsoft Capicom 2.1 On 64bit OS
第一步下载capicom.dll http://files.cnblogs.com/files/chen110xi/DLL.7z 第二步注册capicom.dll至SysWow64 第三步VS中设置 ...