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

  • Home
  • SEARCH
  • 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 8070123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:17:35+00:00 2026-06-05T13:17:35+00:00

I have SQL query which joins 3 tables, one being just a many-to-many connecting

  • 0

I have SQL query which joins 3 tables, one being just a many-to-many connecting the other two. I use a Spring JDBC ResultSetExtractor to convert the ResultSet into my Objects which look roughly like this:

class Customer {
    private String id;
    private Set<AccountType> accountTypes;
    ...
}

The ResultSetExtractor implementation looks like this:

public List<Client> extractData(ResultSet rs) throws SQLException,
    DataAccessException {
        Map<Integer, Client> clientsMap = new LinkedHashMap<Integer, Client>();
        while (rs.next()) {
            int id = rs.getInt("id");
            // add the client to the map only the first time
            if (!clientsMap.containsKey(id)) {
                Client client = new Client();
                client.setId(id);
                ...
                clientsMap.put(id, client);
            }
            // always add the account type to the existing client
            Client client = clientsMap.get(id);
            client.addAccountType(extractAccountTypeFrom(rs, id));
        }
        return new ArrayList<Client>(clientsMap.values());
}

This works fine without pagination.

However, I need to paginate these results. The way I usually do it is by adding this to the query, for example:

SELECT ... ORDER BY name ASC LIMIT 10 OFFSET 30;

However, as this query has joins, when I limit the number of results, I am actually limiting the number of JOINED results (ie. as a client will appear as many times as the number of account types they have, the LIMIT is applied not the number of clients, but to the number of clients*accountTypes, which is not what I want).

The only solution I came up with was to remove the LIMIT (and OFFSET because that would also be wrong) from the query and apply them programmatically:

List<Client> allClients = jdbcTemplate.query....
List<Client> result = allClients.subList(offset, offset+limit);

But this is clearly not a very nice, efficient solution. Is there a better way?

  • 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-05T13:17:38+00:00Added an answer on June 5, 2026 at 1:17 pm

    It’s funny how writing a question makes you think, and actually helps a lot in imagining a solution for your own problem.

    I was able to solve this problem by simply adding the pagination part of the query to a sub-query of my main query, rather than to the main query itself.

    For example, instead of doing:

    SELECT client.id, client.name ...
    FROM clients AS client
    LEFT JOIN client_account_types AS cat ON client.id = cat.client_id
    FULL JOIN account_types AS at ON cat.account_type_id = at.id
    ORDER BY client.name ASC
    LIMIT 10 OFFSET 30;
    

    I am doing this:

    SELECT client.id, client.name ...
    FROM (
        SELECT * FROM clients
        ORDER BY name ASC
        LIMIT 10 OFFSET 0
    ) AS client
    LEFT JOIN client_account_types AS cat ON client.id = cat.client_id
    FULL JOIN account_types AS at ON cat.account_type_id = at.id;
    

    Hope this helps other people too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a complex LINQ to SQL to query, which joins onto two tables
OK I have two sql tables I need to query from which are product
I have two tables with a many to one relationship which represent lots and
I have some data which is being returned by some SQL query which looks
I have an sql query pulling a few results from different tables, one of
I have two tables, one is a list of songs. The other is a
I have an sql query with inner joins of four tables that takes more
I have two tables one named Person , which contains columns ID and Name
I have a SQL query which selects some values (and works across tables in
I have a query which joins 6 tables, produces 800,000 rows, and inserts them

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.