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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:55:04+00:00 2026-06-15T21:55:04+00:00

I have a table Student with fields: Student_id , Student_Name , Mark , Branch

  • 0

I have a table Student with fields: Student_id, Student_Name, Mark, Branch.

I want to get the nth highest mark and name of each branch with in a single query. Is it possible?

for Example if the datas are

S1   |  Amir  |  EC  |  121
S2   |  Ewe   |  EC  |  123
S3   |  Haye  |  EC  |  45
S4   |  Mark  |  EC  |  145
S5   |  Tom   |  CS  |  152
S6   |  Hudd  |  CS  |  218
S7   |  Ken   |  CS  |  48
S8   |  Ben   |  CS  |  15
S9   |  Wode  |  CS  |  123
S10  |  Kayle |  IT  |  125
S11  |  Den   |  IT  |  120
S12  |  Noy   |  IT  |  126

And I am selecting to display the third highest mark in each branch the output should be like

S1   | Amir   | EC   | 121
S9   | Wode   | CS   | 123
S11  | Den    | IT   | 120
  • 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-06-15T21:55:05+00:00Added an answer on June 15, 2026 at 9:55 pm

    This would be much easier if MySQL had windowing functions like several of the other answers have shown. But they don’t so you can use something like the following:

    select student_id,
          student_name,
          branch,
          mark
    from
    (
      select student_id,
          student_name,
          branch,
          mark,
          @num := if(@branch = `branch`, @num + 1, 1) as group_row_number,
          @branch := `branch` as dummy,
          overall_row_num
      from
      (
        select student_id,
          student_name,
          branch,
          mark,
          @rn:=@rn+1 overall_row_num
        from student, (SELECT @rn:=0) r
        order by convert(replace(student_id, 'S', ''), signed) 
      ) src
      order by branch, mark desc
    ) grp
    where group_row_number = 3
    order by overall_row_num
    

    See SQL Fiddle with Demo

    The result would be:

    | STUDENT_ID | STUDENT_NAME | BRANCH | MARK |
    ---------------------------------------------
    |         S1 |         Amir |     EC |  121 |
    |         S9 |         Wode |     CS |  123 |
    |        S11 |          Den |     IT |  120 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have table student,fields are student_id, student_name, year_joining ... now i want to rename
I have a student's table with the following fields: student(student_id, student_name, student_avg) I need
I have a table (session_comments) with the following fields structure: student_id (foreign key to
I have a table student ( id, name ). Then I have one textbox,
I have a table that includes some student group name, lesson time, day names
i have a table which consists of student details like roll no, Name, age.
I have a MYSQL table that is 1275 fields wide. Each row in the
Suppose I have a table with fields Rank , Id and Name On clicking
I have a database that has a student entity: create table student ( name
I have a table called student within it are several fields including: student_matric I

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.