Oracle Sub-Query

Source: Internet
Author: User


Subquery: Includes another query within one query.


Normal subquery


--Find out information about all employees who are higher than 7654 salary SELECT * from emp e where e.sal > (select Sal from emp where empno = 7654);--query out the wage ratio of 7654 high, while with 778 8 Information for all employees in the same job Select * from emp ewhere e.sal > (select Sal from emp where empno = 7654) and E.job = (select Job from EM P WHERE empno = 7788);--check out the lowest wage employee name, job, salary select E.ename, E.job, e.sal from emp ewhere e.sal = (select min (sal) from EMP) ;


In query


The In keyword is used to match records in a collection

--Query employee information for employee number 1234,2345,7369,7900 SELECT * from emp where empno in (1234, 2345, 7369, 7900);



--Query employee number is not 1234,2345,7369,7900 employee information select * from EMP where empno not in (1234, 2345, 7369, 7900);


--Query the employee information for each department's minimum wage select * from emp where Sal in (select min (sal) from EMP Group by DEPTNO);



Any keyword


Any: denotes arbitrary.

< any smaller than any result returned by the subquery, which is less than the maximum value of the returned result

= any and sub-query results are equal, equivalent in

> any larger than any result returned by the subquery, which is greater than the minimum value of the returned result


--Query the minimum wage for each department select min (SAL) min_sal from EMP Group by DEPTNO;


Sal is greater than any (minimum wage per department), which is greater than the minimum value of the returned result

SELECT * from emp where sal > any (select min (sal) from EMP Group by DEPTNO);



Sal = Any (minimum wage per department), i.e. equal to each result in the subquery, with in

SELECT * from emp where sal = any (select min (sal) from EMP Group by DEPTNO);


Sal < Any (minimum wage per department), which is greater than the maximum value of the returned result

SELECT * from emp where Sal < any (select min (sal) from EMP Group by DEPTNO);



All keyword


All: Indicates all.

< All is smaller than all results returned by the subquery, which is less than the minimum value of the returned result

> All is larger than the result returned by the subquery, which is greater than the maximum value of the returned result

= All meaningless and logically not tenable


Check the Payroll collection for wages in the 2000 to 3500 payroll Section

Select distinct sal from EMP where Sal between and 3500;


> All (Payroll collection for payroll at 2000 to 3500), which is greater than the maximum value

SELECT * from emp where sal > All (SELECT DISTINCT Sal from EMP where Sal between and 3500);



< All (Payroll collection for payroll at 2000 to 3500), which is less than the minimum value

SELECT * from emp where Sal < All (select DISTINCT Sal from EMP where Sal between and 3500);




Oracle Sub-Query

Related Article

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.