Precautions during Oracle subquery

Source: Internet
Author: User

We all know that we can use Oracle subqueries in HAVING, and the results of the two query statements can be used for set operations. If you want to know how we use Oracle subqueries in HAVING? And how can the results of the two query statements perform the actual operations on the set? The following are related answers.

1. the query statement can be nested.

Example: SELECT ...... FROM (SELECT ...... FROM table name 1, [Table name 2,...] WHERE condition) WHERE condition 2;

2. Where can I use Oracle subqueries?

When the query conditions are uncertain

Subqueries can also be used in DML (insert, update, delete) statements.

Subqueries are also available in HAVING.

3. The results of the two query statements can be set.

For example:

UNION (remove duplicate records)

Union all (do not remove duplicate records)

10

J2EE @ zxw

Difference set MINUS,

Intersection INTERSECT

4. subquery considerations

First, execute the SQL statement in the brackets, and layer by layer to the outside.

Internal query only once

If multiple result sets are returned IN the layer, you cannot use a comparison character such as <>== to use IN.

5. Example of Oracle subquery (1)

 
 
  1. SQL> select title,avg(salary) from s_emp   
  2. group by title Having avg(salary) =   
  3. (select min(avg(salary)) from s_emp   
  4. group by title);  

Find the job name and salary for the lowest average wage

Example of subquery (2)

The table name in the parent query can be used for Oracle subqueries.

This SQL statement is correct:

 
 
  1. SQL>select cty_name from city where st_code in   
  2. (select st_code from state where st_name='TENNESSEE' and   
  3. city.cnt_code=state.cnt_code);  

Note: The subquery called by the parent query is executed only once.

6. Obtain the SQL statements from 80 to 100 of the result set.

Each result set processed by Oracle has only one ROWNUM field indicating its logical location,

You can only use ROWNUM <100, not ROWNUM> 80.

The following are two SQL statements with better results obtained from Oracle after analysis: 80 to 100

(ID is the field name with the unique keyword)

Statement Syntax:

 
 
  1. SQL> select * from (
  2. (Select rownum as numrow, c. * from (
  3. Select [field_name,...]
    From table_name where Condition 1 order by condition 2) c)
  4. Where numrow> 80 and numrow <= 100)

Order by Condition 3;

The above content is an introduction to Oracle subqueries. I hope you will get some benefits.

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.