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 8627711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:21:06+00:00 2026-06-12T08:21:06+00:00

I often user a separate query to count rows to set a total variable

  • 0

I often user a separate query to count rows to set a total variable when querying a table for paginated results.

Returns total users in table

SELECT Count(*) FROM users WHERE organizationId = :organizationId

Returns paginated results

SELECT * FROM users WHERE organizationId = :organizationId LIMIT :start, :end

public SearchResults findAll(User user, Paginator paginator) {
        HashMap<String, Object> namedParameters = new HashMap<String, Object>();
        namedParameters.put("organizationId", user.getOrganizationId());
        namedParameters.put("userId", user.getId());
        NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);

        //Get count
        int total = namedParameterJdbcTemplate.queryForInt(SQL_FIND_ALL_COUNT, namedParameters);
        int start = 0;
        int end = total;

        if(paginator != null) {         
            start = paginator.getFirst();
            end = paginator.getCount();
        }

        namedParameters.put("start", start);
        namedParameters.put("end", end);
        SqlRowSet results = namedParameterJdbcTemplate.queryForRowSet(SQL_FIND_ALL, namedParameters);
        return new SearchResults(mapUser(results), total);
    }

The only reason for collecting the total is so I can return that as part of the search results, because its necessary for pagination so I know how many pages of results to display to the user without actually having to pass the results and find every record in the database.

Is there a way to do this in one query? So in other words, get the total count, while still using the LIMIT condition to only query for a specific amount (say 10 records at a time). If so, from a performance standpoint, does it make more sense to just keep these in separate queries?

Something like this?

SELECT *, (SELECT Count(*) FROM users WHERE organizationId = :organizationId) FROM users WHERE organizationId = :organizationId"

If the above query works, how do I actually get that subquery column, does it have a name I can reference?

results.getInt("count"); //?
  • 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-12T08:21:07+00:00Added an answer on June 12, 2026 at 8:21 am

    Add an ALIAS after the subquery

    SELECT  *, 
           (
            SELECT Count(*) 
            FROM users 
            WHERE organizationId = :organizationId
           ) `COUNT`
    FROM users
    WHERE organizationId = :organizationId
    

    Alternative solution in your query is to use CROSS JOIN

    SELECT  *, x.totalCount
    FROM users, 
           (
            SELECT Count(*) totalCount
            FROM users 
            WHERE organizationId = :organizationId
           ) x
    WHERE organizationId = :organizationId
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I often use the net user command to have a look at AD groups
So often, I want the user to select a start date and a finish
Our website has one section which often changes to match the user. (examples are
To complete a Yii form field, users often need to search for a referenced
Let's say we have this query SELECT * FROM table And this result from
Often a user will misuse a Rich Internet Application, and attempt to use the
User interfaces often consist of different input devices like buttons, input fields, dialog boxes,
Often I create Child threads within the main() as Thread thread = new Thread(new
Often times I write some SQL like this: string sql = @ -- Multi-line
Often when I'm working on a project with others, the amount of library paths

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.