SQL Method for retrieving data between two values (for example, data between and)

Source: Internet
Author: User

Question: retrieve data between 100 and 200 entries in a table

Method 1: temporary tableCopyCodeThe Code is as follows: Select top 200 * into # Aa from Table order by time -- Insert the top M pen into the temporary table
Set rowcount 100
Select * from # Aa order by time DESC

-- Drop table # Aa -- delete a temporary table

Method 2:Copy codeThe Code is as follows: Select Top 100 * from
(Select top 200 * from Table order by time ASC)
Order by time DESC

Method 3: not inCopy codeThe Code is as follows: Select Top 100 * From v_company where (
Id not in
(Select Top 100 ID from v_company order by id asc)
) Order by ID ASC

Here we only list three methods I tested. There are other solutions that can be supplemented by experts. The efficiency of the three solutions is not the same. I always think that not in is not efficient, however, using not in here is the fastest speed. Please explain it. Thank you.

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.