Correlated subqueries and nested subqueries

Source: Internet
Author: User

Executes each row of data and returns its records. Then, the external query makes a decision based on the returned records.

The following uses the northwind database as an example. We need to list the latest releases from each customer.
.

Select o1.mermerid, o1.orderid, o1.orderdate
From orders O1
Where o1.orderdate =
(Select max (orderdate)
From orders O2
Where o2.customerid = o1.customerid)

For each row of data returned by an external query, the internal query is executed on the condition that the result set and
Customerid match. Max () calls limit the result set to a row of data of interest.

If 500 customers place orders, internal queries are performed 500 times-each customerid is executed once.
Now we have an answer: Return 90 rows of data, that is, each of the 90 rows of data represents one
The customer issued an order.

You can easily scale this query. For example, you may want to query the latest orders placed by each customer.
Two orders. In this case, you need to change the internal query to the following form:

Select o1.mermerid, o1.orderid, o1.orderdate
From orders O1
Where o1.orderdate in (
Select Top 2 o2.orderdate
From orders O2
Where o2.customerid = o1.customerid)
Order by customerid

You can optimize this example in different ways to avoid overhead in () predicates.

In my experience, many developers will soon forget the concept of correlated subqueries.
This is really a pity, because correlated subqueries can easily and elegantly answer difficult questions.
Question.

In my opinion, this syntax is feasible and an SQL statement can be used to query the expected results. This makes me think
A Development Manager repeatedly said to me: "The first version is to make it run, and the second version
Is to make it run faster ."

Nested subquery example:
Select nbbm, (select ypm + Gg from zy_sys2_ypzdk where
Zy_sys2_ypzdk.nbbm = zy_yp1_sfmx.nbbm) as PM, bzsl from
Zy_yp1_sfmx

Query the customer's latest five orders (extensions), which is also very classic
Select o1.mermerid, o1.orderid, o1.orderdate
From orders O1
Where o1.orderdate in (
Select top 5 o2.orderdate
From orders O2
Where o2.customerid = o1.customerid)
Order by customerid
Http://lovewinter.itpub.net/post/493/6435

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.