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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:02:24+00:00 2026-05-17T18:02:24+00:00

Disclaimer : I’m an SQL newb and this is for a class, but I

  • 0

Disclaimer: I’m an SQL newb and this is for a class, but I could really use a poke in the right direction.

I’ve got these three tables:

student(_sid_, sname, sex, age, year, gpa)
section(_dname_, _cno_, _sectno_, pname)
enroll(_sid_, grade, _dname_, _cno_, _sectno_)
(primary keys denoted by underscores)

I’m trying to write an Oracle-compatible SQL query that returns a table with the student’s name (student.sname) that has the highest gpa in each section (that’s including section.cno and section.sectno) as well as all the other attributes from section.

I’ve managed to use an aggregate query and GROUP BY to get the maximum GPA for each section:

  SELECT MAX(s.gpa), e.cno, e.sectno  
    FROM enroll e, 
         student s  
   WHERE s.sid = e.sid  
GROUP BY e.cno, e.sectno

Let alone the other section attributes, I can’t even figure out how to tack on the student name (student.sname). If I add it to the SELECT clause, it has to be included in GROUP BY which messes up the rest of the query. If I use this entire query inside the WHERE or FROM clause of an outer query, I can only access the three fields in the table, which isn’t that much use.

I know you can’t give me the exact answer, but any hints would be appreciated!

  • 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-17T18:02:25+00:00Added an answer on May 17, 2026 at 6:02 pm

    Assuming Oracle 9i+, to get only one of the students with the highest GPA (in the event of ties) use:

    WITH summary AS (
       SELECT e.*,
              s.name,
              ROW_NUMBER() OVER(PARTITION BY e.cno, e.sectno
                                    ORDER BY s.gpa DESC) AS rank
         FROM ENROLL e
         JOIN STUDENT s ON s.sid = e.sid)
    SELECT s.*
      FROM summary s
     WHERE s.rank = 1
    

    Non CTE equivalent:

    SELECT s.*
      FROM (SELECT e.*,
                   s.name,
                   ROW_NUMBER() OVER(PARTITION BY e.cno, e.sectno
                                         ORDER BY s.gpa DESC) AS rank
              FROM ENROLL e
              JOIN STUDENT s ON s.sid = e.sid) s
     WHERE s.rank = 1
    

    If you want to see all students who tied for GPA, use:

    WITH summary AS (
       SELECT e.*,
              s.name,
              DENSE_RANK OVER(PARTITION BY e.cno, e.sectno
                                  ORDER BY s.gpa DESC) AS rank
         FROM ENROLL e
         JOIN STUDENT s ON s.sid = e.sid)
    SELECT s.*
      FROM summary s
     WHERE s.rank = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

[Disclaimer]: Currently we are running older SQL Server 2000, but we could update to
Disclaimer : I already asked this question , but without the deployment requirement. I
Disclaimer This is not strictly a programming question, but most programmers soon or later
Disclaimer: This is not actually a programming question, but I feel the audience on
Disclaimer: This is for a homework assignment, but the question is not regarding the
Disclaimer: This is for class, however I'm fresh out of ideas and a nudge
Disclaimer: I understand the question is very basic, but I could not find the
Disclaimer: I'm sure this has been answered before but I cannot find anything on
Disclaimer: I've been working too late. But, I'm determined to get through this one
Disclaimer: not sure this is WordPress related or not. I'm following a simple tutorial

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.