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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:07:29+00:00 2026-05-23T02:07:29+00:00

Here is a simplified version of my problem: I have two tables: Students :

  • 0

Here is a simplified version of my problem:

I have two tables:

Students:

ST_STUDENT_ID  NAME   ST_DATE_TAKEN
-------------  -----  -------------
1              Jim    2011-01-01
2              Fred   2011-01-02
3              Sarah  2011-01-03
4              Nancy  2001-02-04

SCORES:

SC_STUDENT_ID  SC_SCORE
-------------  --------
1              97
2              97
3              95
4              97

I need to pull the student with the highest score for a month (say January). However, I only want one student even if multiple students received that score and that score could also exist outside my focus month, so this is complicating my query. The only way I could figure to do it was to redo all my criteria at each nested sub-query. Is there a better way. It wasn’t too terrible here, but in my actual problem the where criteria is much more complicated and joined across many tables, duplicating it is a pain, plus the cost gets quite large.

SELECT ST_STUDENT_ID, ST_TIMESTAMP, SC_SCORE
FROM STUDENTS 
JOIN SCORES
  ON ST_STUDENT_ID = SC_STUDENT_ID
WHERE ST_STUDENT_ID = (
  SELECT MAX(ST_STUDENT_ID)
  FROM STUDENTS
  JOIN SCORES
    ON ST_STUDENT_ID = SC_STUDENT_ID
  WHERE ST_TIMESTAMP > '2011-01-01'
    AND ST_TIMESTAMP < '2011-02-01'
    AND SC_SCORE IS NOT NULL
    AND SC_SCORE = (
      SELECT MAX(SC_SCORE)
        FROM STUDENTS
      JOIN SCORES
        ON ST_STUDENT_ID = SC_STUDENT_ID
      WHERE ST_TIMESTAMP > '2011-01-01'
        AND ST_TIMESTAMP < '2011-02-01'))
  • 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-23T02:07:30+00:00Added an answer on May 23, 2026 at 2:07 am

    If you only want one score, and your time period will be passed explicitly into the query, what about something like this?

    SELECT TOP 1 ST_STUDENT_ID, ST_TIMESTAMP, SC_SCORE
    FROM STUDENTS 
    JOIN SCORES ON ST_STUDENT_ID = SC_STUDENT_ID
    WHERE ST_TIMESTAMP >= '2011-01-01'
    AND ST_TIMESTAMP <= '2011-02-01'
    ORDER BY SC_SCORE DESC, ST_STUDENT_ID DESC
    

    That syntax should work for MS SQL Server – different RDBMSs have slightly different syntaxes for the “TOP 1” concept.

    [I see from your later comment that you’re using DB2 – in which case the syntax apparently is FETCH FIRST 1 ROWS ONLY.]

    Note that I’m following the logic in your example, which implies the student with the highest ID takes precedence. Good incentive to register late for class 😉

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

Sidebar

Related Questions

Here is a simplified version of my database model. I have two tables: Image,
Here's a simplified version of a problem I'm working on: I have a bunch
To illustrate the problem, here is a simplified version of my setup. I have
A simplified version of problem I am experiencing: Here is my HTML form: <form
Here is simplified version of my requirement I have a java class say Processor
Here is a simplified version of what I have (not working): prog.h: ... const
Simplified version of the problem: So I have this query which is inside a
THE SITUATION So here's a simplified version of my situation: I have a Payment
I have two data structure classes (this is a simplified version of my code)
Here is a simplified version of what I am doing. I have created a

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.