Collection of SQL statement aggregation and other problems

Source: Internet
Author: User

Collection of SQL statement aggregation and other problems

For example, a data table contains 20 data records.
Read the first five data records
Select top 5 * from Table order by id desc
5 Data records after reading the first 5 data records
(That is, to query the first 10 records, take the last 5 Records)
Select top 5 * from table where
Id not in (select top 5 id from table)

========================================================== ========================================================== ===
Select top 11 * FROM (
SELECT ROW_NUMBER () OVER (order by StuID) AS RowNumber ,*
FROM StuInfo) t
WHERE RowNumber> = 10
Suppose StuID is the primary key "student ID" of the StuInfo table ". First sort by student ID, generate a row number, and then return the first 11 records with the row number> = 10, that is, the row number is 10 ~ 11 records of 20.
========================================================== ====================================
Select top 10 * from [table_name] where id in (select top 20 id from [table_name] order by id desc) order by id asc
As shown in this figure, there are 11 to 20 data records.

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.