w3resource

AdventureWorks Database: Fetch rows from the middle of a sorted table

SQL Query - AdventureWorks: Exercise-36 with Solution

36. From the following table write a query in SQL to skip the first 5 rows and return the next 5 rows from the sorted result set.

Sample table: HumanResources.Department


Click to view Full table

Sample Solution:

SELECT DepartmentID, Name, GroupName  
FROM HumanResources.Department  
ORDER BY DepartmentID   
    OFFSET 5 ROWS  
    FETCH NEXT 5 ROWS ONLY;

Sample Output:

departmentid|name                    |groupname                           |
------------+------------------------+------------------------------------+
           6|Research and Development|Research and Development            |
           7|Production              |Manufacturing                       |
           8|Production Control      |Manufacturing                       |
           9|Human Resources         |Executive General and Administration|
          10|Finance                 |Executive General and Administration|

SQL AdventureWorks Editor:

Practice Online


Contribute your code and comments through Disqus.

Previous: Skip a number of rows from a sorted table.
Next: List all the products that are red or blue.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

SQL: Tips of the Day

T-SQL Cast versus Convert?

CONVERT is SQL Server specific, CAST is ANSI.

CONVERT is more flexible in that you can format dates etc. Other than that, they are pretty much the same. If you don't care about the extended features, use CAST.

Ref : https://bit.ly/3VKT5M8