We gradually have a better understanding and knowledge of Oracle. Over time, we will explain the knowledge of SQL subqueries in this course, I hope the content in the course will help you!
- Subquery: When a query result is a condition of another query, it is called a subquery.
- Notes for using subqueries:
- Subqueries can be nested in multiple layers
- The subquery must be enclosed in parentheses ().
- Subquery Syntax:
- SELECT select_list
- FROM table
- WHERE expr operator
- (SELECT select_list
- FROM table );
- Subquery (internal query) is executed once before the primary query.
- The subquery results are used by the primary query (external query ).
- Example: query the employee information whose salary is greater than JONES
- The analysis process is as follows:
- First, query the employee salary of JONES: Result2975
- SQL> select sal from emp where ename ='Jones';
- What we need to query is: the salary is higher2975The employee information is written as follows:
- SQL> select * from emp where sal>2975;
- // The results of the preceding subquery are as follows:
- SQL> select * from emp where sal> (select sal from emp where ename ='Jones');
- Note:
- The subquery must be included in brackets.
- Place the subquery on the right of the comparison condition.
- Subqueries are classified into single-row subqueries and multi-row subqueries Based on the query results (nested query results,
- Note:
- Single-row operators correspond to single-row subqueries, and multi-row operators correspond to multiple-row subqueries.
- Single Row Operator
- >,>=, <, <=, <>, =
- Example:
- // Query information about employees in the same position numbered 7876 and whose salaries are greater than 7521
- SQL> select * from emp where job = (select job from emp where empno =7876) And sal> (select sal from emp where empno =7521);
- // Subquery contains group functions
- SQL> select * from emp where sal> (select avg (nvl (sal,0) From emp );
- // Subquery the department with the having clause to query the department with the minimum wage greater than the minimum wage of Department 20 and the minimum wage
- SQL> select deptno, min (sal) from emp group by deptno having min (sal)> (select min (sal) from emp where deptno =20);
- Note: The subquery can return empty rows without any query results.
- Multi-row subquery
- Multiple rows are returned.
- Use the multi-line comparison operator.
- Operators include:
- Operator description
- In is equal to any
- Any subquery returns the same value and some
- Comparison between All and All values returned by the subquery
- Exists
- // Query the information of any employee whose salary is less than the employee's CLERK and does not include the information of the employee whose job is CLERK.
- SQL> select * from emp where sal <any (select sal from emp where job ='Cler') And job <>'Cler';
- // Compare all with all values> all indicates that the value is greater than the maximum value of the query result.
- SQL> select * from emp where sal> all (select sal from emp where job ='Cler') And job <>'Cler';
- // The employee information with the same position number as 10 does not include the employee information.
- SQL> select * from emp where job in (select job from emp where deptno =10) And deptno <>10;
- Only thinking?
- SQL> select * from emp where exists (select * from dept );
- EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
- ----------------------------------------------------------------
- 7369SMITH CLERK7902 1980-12-17800.0020
- 7499ALLEN SALESMAN7698 1981-2-201600.00300.0030
- 7521WARD SALESMAN7698 1981-2-221250.00500.0030
- .....
- SQL> select * from emp where exists (select * from dept where deptno =80);
- EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
- ----------------------------------------------------------------
- The above content is copyrighted by redarmy_chen. If you need to reprint it, please attach the source. If you have any questions, please send an email to redarmy_chen.@ Qq. Com