Oracle single row subquery

Source: Internet
Author: User

Oracle single row subquery

Subqueries can contain SELECT statements in SELECT, UPDATE, and DELETE statements. The internal SELECT statement result can be part of the Condition Clause in an external statement,
It can also be used as a temporary table for external queries. Subqueries are of the following types:
1. single-row subquery: Only one row of results is returned if no external results are returned.
2. multi-row subquery: returns zero rows, one row, or multiple rows to the outside.


Single Row subquery code Demo: query the employee name, job, and salary under the SALES Department (SALES. This problem can be achieved through join query. Because all the required results are in the Emp table,

You can first query the Department number corresponding to the sales department from the Dept table, and then query the employee records that match the department according to the current Department number in the Emp table.

The query results from the sales table can be used as the conditions for queries in the Emp table. The SQL statement is implemented as follows:

SQL> SELECT ENAME, JOB, SAL FROM EMP 2 WHERE DEPTNO = (SELECT DEPTNO FROM DEPT WHERE DNAME = 'sales') 3/


Code parsing:
The results of an internal query are used as conditions for an external query.

Note:
If no record is returned for an internal query, the DEPTNO and NULL fields in the external condition are always false, that is, the external query does not return any results.
You can use comparison operators such as =,>, <, >=, <=, and <> in a single row subquery.
The results returned by the internal query of distinct must match the field (DEPTNO) in the external query condition.
If the internal query returns multiple rows, an error occurs.

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.