Home > Database > Mysql Tutorial > How to Replicate LINQ's .Skip(1000).Take(100) in Pure SQL?

How to Replicate LINQ's .Skip(1000).Take(100) in Pure SQL?

Barbara Streisand
Release: 2024-12-22 05:21:13
Original
678 people have browsed it

How to Replicate LINQ's .Skip(1000).Take(100) in Pure SQL?

Pure SQL Equivalent of LINQ's .Skip(1000).Take(100)

LINQ's .Skip(1000).Take(100) method allows selecting a specific range of rows in a dataset. This can be achieved through SQL with the OFFSET and FETCH keywords.

SQL Equivalent of .Skip()

The .Skip(1000) method filters rows such that it skips the first 1000 rows. In SQL, this can be achieved with the OFFSET keyword. For example, to skip the first 1000 rows of the Sales.SalesOrderHeader table, use:

SELECT *
FROM Sales.SalesOrderHeader
OFFSET 1000 ROWS
Copy after login

SQL Equivalent of .Take()

The .Take(100) method retrieves a specific number of rows. In SQL, this can be achieved with the FETCH keyword. To retrieve the next 100 rows after skipping the first 1000, use:

FETCH NEXT 100 ROWS ONLY
Copy after login

Combined Query

Combining the two operations, the SQL equivalent of .Skip(1000).Take(100) is:

SELECT *
FROM Sales.SalesOrderHeader
OFFSET 1000 ROWS
FETCH NEXT 100 ROWS ONLY
Copy after login

This query will select rows 1000-1100 from the Sales.SalesOrderHeader table. It is an efficient way to retrieve a specific range of rows, especially when working with large datasets.

The above is the detailed content of How to Replicate LINQ's .Skip(1000).Take(100) in Pure SQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template