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

  • Home
  • SEARCH
  • 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 3356588
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:31:51+00:00 2026-05-18T02:31:51+00:00

table 1 id name class 1 ab A 2 cd A 3 ef B

  • 0

table 1

id    name   class
1     ab     A
2     cd     A
3     ef     B    
4     ab     B
5     cd     B

table 2

name   test   marks
ab     1      90
ab     2      70
cd     2      80
cd     3      85
ef     3      85
ef     4      60

Hi, I have 2 tables above, my question is what is the most efficient/best or simplest way to get the highest marks from table 2 for each person and join to table 1 such that returns:

id   name   class    [highest marks]
1    ab     A        90
2    cd     A        85
3    ef     B        85
  • 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-18T02:31:52+00:00Added an answer on May 18, 2026 at 2:31 am

    Assuming SQL Server 2005+, using analytic/ranking/windowing functionality:

    WITH example AS (
      SELECT a.id,
             a.name,
             a.class,
             b.marks,
             ROW_NUMBER() OVER(PARTITION BY a.id
                                   ORDER BY b.marks DESC) AS rank
        FROM TABLE_1 a
        JOIN TABLE_2 b ON b.name = a.name)
    SELECT e.id,
           e.name,
           e.class,
           e.marks
      FROM example e
     WHERE e.rank = 1
    

    Using aggregates:

    SELECT a.id,
           a.name,
           a.class,
           b.marks      
      FROM TABLE_1 a
      JOIN (SELECT t.name,
                   MAX(t.mark) AS max_mark
              FROM TABLE_2
          GROUP BY t.name) b ON b.name = a.name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

@Entity @Table(name = your_entity) public class YourEntityClass implements IEntity<Long> { @Id @SequenceGenerator(name = gen_test_entity2_id,
Why is this thing not working? [Database(Name=Relationships_Test)] [Table(Name = Order)] public class Order {
I have an application managing software tests and a class called TestResult: class TestResult
I'm trying to connect to SQLite database with Ecplise but I have some errors.
I added a counter cache but can't get it to update. But I can
(This looks like a long question, but it's not really, honest!) I am trying
I have been developing some Django app and there's some duplicated code for different
I'm trying to execute a simple stored procedure with Spring/Hibernate using Annotations. Here are
I'm running ruby on Mac OSX 10.6.4, using Ruby 1.8 and activerecord 2.3.8. I'm
I'm fairly new to Zend and am having some difficulties with creating my unit

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.