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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:22:58+00:00 2026-05-28T01:22:58+00:00

I have two tables that looks like this: Class Name Score Top 1 Amy

  • 0

I have two tables that looks like this:

Class  Name  Score  Top
1      Amy    90     X
1      Ben    70     X
1      Chu    80     X
2      Don    60     X
2      Elf    65     X
2      Fez    75     X
2      Ges    35     X
2      Han    40     X

Class NumToppers
1      2
2      3

I want to find Top “NumToppers” from each class and accordingly update “Top” field:

Class  Name  Score  Top
1      Amy    90     Y
1      Ben    70     N
1      Chu    80     Y
2      Don    60     Y
2      Elf    65     Y
2      Fez    75     Y
2      Ges    35     N
2      Han    40     N

I have 100s of “Classes” in my real data. So, although data looks toyish, no toy solutions please.

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

    Referring to this excellent page on selecting the ‘greatest-n-per-group’, here’s the query I came up with. In retrospect it is basically identical to @BassamMehanni’s answer, except that there is no ROW_NUMBER() function in MySQL.

    This assumes you have tables class and toppers.

    Solution:

    Note: if you have a primary ID on the class table that isn’t the compound (Class,Name,Score) combination, use that instead for the join condition marked #@@.

    set @class='';
    set @rank=1;
    UPDATE class         
    LEFT JOIN 
      (SELECT Class,Name,Score,
           @rank:=if(@class=Class,@rank+1,1) as rank, 
           @class:=Class as dummy 
      FROM class ORDER BY Class,Score DESC) c
    ON c.Class=class.Class AND c.Score=class.Score  #@@
       AND c.Name=class.Name                        #@@
    LEFT JOIN toppers
    ON c.Class=toppers.Class
    SET Top = (CASE WHEN rank <= NumToppers THEN 'Y' ELSE 'N' END); 
    

    Explanation

    Basically, this query:

    1. numbers the rows of class from top-to-bottom by score, within each class. That is, ranks each student within each class.
    2. selects rows of class for which the rank is <= NumToppers, for each class.
    3. Updates these.

    For step 1, see the following (from the link I referred you):

    set @class='';
    set @rank=1;
    SELECT Class,Name,Score, 
           @num:=if(@class=Class,@rank+1,1) as rank, 
           @class:=Class as dummy 
    FROM class ORDER BY Class,Score DESC;
    

    This looks through every row of class (after by sorting by class and descending score) and sets the rank to 1 if we’re on to a new class, or to rank+1 if we’re within the same class.

    For step 2, we do a JOIN with toppers on class and pick the top NumToppers rows for each class:

    set @class='';
    set @rank=1;
    SELECT *                                          # NEW
    FROM toppers                                      # NEW
    LEFT JOIN                                         # NEW
      (SELECT Class,Name,Score,                       #\
           @rank:=if(@class=Class,@rank+1,1) as rank, # |(same as step 1)
           @class:=class as dummy                     # |
      FROM class ORDER BY Class,Score DESC) c         #/ 
    ON c.Class=toppers.Class                          # NEW
    WHERE rank <= NumToppers;                         # NEW
    

    Finally, we update on these conditions (step 3). However we have to do UPDATE class explicitly so we have to add in an extra JOIN of step 2 with class:

    set @class='';
    set @rank=1;
    UPDATE class                                      # NEW
    LEFT JOIN 
      (SELECT Class,Name,Score,
           @rank:=if(@class=Class,@rank+1,1) as rank, 
           @class:=class as dummy 
      FROM class ORDER BY Class,Score DESC) c
    ON c.Class=class.Class AND c.Score=class.Score    # NEW (join condition)
       AND c.Name=class.Name                          # NEW (join condition)
    LEFT JOIN toppers
    ON c.Class=toppers.Class
    SET top = (CASE WHEN rank <= NumToppers THEN 'Y' ELSE 'N' END); # NEW
    

    If you have a primary ID on class that is preferrably not compound (ie a one-column ID) then join on that instead.

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

Sidebar

Related Questions

Let's say I have two tables that look like this: TH TH TH TH
I have two tables in my database that look like that: Customer: C_ID city
I have two tables that are joined together. A has many B Normally you
I have two tables that should be joined together by a foreign key relationship,
I have two tables that are considered a single entity in my domain model.
I have two tables that I am joining with the following query... select *
I have two tables that are related via a mapping table: keywords titles I
Suppose I have two tables that are linked (one has a foreign key to
If i have two tables that have nothing in common I want to do
I was wondering, if we have two tables that share one column in common

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.