Home > Database > Mysql Tutorial > 关联子查询和嵌套子查询

关联子查询和嵌套子查询

WBOY
Release: 2016-06-07 14:53:33
Original
1489 people have browsed it

关联子查询和嵌套子查询 Sql代码 www.2cto.com create table EMP ( EMPNO NUMBER(4) not null, ENAME VARCHAR2(10), JOB VARCHAR2(9), MGR NUMBER(4), HIREDATE DATE, SAL NUMBER(7,2), DEPTNO NUMBER(2) ); 如上表,要查询所有低于本部门平均工资的员工信息

关联子查询和嵌套子查询

 

Sql代码    www.2cto.com  

create table EMP    

(    

  EMPNO    NUMBER(4) not null,    

  ENAME    VARCHAR2(10),    

  JOB      VARCHAR2(9),    

  MGR      NUMBER(4),    

  HIREDATE DATE,    

  SAL      NUMBER(7,2),    

  DEPTNO   NUMBER(2)    

);    

 如上表,要查询所有低于本部门平均工资的员工信息

 

嵌套子查询:

Sql代码  

select * from emp a where a.sal

 可以看出每条记录都要关联一个子查询(每条都要先查询自己的deptno,然后再子查询),这样效率不高

 

关联子查询:

Sql代码  

select a.* from emp a ,(select deptno,avg(sal) sal from emp group by deptno) b  

where a.deptno=b.deptno  

and a.sal

 

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template