Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7818391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:33:43+00:00 2026-06-02T06:33:43+00:00

I have a situation which I need to do pagination along with INNET JOIN

  • 0

I have a situation which I need to do pagination along with INNET JOIN. Here is a similar scenario I have:

DECLARE @categories AS TABLE(
    CatID INT,
    CategoryName NVARCHAR(100)
);

DECLARE @fooTable AS TABLE(
    ID INT,
    CatID INT,
    Name NVARCHAR(100),
    MinAllow INT,
    Price DECIMAL(18,2)
);

INSERT INTO @categories  VALUES(1, 'Cat1');
INSERT INTO @categories  VALUES(2, 'Cat2');
INSERT INTO @categories  VALUES(3, 'Cat3');
INSERT INTO @categories  VALUES(4, 'Cat4');
INSERT INTO @categories  VALUES(5, 'Cat5');

INSERT INTO @fooTable  VALUES(1, 1, 'Product1', 2, 112.2);
INSERT INTO @fooTable  VALUES(3, 1, 'Product3', 5, 233.32);
INSERT INTO @fooTable  VALUES(6, 1, 'Product6', 4, 12.43);
INSERT INTO @fooTable  VALUES(7, 4, 'Product7', 4, 12.43);
INSERT INTO @fooTable  VALUES(8, 5, 'Product8', 4, 12.43);

These are the records I have. As you can see, some categories do not have any products inside @fooTable. As a next step, we have the following SELECT statement:

SELECT * FROM @fooTable ft
INNER JOIN (
    SELECT ROW_NUMBER() OVER (ORDER BY CatID) AS RowNum, * FROM @categories
) AS cat ON (cat.CatID = ft.CatID);

It is a basic JOIN except that the output will also carry the row number of the categories. The result I got for this query is as follows:

ID   CatID   Name          MinAllow    Price     RowNum   CatID    CategoryName
---- ------- ------------- ----------- --------- -------- -------- -------------
1    1       Product1      2           112.20    1        1        Cat1
3    1       Product3      5           233.32    1        1        Cat1
6    1       Product6      4           12.43     1        1        Cat1
7    4       Product7      4           12.43     4        4        Cat4
8    5       Product8      4           12.43     5        5        Cat5

When you look at the RowNum column, you will see that those values are not pagination friendly. So, when I try to paginate this table as follows, I got an incorrect output:

SELECT * FROM @fooTable ft
INNER JOIN (
    SELECT ROW_NUMBER() OVER (ORDER BY CatID) AS RowNum, * FROM @categories
)AS cat ON (cat.CatID = ft.CatID) AND (cat.RowNum BETWEEN 1 AND 2);

The real situation I have is similar to this one but that query is so complicated and I need to get it working with INNER JOIN. I hope I made it clear. Any idea how I got something like that working?

Edit

According to the above result of my first select query, I should be able to retrieve products whose CatID is 1 and 4 on my second query. That’s what I aim.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:33:44+00:00Added an answer on June 2, 2026 at 6:33 am

    One solution can be the next one:

    SELECT  x.*
    FROM
    (
            SELECT  ft.*, 
                    cat.CategoryName,
                    DENSE_RANK() OVER (ORDER BY ft.CatID) AS Rnk
            FROM @fooTable ft
            INNER JOIN @categories cat ON (cat.CatID = ft.CatID)
    ) AS x
    WHERE   x.Rnk BETWEEN 1 AND 2
    

    Results:

    ID CatID Name     MinAllow Price   CategoryName Rnk
    -- ----- -------- -------- ------- ------------ ---
    1  1     Product1 2        112.20  Cat1         1
    3  1     Product3 5        233.32  Cat1         1
    6  1     Product6 4        12.43   Cat1         1
    7  4     Product7 4        12.43   Cat4         2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a situation in which I need to convert a text data into
I have an odd situation in which I need to modify the position of
I have a situation where I need to retrieve data from a query which
We have a situation in which we need to update records based on existing
I have a situation in which I need to extract Data Annotations information from
I have a situation in which i need a reference to the very next
I'm working with vector now and I have an interesting situation which I need
I have a situation where I have a SQL table which pretty much 'parents'
I have a situation where I need to do a self join on a
I have a, probably unique situation in which I need to use a single

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.