search
HomeDatabaseMysql Tutorial【Oracle篇】常用查询与SQL92笔记(一)

-- 在scott.emp表中,输出工资大于本部门平均工资的人员信息(需要使用Oracle优先的查询类型 还要使用 SQL92标准查询) -- 方法一 select max(salnvl(comm,0)),e.empno from emp e group by empno; select avg(salnvl(comm,0)),e.empno from emp e group by


-- 在scott.emp表中,输出工资大于本部门平均工资的人员信息(需要使用Oracle优先的查询类型 还要使用 SQL92标准查询)
-- 方法一
select max(sal+nvl(comm,0)),e.empno from emp e group by empno;

select avg(sal+nvl(comm,0)),e.empno from emp e group by empno;

select distinct e.*
from emp e,emp e2
where  e.deptno=e2.deptno and (select max(sal+nvl(comm,0))from emp e3)>(select avg(sal+nvl(comm,0)) from emp e4);

-- 方法二 
select distinct e.*
from emp e join emp e2
on (e.deptno=e2.deptno and (select max(sal+nvl(comm,0))from emp e3)>(select avg(sal+nvl(comm,0)) from emp e4));


-- 外连接:输出20部门对应员工信息,及其他部门信息
-- 要求:使用Oracle外连接符号, 还要使用 SQL92标准 outer join;

-- 方法一
select e.*,d.*
from dept d,emp e
where d.deptno(+)=e.deptno and d.deptno(+)=20
union
select e2.*,d2.*
from dept d2,emp e2
where d2.deptno=e2.deptno(+) and e2.deptno(+)=20;

-- 方法二 
select e.*,d.*
from dept d full join emp e
on (d.deptno=e.deptno and d.deptno=20);


-- --3、获取在98年10月15日加入项目的所有职员的部门编号、姓名、员工编号、部门名称
-- 方法一
select e.*,d.dept_name,w.*
from employee e,department d,works_on w
where e.dept_no=d.dept_no and e.emp_no=w.emp_no and w.enter_date=(to_date('1998-10-15','yyyy-mm-dd'));

select * from works_on;

-- 方法二
select e.*,d.dept_name
from employee e join department d on(e.dept_no=d.dept_no) join works_on w
on (e.emp_no=w.emp_no and w.enter_date=(to_date('1998-10-15','yyyy-mm-dd')));


--4、获取会计部门(ACCounting)中的职员所工作的项目名称
--方法一 
select p.project_name,e.*
from employee e,department d,works_on w,eproject p
where e.dept_no=d.dept_no and e.emp_no=w.emp_no and w.project_no=p.project_no and lower(trim(d.dept_name))=lower('ACCounting');

--方法二
select p.project_name,e.*
from employee e join department d on(e.dept_no=d.dept_no and lower(trim(d.dept_name))=lower('ACCounting')) join works_on w on(e.emp_no=w.emp_no) join eproject p 
on (w.project_no=p.project_no );

--5、获取与至少一个其他部门拥有相同所在地的所有部门的全部细节信息(自连接)
-- 方法一
select d.*,d2.*
from department d,department d2
where d.location=d2.location;

-- 方法二
select d.*,d2.*
from department d join department d2
on (d.location=d2.location);

select * from department;

--6、获取与至少一位其他职员工作在同一部门且居住在同一城市的每一名职员编号、姓名
--   居住地(使用employee_enh)
-- 方法一
select en.*
from employee_enh en,department d
where en.dept_no=d.dept_no and en.emp_address=en.emp_address;

--方法二
select en.*
from employee_enh en join department d
on(en.dept_no=d.dept_no and en.emp_address=en.emp_address);

--7、获取为项目编号为p3工作的所有职员姓名
-- 方法一
select e.*
from eproject p,employee e,works_on w
where p.project_no=w.project_no and w.emp_no=e.emp_no and p.project_no='p3';

-- 方法二
select e.*
from eproject p join works_on w on(p.project_no=w.project_no)
join employee e on (w.emp_no=e.emp_no and p.project_no='p3');

--10、获取为项目p1工作的所有职员姓名
-- 方法一
select e.*
from eproject p,employee e,works_on w
where p.project_no=w.project_no and w.emp_no=e.emp_no and p.project_no='p1';

-- 方法二
select e.*
from eproject p join works_on w on(p.project_no=w.project_no)
join employee e on( w.emp_no=e.emp_no and p.project_no='p1');

--11、获取工作部门不再Seattle的所有职员的姓名
-- 方法一
select e.*
from employee e,department d
where not exists (select e2.* from employee e2,department d2 where e2.dept_no=d2.dept_no and lower(trim(d2.location))=lower('Seattle'));

-- 方法二
select e.*
from employee e join department d 
on(e.dept_no=d.dept_no and d.location! ='Seattle');

--方法三
select e.*
from department d,employee e
where e.dept_no=d.dept_no and d.location! ='Seattle';

--12、获取工作部门所在地和员工居住地的相同的员工信息
-- 方法一
select en.*,d.*
from department d , employee_enh en
where (d.dept_no=en.dept_no and d.location=en.emp_address);

-- 方法二
select en.*
from department d , employee_enh en
union
select en2.* from department d2 join employee_enh en2 on(d2.dept_no=en2.dept_no and  d2.location=en2.emp_address);

--13、获取工作部门所在地和员工居住地的不同的员工信息
-- 方法一
select en.*
from department d,employee_enh en
where  exists(select en2.* from  department d2,employee_enh en2 where d2.location!=en2.emp_address);

--方法二
select en.*
from department d ,employee_enh en
union
select en2.* from  department d2 join employee_enh en2 on (en2.dept_no=d2.dept_no and d2.location !=en2.emp_address);

还未结束,下次继续更新。。。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
什么是oracle asm什么是oracle asmApr 18, 2022 pm 04:16 PM

oracle asm指的是“自动存储管理”,是一种卷管理器,可自动管理磁盘组并提供有效的数据冗余功能;它是做为单独的Oracle实例实施和部署。asm的优势:1、配置简单、可最大化推动数据库合并的存储资源利用;2、支持BIGFILE文件等。

oracle怎么查询所有索引oracle怎么查询所有索引May 13, 2022 pm 05:23 PM

方法:1、利用“select*from user_indexes where table_name=表名”语句查询表中索引;2、利用“select*from all_indexes where table_name=表名”语句查询所有索引。

Oracle怎么查询端口号Oracle怎么查询端口号May 13, 2022 am 10:10 AM

在Oracle中,可利用lsnrctl命令查询端口号,该命令是Oracle的监听命令;在启动、关闭或重启oracle监听器之前可使用该命令检查oracle监听器的状态,语法为“lsnrctl status”,结果PORT后的内容就是端口号。

oracle全角怎么转半角oracle全角怎么转半角May 13, 2022 pm 03:21 PM

在oracle中,可以利用“TO_SINGLE_BYTE(String)”将全角转换为半角;“TO_SINGLE_BYTE”函数可以将参数中所有多字节字符都替换为等价的单字节字符,只有当数据库字符集同时包含多字节和单字节字符的时候有效。

oracle怎么删除sequenceoracle怎么删除sequenceMay 13, 2022 pm 03:35 PM

在oracle中,可以利用“drop sequence sequence名”来删除sequence;sequence是自动增加数字序列的意思,也就是序列号,序列号自动增加不能重置,因此需要利用drop sequence语句来删除序列。

oracle怎么查询数据类型oracle怎么查询数据类型May 13, 2022 pm 04:19 PM

在oracle中,可以利用“select ... From all_tab_columns where table_name=upper('表名') AND owner=upper('数据库登录用户名');”语句查询数据库表的数据类型。

oracle查询怎么不区分大小写oracle查询怎么不区分大小写May 10, 2022 pm 05:45 PM

方法:1、利用“LOWER(字段值)”将字段转为小写,或者利用“UPPER(字段值)”将字段转为大写;2、利用“REGEXP_LIKE(字符串,正则表达式,'i')”,当参数设置为“i”时,说明进行匹配不区分大小写。

Oracle怎么修改sessionOracle怎么修改sessionMay 13, 2022 pm 05:06 PM

方法:1、利用“alter system set sessions=修改后的数值 scope=spfile”语句修改session参数;2、修改参数之后利用“shutdown immediate – startup”语句重启服务器即可生效。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),