Associate subqueries and nested subqueries

Source: Internet
Author: User
Associate subquery and nested subquery SQL code www.2cto. comcreatetableEMP (EMPNONUMBER (4) notnull, ENAMEVARCHAR2 (10), JOBVARCHAR2 (9), MGRNUMBER (4), HIREDATEDATE, SALNUMBER (7, 2), DEPTNONUMBER (2, to query information of all employees lower than the average salary of the Department

SQL code www.2cto.com create table EMP (EMPNO NUMBER (4) not null, ENAME VARCHAR2 (10), JOB VARCHAR2 (9), MGR NUMBER (4 ), hiredate date, sal number (), deptno number (2); as shown in the table above, you need to query information about all employees lower than the average salary of your department.

Associate subqueries and nested subqueries

SQL code 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)

);

As shown in the table above, you need to query information about all employees lower than the average salary of your department.

Nested subquery:

SQL code

Select * from emp a where a. sal <(select avg (sal) from emp B where B. deptno = a. deptno)

It can be seen that each record must be associated with a subquery (each record must first query its own deptno and then subquery), which is not efficient.

Associated subquery:

SQL code

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

Where a. deptno = B. deptno

And a. sal <B. sal;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.