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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:53:02+00:00 2026-05-25T12:53:02+00:00

So I have two tables, participations and votes. Every row in the table votes

  • 0

So I have two tables, “participations” and “votes”.
Every row in the table “votes” contains a “participation_id” to reference the participation for which the vote was cast.

Now, I want to be able to select the absolute ranking a participation has based on the number of votes it has.

Normally, this would be simple just using this simple query:

SELECT p.id, COUNT(v.id) as votes 
FROM participations as p 
JOIN votes as v on p.id = v.participation_id 
GROUP BY v.participation_id 
ORDER BY votes DESC;

BUT, I have to be able to add some WHERE-clauses in there somewhere. So if I do that, I’ll just get a relative ranking (ie. its ranking relative to the filtered rowset)

Does anybody know if this is possible with just one query!? (ofcourse subqueries are allowed) I hope this question makes sense to anyone.

  • 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-05-25T12:53:03+00:00Added an answer on May 25, 2026 at 12:53 pm

    You need to use a variable, which is incremented every row, and select from your query’s results, like this:

    SET @rank := 0;
    SELECT id, votes, rank
    from (
      SELECT id, votes, (@rank := @rank + 1) as rank
      from (
        SELECT p.id, COUNT(v.id) as votes 
        FROM participations as p 
        JOIN votes as v on p.id = v.participation_id
        WHERE ... -- add your where clause, if any, here to eliminate completely from results
        GROUP BY v.participation_id 
        ORDER BY votes DESC
      ) x
    ) y
    -- Now join to apply filtering to ranked results, for example:
    JOIN participations p1 on p1.id = y.id
    where p1.date between '2011-06-01' and now(); -- just making up an example
    and p1.gender = 'female'; -- for example
    

    Some explanation:

    The phrase (@rank := @rank + 1) increments the variable and returns the result of the increment, which has been given the alias rank.

    The x at the end is the alias of your query’s results and is required by the syntax (but any alias will do – I just chose x). It is necessary to use the inner query, because that is what provides the ordering – you can’t add rank until that’s been done.

    Note:
    Any where clause or other order by processing you want must happen in the inner query – the outer query is only for the rank processing – it takes the final query row set and adds a rank to it.

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

Sidebar

Related Questions

I have two tables. One table contains information about Assets, another Table about their
I have two tables: table a ida valuea 1 a 2 b 3 c
I have two tables one with ID and NAME table 1 ID | NAME
I have two tables user table user_id | name | 1 | peter |
I have two tables, a user table and a application table: User id username
I have two tables connected with one to many relationship. Parent Table is a
I have two tables with this structure: Table one: ID Description Table two: ID
I have two tables. Table Emp id name 1 Ajay 2 Amol 3 Sanjay
I have two tables CREATE TABLE table1 (id int primary key auto_increment,....); CREATE TABLE
I have two tables. One is temp table and another is main table. In

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.