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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:52:31+00:00 2026-05-14T00:52:31+00:00

How does one handle ties when ranking results in a mysql query? I’ve simplified

  • 0

How does one handle ties when ranking results in a mysql query? I’ve simplified the table names and columns in this example, but it should illustrate my problem:

SET @rank=0;

   SELECT student_names.students, 
          @rank := @rank +1 AS rank, 
          scores.grades
     FROM student_names  
LEFT JOIN scores ON student_names.students = scores.students
 ORDER BY scores.grades DESC

So imagine the the above query produces:

Students  Rank  Grades
=======================
Al         1     90
Amy        2     90
George     3     78
Bob        4     73
Mary       5     NULL
William    6     NULL

Even though Al and Amy have the same grade, one is ranked higher than the other. Amy got ripped-off. How can I make it so that Amy and Al have the same ranking, so that they both have a rank of 1. Also, William and Mary didn’t take the test. They bagged class and were smoking in the boy’s room. They should be tied for last place.

The correct ranking should be:

Students  Rank  Grades
========================
Al         1     90
Amy        1     90
George     2     78
Bob        3     73
Mary       4     NULL
William    4     NULL

If anyone has any advice, please let me know.

  • 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-14T00:52:31+00:00Added an answer on May 14, 2026 at 12:52 am

    EDIT: This is MySQL 4.1+ supported

    Use:

       SELECT st.name,
              sc.grades,
              CASE 
                WHEN @grade = COALESCE(sc.grades, 0) THEN @rownum 
                ELSE @rownum := @rownum + 1 
              END AS rank,
              @grade := COALESCE(sc.grades, 0)
         FROM STUDENTS st
    LEFT JOIN SCORES sc ON sc.student_id = st.id
         JOIN (SELECT @rownum := 0, @grade := NULL) r
     ORDER BY sc.grades DESC
    

    You can use a cross join (in MySQL, an INNER JOIN without any criteria) to declare and use a variable without using a separate SET statement.

    You need the COALESCE to properly handle the NULLs.

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

Sidebar

Ask A Question

Stats

  • Questions 340k
  • Answers 340k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This will get the cursor holding base contact data, and… May 14, 2026 at 4:44 am
  • Editorial Team
    Editorial Team added an answer To get you started on website security, I recommend you… May 14, 2026 at 4:44 am
  • Editorial Team
    Editorial Team added an answer Better use distance May 14, 2026 at 4:44 am

Related Questions

I have a binary field in my database that is hard to describe in
How does one handle a DateTime with a NOT NULL ? I want to
How does one handle logging in and out/creating users, without using Google Users? I'd
I'd like to write a simple game using the Raphael Javascript library, but one
I have been reading about out of memory conditions on Linux, and the following

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.