Introduction to Oracle Database subqueries

Source: Internet
Author: User

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!

  1. Subquery: When a query result is a condition of another query, it is called a subquery.
  2. Notes for using subqueries:
  3. Subqueries can be nested in multiple layers
  4. The subquery must be enclosed in parentheses ().
  5. Subquery Syntax:
  6. SELECT select_list
  7. FROM table
  8. WHERE expr operator
  9. (SELECT select_list
  10. FROM table );
  11. Subquery (internal query) is executed once before the primary query.
  12. The subquery results are used by the primary query (external query ).
  13. Example: query the employee information whose salary is greater than JONES
  14. The analysis process is as follows:
  15. First, query the employee salary of JONES: Result2975
  16. SQL> select sal from emp where ename ='Jones';
  17. What we need to query is: the salary is higher2975The employee information is written as follows:
  18. SQL> select * from emp where sal>2975;
  19. // The results of the preceding subquery are as follows:
  20. SQL> select * from emp where sal> (select sal from emp where ename ='Jones');
  21. Note:
  22. The subquery must be included in brackets.
  23. Place the subquery on the right of the comparison condition.
  24. Subqueries are classified into single-row subqueries and multi-row subqueries Based on the query results (nested query results,
  25. Note:
  26. Single-row operators correspond to single-row subqueries, and multi-row operators correspond to multiple-row subqueries.
  27. Single Row Operator
  28. >,>=, <, <=, <>, =
  29. Example:
  30. // Query information about employees in the same position numbered 7876 and whose salaries are greater than 7521
  31. SQL> select * from emp where job = (select job from emp where empno =7876) And sal> (select sal from emp where empno =7521);
  32. // Subquery contains group functions
  33. SQL> select * from emp where sal> (select avg (nvl (sal,0) From emp );
  34. // 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
  35. SQL> select deptno, min (sal) from emp group by deptno having min (sal)> (select min (sal) from emp where deptno =20);
  36. Note: The subquery can return empty rows without any query results.
  37. Multi-row subquery
  38. Multiple rows are returned.
  39. Use the multi-line comparison operator.
  40. Operators include:
  41. Operator description
  42. In is equal to any
  43. Any subquery returns the same value and some
  44. Comparison between All and All values returned by the subquery
  45. Exists
  46. // 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.
  47. SQL> select * from emp where sal <any (select sal from emp where job ='Cler') And job <>'Cler';
  48. // Compare all with all values> all indicates that the value is greater than the maximum value of the query result.
  49. SQL> select * from emp where sal> all (select sal from emp where job ='Cler') And job <>'Cler';
  50. // The employee information with the same position number as 10 does not include the employee information.
  51. SQL> select * from emp where job in (select job from emp where deptno =10) And deptno <>10;
  52. Only thinking?
  53. SQL> select * from emp where exists (select * from dept );
  54. EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
  55. ----------------------------------------------------------------
  56.  7369SMITH CLERK7902 1980-12-17800.0020
  57.  7499ALLEN SALESMAN7698 1981-2-201600.00300.0030
  58.  7521WARD SALESMAN7698 1981-2-221250.00500.0030
  59. .....
  60. SQL> select * from emp where exists (select * from dept where deptno =80);
  61. EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
  62. ----------------------------------------------------------------
  63. 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

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.