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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:02:48+00:00 2026-05-15T11:02:48+00:00

I have a table that looks something like this: CREATE TABLE student_results(id integer, name

  • 0

I have a table that looks something like this:

CREATE TABLE student_results(id integer, name varchar(32), score float);

Lets make the following two assumptions:

  1. assume that the score goes from 0 to a maximum of 100.
  2. assume that I want to grade students in ‘step sizes’ of 10

so I want to apply the following grading:

Score      Grade Awarded
0-10       GRADE9
10-20      GRADE8
20-30      GRADE7
30-40      GRADE6
40-50      GRADE5
50-60      GRADE4
60-70      GRADE3
70-80      GRADE2
80-90      GRADE1
99-100     GENIUS

I would like to write an SQL query that takes in the following input arguments:

lowest score:  0 in this example
highest score: 100 in this example
'step' size:   10 in this example

As ever, if possible, I would like to write such a query using ANSI SQL. If I have to choose a database, then in order of DECREASING preference, it would have to be:

  • PostgreSQL
  • MySQL

Could someone please explain how I may be able to write an SQL query that does this kind of grading, using the above table as an example?

[Edit]

Sample input data

1, 'homer', 10.5
2. 'santas little helper', 15.2
3, 'bart',  20.5
4, 'marge', 40.5
5. 'lisa', 100

I will have an SQL function grade_rank() – that ranks the student:

The arguments for function grade_rank() are :

1st argument: LOWEST possible score value
2nd argument: HIGHEST possible score value
3rd argument: step size, which determines the levels/divisions between the ranks

select id, name, grade_rank(0,100, 10) grade from student_scores;

the output (based on the input above) should be:

1, homer,               GRADE9
2. santas liitle helper GRADE9
3, bart,                GRADE8
4, marge,               GRADE6
5. lisa,                GENIUS
  • 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-15T11:02:49+00:00Added an answer on May 15, 2026 at 11:02 am

    In this way you can do it more general but the grades will be in reverse order, starting from 1 up to N, ie

    • 0-10 Grade1
    • 10-20 Grade2
    • 20-30 Grade3
    • 30-40 Grade4
    • …

    For example using the values
    step 10
    score 43

    This algorithm

    SELECT (((score-1)-((score-1) % step))/step)+1

    will return 5

    You don’t have to know the maximum score. If the max score is 100 no one will be able to perform higher than 100, you just have to decide the size of the steps. For example if you want a step size of 25. Knowing that the maximum score is 100 there will be 4 grade levels. So by setting step level to 25 instead of 10 the result will be 2, ie grade 2.

    SELECT (((43-1)-((43-1) % 25))/25)+1
    

    Perhaps not right on spot what you expected but maybe generic enough to be useful. Here is how the function would look like in SQL.

    CREATE OR REPLACE FUNCTION grade_rank(IN score integer, IN step integer, OUT rank integer)
    AS 'SELECT ((($1-1)-(($1-1) % $2))/$2)+1'
    LANGUAGE 'SQL';
    

    Now calling this function

    select * from grade_rank(43,10)
    

    returns 5.

    And this the plpgsql equivalent:

    CREATE OR REPLACE FUNCTION grade_rank(IN score integer, IN step integer)
      RETURNS integer AS
    $BODY$ 
    DECLARE rank integer;
    BEGIN
        SELECT (((score-1)-((score-1) % step))/step)+1 INTO rank;
        RETURN rank;
    END;
    $BODY$
      LANGUAGE 'plpgsql';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can do this in global.asax.cs using Global Error Handling… May 15, 2026 at 11:54 pm
  • Editorial Team
    Editorial Team added an answer The user-contributed section in the manual page of rmdir contains… May 15, 2026 at 11:54 pm
  • Editorial Team
    Editorial Team added an answer Afaik, unless you extend the PDO class, I don't think… May 15, 2026 at 11:54 pm

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.