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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:57:19+00:00 2026-06-03T07:57:19+00:00

A bit of background info; this is an application that allows users to created

  • 0

A bit of background info; this is an application that allows users to created challenges and then vote on those challenges (bog standard userX-vs-userY type application).

The end goal here is to get a list of 5 users sorted by the number of challenges they have won, to create a type of leaderboard. A challenge is won by a user if it’s status = expired and the user has > 50 votes for that challenge (challenges expire after 100 votes in total).

I’ll simplify things a bit here, but essentially there are three tables:

  1. users

    • id
    • username
    • …
  2. challenges

    • id
    • issued_to
    • issued_by
    • status
  3. challenges_votes

    • id
    • challenge_id
    • user_id
    • voted_for

So far I have an inner query which looks like:

SELECT `challenges`.`id`
FROM `challenges_votes`
LEFT JOIN `challenges` ON (`challenges`.`id` = `challenges_votes`.`challenge_id`)
WHERE `voted_for` = 1
WHERE `challenges`.`status` = 'expired'
GROUP BY `challenges`.`id`
HAVING COUNT(`challenges_votes`.`id`) > 50

Which in this example would return challenge IDs that have expired and where the user with ID 1 has > 50 votes for.

What I need to do is count the number of rows returned here, apply it to each user from the users table, order this by the number of rows returned and limit it to 5.

To this end I have the following query:

SELECT `users`.`id`, `users`.`username`, COUNT(*) AS challenges_won
FROM (
    SELECT `challenges`.`id`
    FROM `challenges_votes`
    LEFT JOIN `challenges` ON (`challenges`.`id` = `challenges_votes`.`challenge_id`)
    WHERE `voted_for` = 1
    GROUP BY `challenges`.`id`
    HAVING COUNT(`challenges_votes`.`id`) > 0
) AS challenges_won, `users`
GROUP BY `users`.`id`
ORDER BY challenges_won
LIMIT 5

Which is kinda getting there but of course the voted_for user ID here is always 1. Is this even the right way to go about this type of query? Can anyone shed any light on how I should be doing it?

Thanks!

  • 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-03T07:57:20+00:00Added an answer on June 3, 2026 at 7:57 am

    I guess the following script will solve your problem:

    -- get the number of chalenges won by each user and return top 5
    SELECT usr.id, usr.username, COUNT(*) AS challenges_won
    FROM users usr
    JOIN (
        SELECT vot.challenge_id, vot.voted_for
        FROM challenges_votes vot
        WHERE vot.challenge_id IN (       -- is this check really necessary?
            SELECT cha.id                 -- if any user is voted 51 he wins, so
            FROM challenges cha           -- why wait another 49 votes that won't
            WHERE cha.status = 'expired'  -- change the result?
        )                                 -- 
        GROUP BY vot.challenge_id
        HAVING COUNT(*) > 50
    ) aux ON (aux.voted_for = usr.id)
    GROUP BY usr.id, usr.username
    ORDER BY achallenges_won DESC LIMIT 5;
    

    Please allow me to propose a small consideration to the condition to close a challenge: if any user wins after 51 votes, why is it necessary to wait another 49 votes that will not change the result? If this constraint can be dropped, you won’t have to check challenges table and this can improve the query performance — but, it can worsen too, you can only tell after testing with your actual database.

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

Sidebar

Related Questions

A bit of background: I am currently working on an application that allows novice
A bit of background. My company has produced an application that takes a SQL
Bit of Background info, I'm using An NSOutlineView with Core Data. This Is What
First, a bit of background info: The HTTP 1.1 specification, circa 1999, recommends that
For a bit of background, I'm writing a meter reading application in C for
Bit support question. Apologies for that. I have an application linked with GNU readline.
I need to outline a little bit of background info for context. Please bear
Relevant background info I've built a little software that can be customized via a
My site looks like this: I've coloured the central info bit red, so you
First of all bit of background. We are developing an application which receives messages

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.