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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:59:52+00:00 2026-05-16T01:59:52+00:00

I need some help with a MySQL query. I’m trying to rank participants using

  • 0

I need some help with a MySQL query. I’m trying to rank participants using a WHERE clause. So, I’d like to rank novices, intermediates, and experienced separately. For example:

Rank Name    Grade  Type
----------------------------------
1    Bob     98     Novice 
2    Jill    88     Novice 
3    Jimmy   42     Novice 
1    Mark    87     Intermediate 
2    Scott   85     Intermediate
3    Jim     77     Intermediate 
1    Jane    90     Advanced 
2    John    89     Advanced 
3    Josh    87     Advanced

I’ve tried:

SET @rank=0;

(SELECT @rank:=@rank+1 AS rank, name, grade, type FROM myTable WHERE type='novice' ORDER BY grade DESC)
UNION ALL
(SELECT @rank:=@rank+1 AS rank, name, grade, type FROM myTable WHERE type='intermediate' ORDER BY grade DESC)
UNION ALL
(SELECT @rank:=@rank+1 AS rank, name, grade, type FROM myTable WHERE type='experienced' ORDER BY grade DESC)

I guess that I need to re-set the rank somehow. Maybe I have another problem?

  • 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-16T01:59:53+00:00Added an answer on May 16, 2026 at 1:59 am

    Use:

    SELECT t.name,
           t.grade,
           t.type,
           (SELECT COUNT(*)
              FROM YOUR_TABLE x
             WHERE x.type = t.type
               AND x.grade >= t.grade) AS rank
     FROM YOUR_TABLE t
    

    To handle two grade columns, use:

    SELECT t.name,
           t.grade1,
           t.grade2,
           t.type,
           (SELECT COUNT(*)
              FROM YOUR_TABLE x
             WHERE x.type = t.type
               AND (x.grade1 + x.grade2) >= (t.grade1 + t.grade2)) AS rank
     FROM YOUR_TABLE t
    

    How would I rank the novice and intermediates together and the advanced separately?

    Use:

    SELECT y.*,
           (SELECT COUNT(*)
              FROM (SELECT *, 
                           CASE t.type
                             WHEN 'Advanced' THEN t.type
                             ELSE 'Non-Advanced'
                           END AS group_type
                      FROM YOUR_TABLE) x
             WHERE x.group_type = y.group_type
               AND (x.grade1 + x.grade2) >= (y.grade1 + y.grade2)) AS rank
      FROM (SELECT t.name,
                   t.grade1,
                   t.grade2,
                   t.type,
                   CASE t.type
                     WHEN 'Advanced' THEN t.type
                     ELSE 'Non-Advanced'
                   END AS group_type
              FROM YOUR_TABLE t) y
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help on a MySQL query I'm trying to set up. I
I need some help with a MySQL query. I have two tables, one with
I really need some help with forming a MySQL query that I just cannot
I need some help in converting my script from using mysql to mysqli I
I need some help calculating Pi. I am trying to write a python program
I need some help with jQuery script again :-) Just trying to play with
I'm very new to MySQL so I need some help please, I have a
I need some help understanding the correct way to mix variables with strings like
I am new to using prepared statements in mysql with php. I need some
I need some help from the shell-script gurus out there. I have a .txt

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.