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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:53:40+00:00 2026-05-19T02:53:40+00:00

We have a school district with 38 elementary schools. The kids took a test.

  • 0

We have a school district with 38 elementary schools. The kids took a test. The averages for the schools are widely dispersed, but I want to compare the averages of JUST THE TOP 10 students from each school.

Requirement: use temporary tables only.

I have done this in a very work-intensive, error-prone sort of way as follows.
(sch_code = e.g., 9043;
— schabbrev = e.g., “Carter”;
— totpct_stu = e.g., 61.3)

DROP TEMPORARY TABLE IF EXISTS avg_top10 ;
CREATE TEMPORARY TABLE avg_top10
   ( sch_code   VARCHAR(4),
     schabbrev  VARCHAR(75),
     totpct_stu DECIMAL(5,1)
   );

INSERT 
  INTO avg_top10
SELECT sch_code
     , schabbrev
     , totpct_stu
  FROM test_table
 WHERE sch_code IN ('5489')
 ORDER
    BY totpct_stu DESC
 LIMIT 10;

-- I do that last query for EVERY school, so the total 
-- length of the code is well in excess of 300 lines.  
-- Then, finally...

SELECT schabbrev, ROUND( AVG( totpct_stu ), 1 ) AS top10
  FROM avg_top10
 GROUP
    BY schabbrev
 ORDER
    BY top10 ;

-- OUTPUT:
-----------------------------------
schabbrev   avg_top10
----------  ---------
Goulding         75.4
Garth            77.7
Sperhead         81.4
Oak_P            83.7
Spring           84.9
-- etc...

Question: So this works, but isn’t there a lot better way to do it?

Thanks!

PS — Looks like homework, but this is, well…real.

  • 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-19T02:53:41+00:00Added an answer on May 19, 2026 at 2:53 am

    Using this technique.

    select sch_code,
           schabbrev,
           ROUND( AVG( totpct_stu ), 1 ) AS top10
    from   (select sch_code,
                   schabbrev,
                   totpct_stu,
                   @num := if(@group = sch_code, @num + 1, 1) as row_number,
                   @group := sch_code as dummy
            from   test_table
            order by sch_code, totpct_stu desc) as x
    where  row_number <= 10
    GROUP BY sch_code,
           schabbrev
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We'll soon have some high school kids here in our company for some training
I've just been messing around with file_get_contents() at school and have noticed, it allows
We have a system that allows our parents (I work for a school district)
I want part of my model to have school and semester. They will both
I have a school project, for which I want to develop a web application
I have a school problem but I do not understand what it actually asks.
I have a school project and i want to dedicate this for New Year,
I have a school project in which I have to implement a chat application,
I have a school project in which we're going to write a financial engine
I have a school project to program multilayer perceptron that classify data into three

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.