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

Source: Internet
Author: User
Here we only list three methods I tested. Other methods are supplemented by experts, and the efficiency of the three solutions is not the same. I always think notin is inefficient, however, notin is the fastest to use here. Please explain it. Thank you.

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.

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

Method 1: temporary table
The 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:
The Code is as follows:
Select top 100 * from
(Select top 200 * from table order by time asc)
Order by time desc



Method 3: not in
The 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.